aboutsummaryrefslogtreecommitdiff
path: root/compile.c
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 /compile.c
parent45d646be10af585b2d784b7f95f20b18e5cc59d1 (diff)
Doctests for assignment
Diffstat (limited to 'compile.c')
-rw-r--r--compile.c24
1 files changed, 23 insertions, 1 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",