OSDN Git Service

truthy
authorSimon Forman <sforman@hushmail.com>
Sun, 5 Feb 2023 07:45:13 +0000 (23:45 -0800)
committerSimon Forman <sforman@hushmail.com>
Sun, 5 Feb 2023 07:45:13 +0000 (23:45 -0800)
implementations/C/convert_defs.py
implementations/C/joy.c

index 2dd5e7c..fd5f1bf 100644 (file)
@@ -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'))
index fb300f8..2cd0220 100644 (file)
@@ -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;