all repos — hex @ 0d846c62126be30a5fa87fe0a7c3d7fb53819a7d

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

Implemented logical symbols.
h3rald h3rald@h3rald.com
Mon, 18 Nov 2024 10:39:07 +0100
commit

0d846c62126be30a5fa87fe0a7c3d7fb53819a7d

parent

5f13c932fa88647644e350b365b5f08cd0b8950e

1 files changed, 80 insertions(+), 0 deletions(-)

jump to
M hex.chex.c

@@ -988,6 +988,82 @@ hex_free_element(b);

return result; } +// Logical symbols + +int hex_symbol_and() +{ + HEX_StackElement b = hex_pop(); + HEX_StackElement a = hex_pop(); + int result = 0; + if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) + { + result = hex_push_int(a.data.intValue && b.data.intValue); + } + else + { + hex_error("'and' symbol requires two integers"); + result = 1; + } + hex_free_element(a); + hex_free_element(b); + return result; +} + +int hex_symbol_or() +{ + HEX_StackElement b = hex_pop(); + HEX_StackElement a = hex_pop(); + int result = 0; + if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) + { + result = hex_push_int(a.data.intValue || b.data.intValue); + } + else + { + hex_error("'or' symbol requires two integers"); + result = 1; + } + hex_free_element(a); + hex_free_element(b); + return result; +} + +int hex_symbol_not() +{ + HEX_StackElement a = hex_pop(); + int result = 0; + if (a.type == HEX_TYPE_INTEGER) + { + result = hex_push_int(!a.data.intValue); + } + else + { + hex_error("'not' symbol requires an integer"); + result = 1; + } + hex_free_element(a); + return result; +} + +int hex_symbol_xor() +{ + HEX_StackElement b = hex_pop(); + HEX_StackElement a = hex_pop(); + int result = 0; + if (a.type == HEX_TYPE_INTEGER && b.type == HEX_TYPE_INTEGER) + { + result = hex_push_int(a.data.intValue ^ b.data.intValue); + } + else + { + hex_error("'xor' symbol requires two integers"); + result = 1; + } + hex_free_element(a); + hex_free_element(b); + return result; +} + //////////////////////////////////////// // Native Symbol Registration // ////////////////////////////////////////

@@ -1017,6 +1093,10 @@ hex_register_symbol(">", hex_symbol_greater);

hex_register_symbol("<", hex_symbol_less); hex_register_symbol(">=", hex_symbol_greaterequal); hex_register_symbol("<=", hex_symbol_lessequal); + hex_register_symbol("and", hex_symbol_and); + hex_register_symbol("or", hex_symbol_or); + hex_register_symbol("not", hex_symbol_not); + hex_register_symbol("xor", hex_symbol_xor); } ////////////////////////////////////////