Skip to content

Commit

Permalink
Change semantics of bracketed tuple equality.
Browse files Browse the repository at this point in the history
Comparison between different bracket and normal tuples
will now take into account the delimiter type. This solves strange
non-locality issues in the compiler due to this false equality, and is
more consistent with Janet's otherwise strong equality philosophy.
  • Loading branch information
bakpakin committed Mar 4, 2023
1 parent 7522933 commit aa5c987
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
All notable changes to this project will be documented in this file.

## ??? - Unreleased
- Change semantics around bracket tuples to no longer be equal to regular tuples.
- Add `index` argument to `ffi/write` for symmetry with `ffi/read`.
- Add `buffer/push-at`
- Add `ffi/pointer-buffer` to convert pointers to buffers the cannot be reallocated. This
allows easier manipulation of FFI memory, memory mapped files, and buffer memory shared between threads.
Expand Down
2 changes: 1 addition & 1 deletion src/core/ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@ JANET_CORE_FN(cfun_ffi_pointer_buffer,
"Create a buffer from a pointer. The underlying memory of the buffer will not be "
"reallocated or freed by the garbage collector, allowing unmanaged, mutable memory "
"to be manipulated with buffer functions. Attempts to resize or extend the buffer "
"beyond it's initial capacity will raise an error. As with many FFI functions, it is memory "
"beyond its initial capacity will raise an error. As with many FFI functions, this is memory "
"unsafe and can potentially allow out of bounds memory access. Returns a new buffer.") {
janet_sandbox_assert(JANET_SANDBOX_FFI);
janet_arity(argc, 2, 4);
Expand Down
5 changes: 5 additions & 0 deletions src/core/value.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ int janet_equals(Janet x, Janet y) {
const Janet *t1 = janet_unwrap_tuple(x);
const Janet *t2 = janet_unwrap_tuple(y);
if (t1 == t2) break;
if (JANET_TUPLE_FLAG_BRACKETCTOR & (janet_tuple_flag(t1) ^ janet_tuple_flag(t2))) return 0;
if (janet_tuple_hash(t1) != janet_tuple_hash(t2)) return 0;
if (janet_tuple_length(t1) != janet_tuple_length(t2)) return 0;
push_traversal_node(janet_tuple_head(t1), janet_tuple_head(t2), 0);
Expand Down Expand Up @@ -321,6 +322,7 @@ int32_t janet_hash(Janet x) {
break;
case JANET_TUPLE:
hash = janet_tuple_hash(janet_unwrap_tuple(x));
hash += (janet_tuple_flag(janet_unwrap_tuple(x)) & JANET_TUPLE_FLAG_BRACKETCTOR) ? 1 : 0;
break;
case JANET_STRUCT:
hash = janet_struct_hash(janet_unwrap_struct(x));
Expand Down Expand Up @@ -412,6 +414,9 @@ int janet_compare(Janet x, Janet y) {
case JANET_TUPLE: {
const Janet *lhs = janet_unwrap_tuple(x);
const Janet *rhs = janet_unwrap_tuple(y);
if (JANET_TUPLE_FLAG_BRACKETCTOR & (janet_tuple_flag(lhs) ^ janet_tuple_flag(rhs))) {
return (janet_tuple_flag(lhs) & JANET_TUPLE_FLAG_BRACKETCTOR) ? 1 : -1;
}
push_traversal_node(janet_tuple_head(lhs), janet_tuple_head(rhs), 1);
break;
}
Expand Down
2 changes: 1 addition & 1 deletion test/suite0001.janet
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@

(assert (= 14 (sum (map inc @[1 2 3 4]))) "sum map")
(def myfun (juxt + - * /))
(assert (= '[2 -2 2 0.5] (myfun 2)) "juxt")
(assert (= [2 -2 2 0.5] (myfun 2)) "juxt")

# Case statements
(assert
Expand Down
4 changes: 2 additions & 2 deletions test/suite0002.janet
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@

# Looping idea
(def xs
(seq [x :in '[-1 0 1] y :in '[-1 0 1] :when (not= x y 0)] (tuple x y)))
(seq [x :in [-1 0 1] y :in [-1 0 1] :when (not= x y 0)] (tuple x y)))
(def txs (apply tuple xs))

(assert (= txs '[[-1 -1] [-1 0] [-1 1] [0 -1] [0 1] [1 -1] [1 0] [1 1]]) "nested seq")
(assert (= txs [[-1 -1] [-1 0] [-1 1] [0 -1] [0 1] [1 -1] [1 0] [1 1]]) "nested seq")

# Generators
(def gen (generate [x :range [0 100] :when (pos? (% x 4))] x))
Expand Down

0 comments on commit aa5c987

Please sign in to comment.