changeset 87:8156b5031546

built-ins: Update test to new built-in calling convention
author Lewin Bormann <lbo@spheniscida.de>
date Mon, 26 Aug 2019 12:19:25 +0200
parents 5c761c80c411
children d197a9c11953
files src/built-ins.c src/built-ins_test.c
diffstat 2 files changed, 31 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/built-ins.c	Mon Aug 26 12:03:31 2019 +0200
+++ b/src/built-ins.c	Mon Aug 26 12:19:25 2019 +0200
@@ -51,6 +51,7 @@
     return exception;
 }
 
+
 static YBUILTIN_TYPE ybuiltin_id(ystr_t* sym) {
     const size_t builtins =
         sizeof(YBUILTIN_ID_MAPPING) / sizeof(struct ybuiltin_id_mapping);
@@ -82,6 +83,7 @@
         goto badexprfail;
 
     ref = YVEC_AT(letlist, counter++, yexpr_t);
+    // TODO evaluate all expressions and assign last
     val = YVEC_AT(letlist, counter++, yexpr_t);
     if (ref->typ != YEXPR_REF || val->typ == YEXPR_UNDEF) goto typefail;
 
@@ -98,8 +100,8 @@
 notenoughfail:
     return ybuiltin_type_error(
         YBUILTIN_LET,
-        ystr_new("BUG: not enough arguments on call stack or to builtin"),
-        &let);
+        ystr_new("BUG: not enough arguments on call stack"),
+        NULL);
 
 typefail:
     nothing();
--- a/src/built-ins_test.c	Mon Aug 26 12:03:31 2019 +0200
+++ b/src/built-ins_test.c	Mon Aug 26 12:19:25 2019 +0200
@@ -5,23 +5,36 @@
 
 void test_builtin_let(void) {
     yexpr_t ref_expr = yexpr_new();
+    yexpr_t let_expr = yexpr_new();
     yref_t ref = yref_new();
+    yexpr_t val_expr = yexpr_new();
+    yexpr_t let_call = yexpr_new();
+
+    // (let <ref> 'my-atom)
     yexpr_set_ref(&ref_expr, ref);
-    yexpr_t val_expr = yexpr_new();
+    yexpr_set_builtin(&let_expr, YBUILTIN_LET);
     yexpr_set_atom(&val_expr, yatom_get_or_add("my-atom"));
 
+    yvec_t let_args;
+    YVEC_INIT(&let_args, 4, yexpr_t);
+    YVEC_PUSH(&let_args, &let_expr);
+    YVEC_PUSH(&let_args, &ref_expr);
+    YVEC_PUSH(&let_args, &val_expr);
+
+    yexpr_set_list(&let_call, let_args);
+
     // NOTE: The expressions should be copied, but for this test we can store
     // them directly.
-    yvec_t stack;
-    YVEC_INIT(&stack, 4, yexpr_t);
-    YVEC_PUSH(&stack, &ref_expr);
-    YVEC_PUSH(&stack, &val_expr);
+    yeval_state_t state;
+    yvec_t* stack = &state.call_stack;
+    YVEC_INIT(stack, 4, yexpr_t);
+    YVEC_PUSH(stack, &let_call);
 
-    yexpr_t result = ybuiltin_run(YBUILTIN_LET, &stack);
+    yexpr_t result = ybuiltin_run(YBUILTIN_LET, &state);
     yexpr_debug(&result);
     fputs("\n", stderr);
     assert(result.typ == YEXPR_UNDEF);
-    assert(stack.len == 0);
+    assert(stack->len == 0);
 
     yvalue_t* stored = yvalue_get(ref);
     assert(stored->typ == YVALUE_EXPR);
@@ -29,18 +42,17 @@
     assert(stored->value.expr.value.atom == val_expr.value.atom);
 
     // Type mismatch exception.
-    yexpr_set_number(&ref_expr, 124);
-    YVEC_PUSH(&stack, &ref_expr);
-    YVEC_PUSH(&stack, &val_expr);
-    result = ybuiltin_run(YBUILTIN_LET, &stack);
+    yexpr_set_number(YVEC_AT(&let_call.value.list, 1, yexpr_t), 124);
+    YVEC_PUSH(stack, &let_call);
+    result = ybuiltin_run(YBUILTIN_LET, &state);
     yexpr_debug(&result);
     fputs("\n", stderr);
     assert(result.typ == YEXPR_EXCEPTION);
-    assert(stack.len == 0);
+    assert(stack->len == 0);
     yexpr_destroy(&result);
 
     // Not enough arguments on stack.
-    result = ybuiltin_run(YBUILTIN_LET, &stack);
+    result = ybuiltin_run(YBUILTIN_LET, &state);
     assert(result.typ == YEXPR_EXCEPTION);
     fputs(ystr_str(&result.value.str), stderr);
     assert(0 ==
@@ -51,7 +63,8 @@
     yexpr_destroy(&result);
     yexpr_destroy(&ref_expr);
     yexpr_destroy(&val_expr);
-    yvec_destroy(&stack);
+    yexpr_destroy(&let_call);
+    yvec_destroy(stack);
 };
 
 int main(void) {