diff options
| author | Zuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian> | 2026-01-17 23:39:15 +0000 |
|---|---|---|
| committer | Zuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian> | 2026-01-17 23:39:15 +0000 |
| commit | efb6cda22ec9ca124c22b40d1b0049c3992bbf32 (patch) | |
| tree | 04f27713d9b5ec620874b5fe1e478d79d73fe59f | |
| parent | c6a56ffa0e16cd3d315e592b32fdb4825a2e9be1 (diff) | |
Fix for #66
| -rw-r--r-- | src/codegen/codegen.c | 2 | ||||
| -rw-r--r-- | src/codegen/codegen_main.c | 43 |
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) |
