all repos — hex @ 55b0654160b7b4f7dca7b3bfa6d6f04241238c0b

A tiny, minimalist, slightly-esoteric concatenative programming lannguage.

Merge branch 'master' of github.com:h3rald/hex
h3rald h3rald@h3rald.com
Tue, 10 Dec 2024 09:25:11 +0100
commit

55b0654160b7b4f7dca7b3bfa6d6f04241238c0b

parent

153649ed7756d94780deec2282e5ec74414cdc6b

2 files changed, 60 insertions(+), 74 deletions(-)

jump to
M scripts/test.hexscripts/test.hex

@@ -5,6 +5,7 @@ 0x0 "successes" :

0x0 "failures" : () "errors" : () "fails" : +0x0 "result" : (dup puts) "_" :

@@ -159,6 +160,37 @@ (("test" "abc") ("test" "abc") <= 0x1 ==)

;80 ("hello" "" split ("h" "e" "l" "l" "o") ==) + (("3" 0x3 /) (error) try "Symbol '/' requires two integers" ==) + ((0x4 0x0 %) (error) try "Division by zero" ==) + ((() 0x3 %) (error) try "Symbol '%' requires two integers" ==) + ;84 + + ((() 0x3 &) (error) try "Symbol '&' requires two integers" ==) + ((0x2 "" |) (error) try "Symbol '|' requires two integers" ==) + ((() "" ^) (error) try "Symbol '^' requires two integers" ==) + ((() 0x1 >>) (error) try "Symbol '>>' requires two integers" ==) + ;88 + + ((() 0x1 <<) (error) try "Symbol '<<' requires two integers" ==) + (("" ~) (error) try "Symbol '~' requires an integer" ==) + ((0x5 int) (error) try "Symbol 'int' requires a string" ==) + (((0x3) int) (error) try "Symbol 'int' requires a string" ==) + ;92 + + (("5" str) (error) try "Symbol 'str' requires an integer" ==) + ((("aaa") str) (error) try "Symbol 'str' requires an integer" ==) + (("10" dec) (error) try "Symbol 'dec' requires an integer" ==) + ((0x5 hex) (error) try "Symbol 'hex' requires a string representing a decimal integer" ==) + ;96 + + ("0x2" 0x2 == 0x0 ==) + ("0x2" (0x2) !=) + ((0x3 "aaa") ("aaa" 0x5) > 0x0 ==) + (("aaa" 0x5) ("aaa" 0x5 0x4) >= 0x0 ==) + ;100 + + ((test "aaa") (test "aaa" 0x5) < 0x1 ==) + (("aaa" 0x5) ("" "aaa" 0x5) <= 0x0 ==) ) "tests" :

@@ -168,7 +200,6 @@

; --- Run tests tests (test .) each - ; --- Report "\nSuccessful Tests: " print successes dec print "/" print successes failures + dec puts

@@ -178,15 +209,15 @@ (errors len 0x0 >)

( "Errors:" warn errors (warn) each - 0x1 exit + 0x1 "result" : ) when (fails len 0x0 >) ( "Failed Tests: " fails ", " join cat warn - 0x1 exit + 0x1 "result" : ) when -0x0 exit +result exit
M src/hex.csrc/hex.c

@@ -1431,7 +1431,7 @@ return 1;

} return hex_push_integer(ctx, a.data.int_value / b.data.int_value); } - hex_error(ctx, "'/' symbol requires two integers"); + hex_error(ctx, "Symbol '/' requires two integers"); FREE(ctx, a); FREE(ctx, b); return 1;

@@ -1458,10 +1458,11 @@ {

if (b.data.int_value == 0) { hex_error(ctx, "Division by zero"); + return 1; } return hex_push_integer(ctx, a.data.int_value % b.data.int_value); } - hex_error(ctx, "'%%' symbol requires two integers"); + hex_error(ctx, "Symbol '%%' requires two integers"); FREE(ctx, a); FREE(ctx, b); return 1;

