aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-04-21 11:22:11 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-04-21 11:22:11 -0400
commit3f10460a6e04f014d3fe7a911f6afa844f975585 (patch)
tree2f2cfe9e82e0b29925acef2b0cf677e5d70ba6ae
parentaa66a38b08fcf26962e50c92b62cfa43b3c053f0 (diff)
Support loading imports as top-level statements
-rw-r--r--ast.c1
-rw-r--r--compile.c32
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,