From 1092185c843d77b5535d44956c1db1aa484d4b74 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 4 Sep 2024 05:01:21 -0400 Subject: [PATCH] Improve codegen by making test() even more concise --- builtins/functions.c | 2 +- builtins/functions.h | 4 ++-- compile.c | 20 ++++++++++---------- environment.c | 4 ++-- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/builtins/functions.c b/builtins/functions.c index 3fbccf8..18a8d3b 100644 --- a/builtins/functions.c +++ b/builtins/functions.c @@ -206,7 +206,7 @@ public void end_test(void *expr, const TypeInfo *type, const char *expected, con (void)start; (void)end; --TEST_DEPTH; - if (!expr) return; + if (!expr || !type) return; Text_t expr_text = generic_as_text(expr, USE_COLOR, type); Text_t type_name = generic_as_text(NULL, false, type); diff --git a/builtins/functions.h b/builtins/functions.h index d6b4c4b..70c5194 100644 --- a/builtins/functions.h +++ b/builtins/functions.h @@ -18,9 +18,9 @@ void fail_source(const char *filename, int64_t start, int64_t end, const char *f Text_t builtin_last_err(); 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); -#define test(expr, type, expected, start, end) {\ +#define test(expr, typeinfo, expected, 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); uint64_t generic_hash(const void *obj, const TypeInfo *type); diff --git a/compile.c b/compile.c index 9b6aabc..cf7f7e7 100644 --- a/compile.c +++ b/compile.c @@ -356,9 +356,9 @@ CORD compile_statement(env_t *env, ast_t *ast) } return CORD_asprintf( "%r;\n" - "test((%r = %r, &%r), %r, %r, %ld, %ld);\n", + "test((%r = %r), %r, %r, %ld, %ld);\n", compile_declaration(t, var), - var, val_code, var, + var, val_code, compile_type_info(env, get_type(env, decl->value)), CORD_quoted(output), (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); } return CORD_asprintf( - "test((%r, &%r), %r, %r, %ld, %ld);", + "test((%r), %r, %r, %ld, %ld);", compile_assignment(env, assign->targets->ast, value), - compile(env, assign->targets->ast), compile_type_info(env, lhs_t), CORD_quoted(test->output), (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"); CORD code = "test(({ // Assignment\n"; + int64_t i = 1; 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); @@ -416,7 +416,7 @@ CORD compile_statement(env_t *env, ast_t *ast) 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"); - 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)), CORD_quoted(test->output), (int64_t)(test->expr->start - test->expr->file->text), @@ -424,25 +424,25 @@ CORD compile_statement(env_t *env, ast_t *ast) return code; } } else if (test->expr->tag == UpdateAssign) { + type_t *lhs_t = get_type(env, Match(test->expr, UpdateAssign)->lhs); return CORD_asprintf( - "test(({ %r; &%r; }), %r, %r, %ld, %ld);", + "test(({%r %r;}), %r, %r, %ld, %ld);", compile_statement(env, test->expr), 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), (int64_t)(test->expr->start - 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) { return CORD_asprintf( - "test(({ %r; NULL; }), NULL, NULL, %ld, %ld);", + "test(({%r NULL;}), NULL, NULL, %ld, %ld);", compile_statement(env, test->expr), (int64_t)(test->expr->start - test->expr->file->text), (int64_t)(test->expr->end - test->expr->file->text)); } else { return CORD_asprintf( "test(%r, %r, %r, %ld, %ld);", - test->expr->tag == Var ? CORD_all("&", compile(env, test->expr)) - : CORD_all("(", compile_type(expr_t), "[1]){", compile(env, test->expr), "}"), + compile(env, test->expr), compile_type_info(env, expr_t), CORD_quoted(output), (int64_t)(test->expr->start - test->expr->file->text), diff --git a/environment.c b/environment.c index 6477039..eb45603 100644 --- a/environment.c +++ b/environment.c @@ -180,8 +180,8 @@ env_t *new_compilation_unit(CORD *libname) {"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(PI), C(PI_4), C(SQRT2), C(SQRT1_2), - {"INF", "INFINITY", "Num"}, - {"TAU", "(2.*M_PI)", "Num"}, + {"INF", "(Num_t)(INFINITY)", "Num"}, + {"TAU", "(Num_t)(2.*M_PI)", "Num"}, {"random", "Num$random", "func()->Num"}, {"mix", "Num$mix", "func(amount:Num, x:Num, y:Num)->Num"}, {"from_text", "Num$from_text", "func(text:Text, the_rest=!&Text)->Num"},