changeset 108:145e36e5857d

built-ins: Make no-result for loop work
author Lewin Bormann <lbo@spheniscida.de>
date Thu, 29 Aug 2019 08:51:52 +0200
parents 1b3e8cf29e1e
children 7217a4cb2b97
files src/built-ins.c src/built-ins_test.c src/types.h
diffstat 3 files changed, 10 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/built-ins.c	Thu Aug 29 08:51:32 2019 +0200
+++ b/src/built-ins.c	Thu Aug 29 08:51:52 2019 +0200
@@ -49,7 +49,8 @@
     return exception;
 }
 
-/// Return the ID corresponding to the builtin name in the string.
+/// Return the ID corresponding to the builtin name in the string or -1 if
+/// there is no such built-in.
 static YBUILTIN_TYPE ybuiltin_id(ystr_t* sym) {
     const size_t builtins =
         sizeof(YBUILTIN_ID_MAPPING) / sizeof(struct ybuiltin_id_mapping);
@@ -58,7 +59,7 @@
             return YBUILTIN_ID_MAPPING[i].builtin;
         }
     }
-    return YBUILTIN_UNDEF;
+    return -1;
 }
 
 /// Make a symbolic reference into a built-in reference, if possible.
@@ -66,7 +67,7 @@
     if (expr->typ != YEXPR_REF) return false;
     if (yref_type(&expr->value.ref) != YREF_SYM) return false;
     YBUILTIN_TYPE bi = ybuiltin_id(&expr->value.ref.ref.sym);
-    if (bi == YBUILTIN_UNDEF) return false;
+    if (bi < 0) return false;
     yexpr_destroy(expr);
     yexpr_set_builtin(expr, bi);
     return true;
--- a/src/built-ins_test.c	Thu Aug 29 08:51:32 2019 +0200
+++ b/src/built-ins_test.c	Thu Aug 29 08:51:52 2019 +0200
@@ -88,7 +88,7 @@
 void test_builtin_for_noresult(void) {
     fprintf(stderr, "test_builtin_for_complex ============\n");
 
-    ystr_t input = ystr_new("(for loopvar (1 2 3) (+ loopvar 1 2))"),
+    ystr_t input = ystr_new("(for loopvar (1 2 3) (undef))"),
            error = ystr_new(NULL);
     yexpr_t program = yexpr_new();
     assert(yparse_str(&input, &program, &error));
@@ -105,10 +105,7 @@
 
     yexpr_t result = ybuiltin_run(YBUILTIN_FOR, &state);
     assert(result.typ == YEXPR_LIST);
-
-    // (1 2 3) should each be increased by 3.
-    yexpr_debug(&result);
-    assert(4 == YVEC_AT(&result.value.list, 0, yexpr_t)->value.n);
+    assert(result.value.list.len == 0);
 
     yvec_destroy(&state.call_stack);
     yexpr_destroy(&program);
@@ -216,6 +213,7 @@
     test_builtin_translate();
     test_builtin_for();
     test_builtin_for_complex();
+    test_builtin_for_noresult();
     test_builtin_let();
     test_builtin_let_parsed();
     return 0;
--- a/src/types.h	Thu Aug 29 08:51:32 2019 +0200
+++ b/src/types.h	Thu Aug 29 08:51:52 2019 +0200
@@ -50,6 +50,9 @@
  * NOTE: Don't forget updating YBUILTIN_ENUM_STR when adding types here.
  */
 typedef enum {
+    // Purely internal
+    YBUILTIN_FAIL = -1,
+
     YBUILTIN_UNDEF = 0,
     YBUILTIN_FOR = 1,
     YBUILTIN_LET = 2,