all repos — hex @ 6f3d40e148cf15d5e4b77334c84644a67ca480c4

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

Added more tests.
h3rald h3rald@h3rald.com
Mon, 09 Dec 2024 18:01:01 +0100
commit

6f3d40e148cf15d5e4b77334c84644a67ca480c4

parent

2105a2cb859f84ad5bc6637df571f9fb15d1b6ce

2 files changed, 52 insertions(+), 32 deletions(-)

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

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

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

@@ -157,6 +158,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 ==) (error) try "Symbol '==' requires two integers, two strings, or two quotations" ==) + (("0x2" (0x2) !=) (error) try "Symbol '!=' requires two integers, two strings, or two quotations" ==) + ( ((0x3 "aaa") ("aaa" 0x5) >) (error) try "Cannot compare quotations with mismatched types" ==) + (("aaa" 0x5) ("aaa" 0x5 0x4) >= 0x0 ==) + ;100 + + ((test "aaa") (test "aaa" 0x5) < 0x1 ==) + (("aaa" 0x5) ("" "aaa" 0x5) <= 0x0 ==) ) "tests" :

@@ -166,7 +198,6 @@

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

@@ -176,15 +207,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

@@ -1396,7 +1396,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;

@@ -1423,10 +1423,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;

@@ -1454,7 +1455,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;

@@ -1480,7 +1481,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;

@@ -1506,7 +1507,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;

@@ -1532,7 +1533,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;

@@ -1558,7 +1559,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;

@@ -1577,7 +1578,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; }

@@ -1590,12 +1591,6 @@

POP(ctx, a); if (a.type == HEX_TYPE_INVALID) { - 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; }

@@ -1603,7 +1598,7 @@ 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; }

@@ -1617,17 +1612,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; }

@@ -1645,7 +1634,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; }

@@ -1663,7 +1652,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; }

@@ -1778,8 +1767,8 @@ }

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

@@ -1817,7 +1806,7 @@ 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"); + hex_error(ctx, "Symbol '==' requires two integers, two strings, or two quotations"); FREE(ctx, a); FREE(ctx, b); return 1;

@@ -1843,7 +1832,7 @@ 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"); + hex_error(ctx, "Symbol '!=' requires two integers, two strings, or two quotations"); FREE(ctx, a); FREE(ctx, b); return 1;