Doctests for assignment

This commit is contained in:
Bruce Hill 2024-02-13 13:32:08 -05:00
parent 45d646be10
commit 5de71394a3
2 changed files with 26 additions and 2 deletions

View File

@ -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",

View File

@ -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)