Improve codegen by making test() even more concise

This commit is contained in:
Bruce Hill 2024-09-04 05:01:21 -04:00
parent 85f1210b1a
commit 1092185c84
4 changed files with 15 additions and 15 deletions

View File

@ -206,7 +206,7 @@ public void end_test(void *expr, const TypeInfo *type, const char *expected, con
(void)start; (void)start;
(void)end; (void)end;
--TEST_DEPTH; --TEST_DEPTH;
if (!expr) return; if (!expr || !type) return;
Text_t expr_text = generic_as_text(expr, USE_COLOR, type); Text_t expr_text = generic_as_text(expr, USE_COLOR, type);
Text_t type_name = generic_as_text(NULL, false, type); Text_t type_name = generic_as_text(NULL, false, type);

View File

@ -18,9 +18,9 @@ void fail_source(const char *filename, int64_t start, int64_t end, const char *f
Text_t builtin_last_err(); Text_t builtin_last_err();
void start_test(const char *filename, int64_t start, int64_t end); void start_test(const char *filename, int64_t start, int64_t end);
void end_test(void *expr, const TypeInfo *type, const char *expected, const char *filename, int64_t start, int64_t end); void end_test(void *expr, const TypeInfo *type, const char *expected, const char *filename, int64_t start, int64_t end);
#define test(expr, type, expected, start, end) {\ #define test(expr, typeinfo, expected, start, end) {\
start_test(__SOURCE_FILE__, start, end); \ start_test(__SOURCE_FILE__, start, end); \
end_test(expr, type, expected, __SOURCE_FILE__, start, end); } end_test((__typeof__(expr)[1]){expr}, typeinfo, expected, __SOURCE_FILE__, start, end); }
void say(Text_t text, bool newline); void say(Text_t text, bool newline);
uint64_t generic_hash(const void *obj, const TypeInfo *type); uint64_t generic_hash(const void *obj, const TypeInfo *type);

View File