@@ -1489,7 +1490,7 @@ if (left.type == HEX_TYPE_INTEGER && right.type == HEX_TYPE_INTEGER)

{ return hex_push_integer(ctx, left.data.int_value & right.data.int_value); } - hex_error(ctx, "'&' symbol requires two integers"); + hex_error(ctx, "Symbol '&' requires two integers"); FREE(ctx, left); FREE(ctx, right); return 1;

@@ -1515,7 +1516,7 @@ if (left.type == HEX_TYPE_INTEGER && right.type == HEX_TYPE_INTEGER)

{ return hex_push_integer(ctx, left.data.int_value | right.data.int_value); } - hex_error(ctx, "'|' symbol requires two integers"); + hex_error(ctx, "Symbol '|' requires two integers"); FREE(ctx, left); FREE(ctx, right); return 1;

@@ -1541,7 +1542,7 @@ if (left.type == HEX_TYPE_INTEGER && right.type == HEX_TYPE_INTEGER)

{ return hex_push_integer(ctx, left.data.int_value ^ right.data.int_value); } - hex_error(ctx, "'^' symbol requires two integers"); + hex_error(ctx, "Symbol '^' requires two integers"); FREE(ctx, left); FREE(ctx, right); return 1;

@@ -1567,7 +1568,7 @@ if (left.type == HEX_TYPE_INTEGER && right.type == HEX_TYPE_INTEGER)

{ return hex_push_integer(ctx, left.data.int_value << right.data.int_value); } - hex_error(ctx, "'<<' symbol requires two integers"); + hex_error(ctx, "Symbol '<<' requires two integers"); FREE(ctx, left); FREE(ctx, right); return 1;

@@ -1593,7 +1594,7 @@ if (left.type == HEX_TYPE_INTEGER && right.type == HEX_TYPE_INTEGER)

{ return hex_push_integer(ctx, left.data.int_value >> right.data.int_value); } - hex_error(ctx, "'>>' symbol requires two integers"); + hex_error(ctx, "Symbol '>>' requires two integers"); FREE(ctx, left); FREE(ctx, right); return 1;

@@ -1612,7 +1613,7 @@ if (item.type == HEX_TYPE_INTEGER)

{ return hex_push_integer(ctx, ~item.data.int_value); } - hex_error(ctx, "'~' symbol requires one integer"); + hex_error(ctx, "Symbol '~' requires an integer"); FREE(ctx, item); return 1; }

@@ -1628,17 +1629,11 @@ {

FREE(ctx, a); return 1; } - if (a.type == HEX_TYPE_QUOTATION) - { - hex_error(ctx, "Cannot convert a quotation to an integer"); - FREE(ctx, a); - return 1; - } if (a.type == HEX_TYPE_STRING) { return hex_push_integer(ctx, strtol(a.data.str_value, NULL, 16)); } - hex_error(ctx, "Unsupported data type: %s", hex_type(a.type)); + hex_error(ctx, "Symbol 'int' requires a string"); FREE(ctx, a); return 1; }

@@ -1652,17 +1647,11 @@ {

FREE(ctx, a); return 1; } - if (a.type == HEX_TYPE_QUOTATION) - { - hex_error(ctx, "Cannot convert a quotation to a string"); - FREE(ctx, a); - return 1; - } if (a.type == HEX_TYPE_INTEGER) { return hex_push_string(ctx, hex_itoa_hex(a.data.int_value)); } - hex_error(ctx, "Unsupported data type: %s", hex_type(a.type)); + hex_error(ctx, "Symbol 'str' requires an integer"); FREE(ctx, a); return 1; }

@@ -1680,7 +1669,7 @@ if (a.type == HEX_TYPE_INTEGER)

{ return hex_push_string(ctx, hex_itoa_dec(a.data.int_value)); } - hex_error(ctx, "An integer is required"); + hex_error(ctx, "Symbol 'dec' requires an integer"); FREE(ctx, a); return 1; }

