From b1a06979c29e659e26befaa46fe7cc8b025ea36b Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Sat, 4 Feb 2023 23:45:13 -0800 Subject: [PATCH] truthy --- implementations/C/convert_defs.py | 15 +++++++++++++++ implementations/C/joy.c | 37 +++++++++++++++++++++++++------------ 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/implementations/C/convert_defs.py b/implementations/C/convert_defs.py index 2dd5e7c..fd5f1bf 100644 --- a/implementations/C/convert_defs.py +++ b/implementations/C/convert_defs.py @@ -1,3 +1,18 @@ +''' +It's cheap, but it works. + +Doesn't handle non-alnum names. + +Because the strings are parsed at start time, rather than compile time, +it's basically the same as implementing an inscribe command +and using it to write a simple Joy script to load the defs: + + for line in defs: + print(f'[{line}] inscribe') + +Eh? + +''' import sys #list(open('../defs.txt')) diff --git a/implementations/C/joy.c b/implementations/C/joy.c index fb300f8..2cd0220 100644 --- a/implementations/C/joy.c +++ b/implementations/C/joy.c @@ -583,26 +583,39 @@ clear(JoyListPtr stack, __attribute__((unused)) JoyListPtr expression) void -truthy(JoyListPtr stack, JoyListPtr expression) +truthy(JoyListPtr stack, __attribute__((unused)) JoyListPtr expression) { - stack = expression; -} -/* - JoyListPtr s = stack; + /* + Keep the original stack in case the top item is already a Boolean value. + */ + JoyList s = *stack; JoyList node = pop_any(stack); switch (node->head->kind) { case joyTrue: - stack = s; + *stack = s; + break; case joyFalse: - stack = s; + *stack = s; + break; case joyInt: - push_thing( - if (node->head->value.i); + if mpz_cmp_si(node->head->value.i, 0) { + push_thing(JoyTrue, stack); + } else { + push_thing(JoyFalse, stack); + } + break; + case joyList: + if (node->head->value.el) { + push_thing(JoyTrue, stack); + } else { + push_thing(JoyFalse, stack); + } + break; default: - - stack = expression; + printf("Cannot Boolify.\n"); + exit(1); + } } -*/ JoyList def_abs_body; -- 2.11.0