diff --git a/implementations/C/KEYWORDS.txt b/implementations/C/KEYWORDS.txt index 50e2cf1..3c581a8 100644 --- a/implementations/C/KEYWORDS.txt +++ b/implementations/C/KEYWORDS.txt @@ -6,18 +6,12 @@ %} struct dict_entry; %% -!=, neq "%", mod *, mul +, add -, sub /, div_joyfunc -<, lt -<=, le -<>, neq -=, eq ->, gt ->=, ge bool, truthy branch, branch clear, clear +cmp, cmp_joyfunc \ No newline at end of file diff --git a/implementations/C/joy.c b/implementations/C/joy.c index 2be9103..93e417e 100644 --- a/implementations/C/joy.c +++ b/implementations/C/joy.c @@ -362,6 +362,39 @@ BINARY_MATH_OP(sub) BINARY_MATH_OP(mul) +/* +With mpz_cmp we can implement the rest of the comparison functions as definitions: + + G E L + eq [false] [true] [false] cmp + gt [true] [false] [false] cmp + lt [false] [false] [true] cmp +neq [true] [false] [true] cmp + le [false] [true] [true] cmp + ge [true] [true] [false] cmp +*/ +void +cmp_joyfunc(JoyListPtr stack, JoyListPtr expression) +{ + int hmm; + mpz_t *a, *b; + JoyList G, E, L; + L = pop_list_node(stack); + E = pop_list_node(stack); + G = pop_list_node(stack); + b = pop_int(stack); + a = pop_int(stack); + hmm = mpz_cmp(*a, *b); + if (hmm > 0) { + push_quote(G, expression); + } else if (hmm < 0) { + push_quote(L, expression); + } else { + push_quote(E, expression); + } +} + + void branch(JoyListPtr stack, JoyListPtr expression) {stack = expression;} void clear(JoyListPtr stack, JoyListPtr expression) {stack = expression;} void div_joyfunc(JoyListPtr stack, JoyListPtr expression) {stack = expression;} diff --git a/implementations/C/joy.h b/implementations/C/joy.h index b0fbf21..42d226b 100644 --- a/implementations/C/joy.h +++ b/implementations/C/joy.h @@ -59,15 +59,10 @@ typedef void (*JoyFunc)(JoyListPtr, JoyListPtr); void add(JoyListPtr stack, JoyListPtr expression); void branch(JoyListPtr stack, JoyListPtr expression); void clear(JoyListPtr stack, JoyListPtr expression); +void cmp_joyfunc(JoyListPtr stack, JoyListPtr expression); void div_joyfunc(JoyListPtr stack, JoyListPtr expression); -void eq(JoyListPtr stack, JoyListPtr expression); -void ge(JoyListPtr stack, JoyListPtr expression); -void gt(JoyListPtr stack, JoyListPtr expression); -void le(JoyListPtr stack, JoyListPtr expression); -void lt(JoyListPtr stack, JoyListPtr expression); void mod(JoyListPtr stack, JoyListPtr expression); void mul(JoyListPtr stack, JoyListPtr expression); -void neq(JoyListPtr stack, JoyListPtr expression); void sub(JoyListPtr stack, JoyListPtr expression); void truthy(JoyListPtr stack, JoyListPtr expression); diff --git a/implementations/C/notes b/implementations/C/notes index 580c61d..f1a809a 100644 --- a/implementations/C/notes +++ b/implementations/C/notes @@ -9,3 +9,30 @@ /*el = text_to_expression(text);*/ /*print_list(el);*/ /*printf("\n");*/ + + concat +cons +dip +dup +first +i +loop +pop +rest +stack +swaack +swap + + +pop_any(), pop_int(), and add + +With cmp (provided by the GMP lib) we can implement the rest of +the comparison functions as definitions: + + G E L + eq [false] [true] [false] cmp + gt [true] [false] [false] cmp + lt [false] [false] [true] cmp +neq [true] [false] [true] cmp + le [false] [true] [true] cmp + ge [true] [true] [false] cmp \ No newline at end of file