aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-02-13 13:32:08 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-02-13 13:32:08 -0500
commit5de71394a3630f1b7c1a42a11d93866f5ea4eacb (patch)
tree0a990b058663caebd86c6fa8ab758c6740173099
parent45d646be10af585b2d784b7f95f20b18e5cc59d1 (diff)
Doctests for assignment
-rw-r--r--compile.c24
-rw-r--r--nextlang.h4
2 files changed, 26 insertions, 2 deletions
diff --git a/compile.c b/compile.c
index 87a4a764..c3ae3697 100644
--- a/compile.c
+++ b/compile.c
@@ -326,7 +326,29 @@ CORD compile(ast_t *ast)
compile(decl->var),
compile(WrapAST(test->expr, StringLiteral, .cord=test->output)));
} else if (test->expr->tag == Assign) {
- errx(1, "Not implemented");
+ auto assign = Match(test->expr, Assign);
+ CORD code = "{ // Assignment\n";
+ int64_t i = 1;
+ for (ast_list_t *value = assign->values; value; value = value->next)
+ CORD_appendf(&code, "__declare(_%ld, %r);\n", i++, compile(value->ast));
+ i = 1;
+ for (ast_list_t *target = assign->targets; target; target = target->next)
+ CORD_appendf(&code, "%r = _%ld;\n", compile(target->ast), i++);
+
+ CORD expr_cord = "CORD_asprintf(\"";
+ for (ast_list_t *target = assign->targets; target; target = target->next)
+ expr_cord = CORD_cat(expr_cord, target->next ? "%r, " : "%r");
+ expr_cord = CORD_cat(expr_cord, "\"");
+ i = 1;
+ for (ast_list_t *target = assign->targets; target; target = target->next)
+ CORD_appendf(&expr_cord, ", __cord(_%ld)", i++);
+ expr_cord = CORD_cat(expr_cord, ")");
+
+ CORD_appendf(&code, "__test(%r, %r, %r);",
+ compile(WrapAST(test->expr, StringLiteral, .cord=src)),
+ expr_cord,
+ compile(WrapAST(test->expr, StringLiteral, .cord=test->output)));
+ return CORD_cat(code, "\n}");
} else {
return CORD_asprintf(
"__test(%r, %r, %r);\n",
diff --git a/nextlang.h b/nextlang.h
index bc252bb2..83dc7996 100644
--- a/nextlang.h
+++ b/nextlang.h
@@ -3,6 +3,7 @@
#include <err.h>
#include <gc.h>
#include <gc/cord.h>
+#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
@@ -82,7 +83,8 @@ CORD as_cord(void *x, bool use_color, const char *fmt, ...);
CORD __result = __cord(expr); \
say(CORD_catn(5, USE_COLOR ? "\x1b[33;1m>>\x1b[0m " : ">> ", src, USE_COLOR ? "\n\x1b[0;2m=\x1b[m " : "\n= ", __result, "\x1b[m")); \
if (expected && CORD_cmp(__result, expected)) { \
- errx(1, "I expected:\n%s but got:\n%s", CORD_to_const_char_star(expected), CORD_to_const_char_star(__result)); \
+ fprintf(stderr, USE_COLOR ? "\x1b[31;1;7mTEST FAILURE!\x1b[27m\nI expected:\n\t\x1b[0;1m%s\x1b[1;31m\nbut got:\n\t%s\x1b[m\n" : "TEST FAILURE!\nI expected:\n\t%s\nbut got:\n\t%s\n", CORD_to_const_char_star(expected), CORD_to_const_char_star(__result)); \
+ raise(SIGABRT); \
} \
} while (0)