summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/codegen/codegen.c2
-rw-r--r--src/codegen/codegen_main.c43
2 files changed, 38 insertions, 7 deletions
diff --git a/src/codegen/codegen.c b/src/codegen/codegen.c
index 1d825dc..f4f8914 100644
--- a/src/codegen/codegen.c
+++ b/src/codegen/codegen.c
@@ -981,7 +981,7 @@ void codegen_expression(ParserContext *ctx, ASTNode *node, FILE *out)
else
{
fprintf(out,
- "; if (!_try.is_ok) return %s_Err(_try.err); "
+ "; if (!_try.is_ok) return %s__Err(_try.err); "
"_try.val; })",
search_name);
}
diff --git a/src/codegen/codegen_main.c b/src/codegen/codegen_main.c
index c86b6c9..d5d6bbc 100644
--- a/src/codegen/codegen_main.c
+++ b/src/codegen/codegen_main.c
@@ -336,24 +336,55 @@ void codegen_node(ParserContext *ctx, ASTNode *node, FILE *out)
emit_enum_protos(sorted, out);
emit_trait_defs(kids, out);
+ // First pass: emit ONLY preprocessor directives before struct defs
+ // so that macros like `panic` are available in function bodies
+ ASTNode *raw_iter = kids;
+ while (raw_iter)
+ {
+ if (raw_iter->type == NODE_RAW_STMT && raw_iter->raw_stmt.content)
+ {
+ const char *content = raw_iter->raw_stmt.content;
+ // Skip leading whitespace
+ while (*content == ' ' || *content == '\t' || *content == '\n')
+ {
+ content++;
+ }
+ // Emit only if it's a preprocessor directive
+ if (*content == '#')
+ {
+ fprintf(out, "%s\n", raw_iter->raw_stmt.content);
+ }
+ }
+ raw_iter = raw_iter->next;
+ }
+
if (sorted)
{
emit_struct_defs(ctx, sorted, out);
}
- // Emit type aliases after struct defs (so aliased generic types exist)
- emit_type_aliases(kids, out);
-
- ASTNode *raw_iter = kids;
+ // Second pass: emit non-preprocessor raw statements after struct defs
+ raw_iter = kids;
while (raw_iter)
{
- if (raw_iter->type == NODE_RAW_STMT)
+ if (raw_iter->type == NODE_RAW_STMT && raw_iter->raw_stmt.content)
{
- fprintf(out, "%s\n", raw_iter->raw_stmt.content);
+ const char *content = raw_iter->raw_stmt.content;
+ while (*content == ' ' || *content == '\t' || *content == '\n')
+ {
+ content++;
+ }
+ if (*content != '#')
+ {
+ fprintf(out, "%s\n", raw_iter->raw_stmt.content);
+ }
}
raw_iter = raw_iter->next;
}
+ // Emit type aliases after struct defs (so aliased generic types exist)
+ emit_type_aliases(kids, out);
+
ASTNode *merged_globals = NULL; // Head
if (ctx->parsed_globals_list)