From 3f10460a6e04f014d3fe7a911f6afa844f975585 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 21 Apr 2024 11:22:11 -0400 Subject: Support loading imports as top-level statements --- ast.c | 1 + compile.c | 32 ++++++++++++++++++++++---------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/ast.c b/ast.c index e841089e..64464dc9 100644 --- a/ast.c +++ b/ast.c @@ -215,6 +215,7 @@ bool is_constant(ast_t *ast) return is_constant(binop->lhs) && is_constant(binop->rhs); } } + case Use: return true; default: return false; } } diff --git a/compile.c b/compile.c index 434fd832..8c3717db 100644 --- a/compile.c +++ b/compile.c @@ -214,16 +214,26 @@ CORD compile_statement(env_t *env, ast_t *ast) if (test->expr->tag == Declare) { auto decl = Match(test->expr, Declare); - return CORD_asprintf( - "%r\n" - "test(&%r, %r, %r, %r, %ld, %ld);", - compile_statement(env, test->expr), - compile(env, decl->var), - compile_type_info(env, get_type(env, decl->value)), - compile(env, WrapAST(test->expr, TextLiteral, .cord=output)), - compile(env, WrapAST(test->expr, TextLiteral, .cord=test->expr->file->filename)), - (int64_t)(test->expr->start - test->expr->file->text), - (int64_t)(test->expr->end - test->expr->file->text)); + if (decl->value->tag == Use) { + assert(compile_statement(env, test->expr) == CORD_EMPTY); + return CORD_asprintf( + "test(NULL, NULL, %r, %r, %ld, %ld);", + compile(env, WrapAST(test->expr, TextLiteral, .cord=output)), + compile(env, WrapAST(test->expr, TextLiteral, .cord=test->expr->file->filename)), + (int64_t)(test->expr->start - test->expr->file->text), + (int64_t)(test->expr->end - test->expr->file->text)); + } else { + return CORD_asprintf( + "%r\n" + "test(&%r, %r, %r, %r, %ld, %ld);", + compile_statement(env, test->expr), + compile(env, decl->var), + compile_type_info(env, get_type(env, decl->value)), + compile(env, WrapAST(test->expr, TextLiteral, .cord=output)), + compile(env, WrapAST(test->expr, TextLiteral, .cord=test->expr->file->filename)), + (int64_t)(test->expr->start - test->expr->file->text), + (int64_t)(test->expr->end - test->expr->file->text)); + } } else if (test->expr->tag == Assign) { auto assign = Match(test->expr, Assign); if (!assign->targets->next && assign->targets->ast->tag == Var) { @@ -2113,6 +2123,8 @@ module_code_t compile_file(ast_t *ast) env->code->staticdefs, "static ", compile_type(env, t), " $", decl_name, " = ", compile(env, decl->value), ";\n"); + } else if (decl->value->tag == Use) { + assert(compile_statement(env, stmt->ast) == CORD_EMPTY); } else { env->code->fndefs = CORD_all( env->code->fndefs, -- cgit v1.2.3