Enhance conditional constructs with 'unless' and update tests; improve memory management in stack operations
h3rald h3rald@h3rald.com
Thu, 03 Apr 2025 07:23:40 +0200
3 files changed,
23 insertions(+),
12 deletions(-)
M
lib/utils.hex
→
lib/utils.hex
@@ -7,15 +7,18 @@ ;;; when
;; q1 q2 -> * ;; If %:q1%% pushes 0x1 on the stack, dequotes %:q2%%. ( - "_when_q" : - "_when_cond" : - (_when_cond .) - (_when_q .) - () + () if - "_when_q" # - "_when_cond" # ) "when" :: + +;;; unless +;; q1 q2 -> * +;; If %:q1%% pushes 0x0 on the stack, dequotes %:q2%%. +( + () + swap + if +) "unless" :: ;; isi ;; a -> i
M
scripts/test.hex
→
scripts/test.hex
@@ -130,7 +130,7 @@
((args len 0x2 ==) ("two") ("no") if "two" ==) ((dup *) "square" :: 0x2 square 0x4 == "square" #) (0x1 "tmp-a" : (tmp-a 0x3 <) (tmp-a 0x1 + "tmp-a" :) while tmp-a 0x3 ==) - (symbols len 0x62 ==) + (symbols len 0x63 ==) ;60 ((0x2 0x0 /) (error "[symbol /] Division by zero" ==) try)@@ -310,14 +310,20 @@ ("This is a test" "This" begins)
;160 ("This is a test" "this" begins not) - ;((0x0 "aaa" begins) (error) try _ "[symbol begins] A string item is required" ==) - ;(("aaa" () begins) (error) try _ "[symbol begins] A string prefix is required" ==) + ((0x0 "aaa" begins) (error) try "[symbol begins] A string item is required" ==) + (("aaa" () begins) (error) try "[symbol begins] A string prefix is required" ==) ("This is a test" "test" ends) ;164 ("This is a test" "Test" ends not) - ;((0x0 "aaa" ends) (error) try _ "[symbol ends] A string item is required" ==) + ((0x0 "aaa" ends) (error) try "[symbol ends] A string item is required" ==) (("aaa" () ends) (error) try "[symbol ends] A string suffix is required" ==) + ((0x1 isi)(0x2) when 0x2 ==) + ;168 + + (0x1 (0x4 iss)(0x2) when 0x1 ==) + (0x3 (0x4 isi)(0x2) unless 0x3 ==) + ((0x1 iss)(0x2) unless 0x2 ==) ) "TESTS" :
M
src/stack.c
→
src/stack.c
@@ -43,7 +43,8 @@ for (size_t i = 0; i < value->quotation_size; i++)
{ if (hex_push(ctx, value->data.quotation_value[i]) != 0) { - HEX_FREE(ctx, value); + // Cannot free the item here because it is still in use + // HEX_FREE(ctx, value); hex_debug_item(ctx, "FAIL", item); return 1; }@@ -237,6 +238,7 @@ hex_debug(ctx, "FREE: item #%zu", i);
hex_free_item(ctx, quotation[i]); // Free each item } } + hex_debug(ctx, "FREE: quotation freed (%zu items)", size); } void hex_free_item(hex_context_t *ctx, hex_item_t *item)