@@ -1698,7 +1687,7 @@ if (item.type == HEX_TYPE_STRING)

{ return hex_push_integer(ctx, strtol(item.data.str_value, NULL, 10)); } - hex_error(ctx, "'hex' symbol requires a string representing a decimal integer"); + hex_error(ctx, "Symbol 'hex' requires a string representing a decimal integer"); FREE(ctx, item); return 1; }

@@ -1781,8 +1770,8 @@

// Perform element-wise comparison if (it_a->type != it_b->type && !(hex_is_type_symbol(it_a) && hex_is_type_symbol(it_b))) { - hex_error(ctx, "Cannot compare quotations with mismatched types"); - return -1; + // Mismatched types, return false + return 0; } if (it_a->type == HEX_TYPE_INTEGER)

@@ -1813,8 +1802,8 @@ }

} else { - hex_error(ctx, "Unsupported element type in quotation comparison"); - return -1; + // Mismatched types, return false + return 0; } }

@@ -1852,10 +1841,8 @@ if ((a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) || (a.type == HEX_TYPE_STRING && b.type == HEX_TYPE_STRING) || (a.type == HEX_TYPE_QUOTATION && b.type == HEX_TYPE_QUOTATION))

{ return hex_push_integer(ctx, hex_equal(a, b)); } - hex_error(ctx, "'==' symbol requires two integers, two strings, or two quotations"); - FREE(ctx, a); - FREE(ctx, b); - return 1; + // Different types => false + return hex_push_integer(ctx, 0); } int hex_symbol_notequal(hex_context_t *ctx)

@@ -1878,10 +1865,8 @@ if ((a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) || (a.type == HEX_TYPE_STRING && b.type == HEX_TYPE_STRING) || (a.type == HEX_TYPE_QUOTATION && b.type == HEX_TYPE_QUOTATION))

{ return hex_push_integer(ctx, !hex_equal(a, b)); } - hex_error(ctx, "'!=' symbol requires two integers, two strings, or two quotations"); - FREE(ctx, a); - FREE(ctx, b); - return 1; + // Different types => true + return hex_push_integer(ctx, 1); } int hex_symbol_greater(hex_context_t *ctx)

@@ -1902,14 +1887,7 @@ return 1;

} hex_item_t *pa = &a; hex_item_t *pb = &b; - int result = hex_greater(ctx, pa, pb, ">"); - if (result < 0) - { - FREE(ctx, a); - FREE(ctx, b); - return 1; - } - hex_push_integer(ctx, result); + hex_push_integer(ctx, hex_greater(ctx, pa, pb, ">")); return 0; }

@@ -1931,14 +1909,7 @@ return 1;

} hex_item_t *pa = &a; hex_item_t *pb = &b; - int result = hex_greater(ctx, pb, pa, "<"); - if (result < 0) - { - FREE(ctx, a); - FREE(ctx, b); - return 1; - } - hex_push_integer(ctx, result); + hex_push_integer(ctx, hex_greater(ctx, pb, pa, "<")); return 0; }

@@ -1960,15 +1931,7 @@ return 1;

} hex_item_t *pa = &a; hex_item_t *pb = &b; - int result = hex_greater(ctx, pa, pb, ">"); - if (result < 0) - { - FREE(ctx, a); - FREE(ctx, b); - return 1; - } - result = result || hex_equal(a, b); - hex_push_integer(ctx, result); + hex_push_integer(ctx, hex_greater(ctx, pa, pb, ">") || hex_equal(a, b)); return 0; }

@@ -1990,15 +1953,7 @@ return 1;

} hex_item_t *pa = &a; hex_item_t *pb = &b; - int result = hex_greater(ctx, pb, pa, "<"); - if (result < 0) - { - FREE(ctx, a); - FREE(ctx, b); - return 1; - } - result = !result || hex_equal(a, b); - hex_push_integer(ctx, result); + hex_push_integer(ctx, !hex_greater(ctx, pb, pa, "<") || hex_equal(a, b)); return 0; }