Made comparisons more forgiving in case of different types.
h3rald h3rald@h3rald.com
Mon, 09 Dec 2024 18:11:52 +0100
2 files changed,
13 insertions(+),
47 deletions(-)
M
scripts/test.hex
→
scripts/test.hex
@@ -181,9 +181,9 @@ (("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" ==) + ("0x2" 0x2 == 0x0 ==) + ("0x2" (0x2) !=) + ((0x3 "aaa") ("aaa" 0x5) > 0x0 ==) (("aaa" 0x5) ("aaa" 0x5 0x4) >= 0x0 ==) ;100
M
src/hex.c
→
src/hex.c
@@ -1735,8 +1735,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)@@ -1806,10 +1806,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)@@ -1832,10 +1830,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)@@ -1856,14 +1852,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; }@@ -1885,14 +1874,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; }@@ -1914,15 +1896,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; }@@ -1944,15 +1918,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; }