@ -356,9 +356,9 @@ CORD compile_statement(env_t *env, ast_t *ast)
} }
return CORD_asprintf( return CORD_asprintf(
"%r;\n" "%r;\n"
"test((%r = %r, &%r), %r, %r, %ld, %ld);\n", "test((%r = %r), %r, %r, %ld, %ld);\n",
compile_declaration(t, var), compile_declaration(t, var),
var, val_code, var, var, val_code,
compile_type_info(env, get_type(env, decl->value)), compile_type_info(env, get_type(env, decl->value)),
CORD_quoted(output), CORD_quoted(output),
(int64_t)(test->expr->start - test->expr->file->text), (int64_t)(test->expr->start - test->expr->file->text),
@ -382,9 +382,8 @@ CORD compile_statement(env_t *env, ast_t *ast)
code_err(assign->values->ast, "You cannot assign a %T value to a %T operand", rhs_t, lhs_t); code_err(assign->values->ast, "You cannot assign a %T value to a %T operand", rhs_t, lhs_t);
} }
return CORD_asprintf( return CORD_asprintf(
"test((%r, &%r), %r, %r, %ld, %ld);", "test((%r), %r, %r, %ld, %ld);",
compile_assignment(env, assign->targets->ast, value), compile_assignment(env, assign->targets->ast, value),
compile(env, assign->targets->ast),
compile_type_info(env, lhs_t), compile_type_info(env, lhs_t),
CORD_quoted(test->output), CORD_quoted(test->output),
(int64_t)(test->expr->start - test->expr->file->text), (int64_t)(test->expr->start - test->expr->file->text),
@ -395,6 +394,7 @@ CORD compile_statement(env_t *env, ast_t *ast)
code_err(ast, "Sorry, but doctesting with '=' is not supported for multi-assignments"); code_err(ast, "Sorry, but doctesting with '=' is not supported for multi-assignments");
CORD code = "test(({ // Assignment\n"; CORD code = "test(({ // Assignment\n";
int64_t i = 1; int64_t i = 1;
for (ast_list_t *target = assign->targets, *value = assign->values; target && value; target = target->next, value = value->next) { for (ast_list_t *target = assign->targets, *value = assign->values; target && value; target = target->next, value = value->next) {
type_t *target_type = get_type(env, target->ast); type_t *target_type = get_type(env, target->ast);
@ -416,7 +416,7 @@ CORD compile_statement(env_t *env, ast_t *ast)
for (ast_list_t *target = assign->targets; target; target = target->next) for (ast_list_t *target = assign->targets; target; target = target->next)
code = CORD_all(code, compile_assignment(env, target->ast, CORD_asprintf("$%ld", i++)), ";\n"); code = CORD_all(code, compile_assignment(env, target->ast, CORD_asprintf("$%ld", i++)), ";\n");
CORD_appendf(&code, "&$1; }), %r, %r, %ld, %ld);", CORD_appendf(&code, "$1; }), %r, %r, %ld, %ld);",
compile_type_info(env, get_type(env, assign->targets->ast)), compile_type_info(env, get_type(env, assign->targets->ast)),
CORD_quoted(test->output), CORD_quoted(test->output),
(int64_t)(test->expr->start - test->expr->file->text), (int64_t)(test->expr->start - test->expr->file->text),
@ -424,25 +424,25 @@ CORD compile_statement(env_t *env, ast_t *ast)
return code; return code;
} }
} else if (test->expr->tag == UpdateAssign) { } else if (test->expr->tag == UpdateAssign) {
type_t *lhs_t = get_type(env, Match(test->expr, UpdateAssign)->lhs);
return CORD_asprintf( return CORD_asprintf(
"test(({ %r; &%r; }), %r, %r, %ld, %ld);", "test(({%r %r;}), %r, %r, %ld, %ld);",
compile_statement(env, test->expr), compile_statement(env, test->expr),
compile_lvalue(env, Match(test->expr, UpdateAssign)->lhs), compile_lvalue(env, Match(test->expr, UpdateAssign)->lhs),
compile_type_info(env, get_type(env, Match(test->expr, UpdateAssign)->lhs)), compile_type_info(env, lhs_t),
CORD_quoted(test->output), CORD_quoted(test->output),
(int64_t)(test->expr->start - test->expr->file->text), (int64_t)(test->expr->start - test->expr->file->text),
(int64_t)(test->expr->end - test->expr->file->text)); (int64_t)(test->expr->end - test->expr->file->text));
} else if (expr_t->tag == VoidType || expr_t->tag == AbortType || expr_t->tag == ReturnType) { } else if (expr_t->tag == VoidType || expr_t->tag == AbortType || expr_t->tag == ReturnType) {
return CORD_asprintf( return CORD_asprintf(
"test(({ %r; NULL; }), NULL, NULL, %ld, %ld);", "test(({%r NULL;}), NULL, NULL, %ld, %ld);",
compile_statement(env, test->expr), compile_statement(env, test->expr),
(int64_t)(test->expr->start - test->expr->file->text), (int64_t)(test->expr->start - test->expr->file->text),
(int64_t)(test->expr->end - test->expr->file->text)); (int64_t)(test->expr->end - test->expr->file->text));
} else { } else {
return CORD_asprintf( return CORD_asprintf(
"test(%r, %r, %r, %ld, %ld);", "test(%r, %r, %r, %ld, %ld);",
test->expr->tag == Var ? CORD_all("&", compile(env, test->expr)) compile(env, test->expr),
: CORD_all("(", compile_type(expr_t), "[1]){", compile(env, test->expr), "}"),
compile_type_info(env, expr_t), compile_type_info(env, expr_t),
CORD_quoted(output), CORD_quoted(output),
(int64_t)(test->expr->start - test->expr->file->text), (int64_t)(test->expr->start - test->expr->file->text),

View File

@ -180,8 +180,8 @@ env_t *new_compilation_unit(CORD *libname)
{"isnan", "Num$isnan", "func(n:Num)->Bool"}, {"isnan", "Num$isnan", "func(n:Num)->Bool"},
C(2_SQRTPI), C(E), C(PI_2), C(2_PI), C(1_PI), C(LN10), C(LN2), C(LOG2E), C(2_SQRTPI), C(E), C(PI_2), C(2_PI), C(1_PI), C(LN10), C(LN2), C(LOG2E),
C(PI), C(PI_4), C(SQRT2), C(SQRT1_2), C(PI), C(PI_4), C(SQRT2), C(SQRT1_2),
{"INF", "INFINITY", "Num"}, {"INF", "(Num_t)(INFINITY)", "Num"},
{"TAU", "(2.*M_PI)", "Num"}, {"TAU", "(Num_t)(2.*M_PI)", "Num"},
{"random", "Num$random", "func()->Num"}, {"random", "Num$random", "func()->Num"},
{"mix", "Num$mix", "func(amount:Num, x:Num, y:Num)->Num"}, {"mix", "Num$mix", "func(amount:Num, x:Num, y:Num)->Num"},
{"from_text", "Num$from_text", "func(text:Text, the_rest=!&Text)->Num"}, {"from_text", "Num$from_text", "func(text:Text, the_rest=!&Text)->Num"},