aboutsummaryrefslogtreecommitdiff
path: root/compile.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-09-17 15:17:13 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-09-17 15:17:13 -0400
commitaaa51fc734dde35ab8109bad04e478cdf4fff950 (patch)
tree94b8d8e0d38ae055ff6aa009763c63c1b1b92c48 /compile.c
parent2d5c8c3124dfe82c983bc91b62ed4b69be3fc647 (diff)
Perform topological ordering when compiling typedefs so users don't need
to think about ordering their definitions.
Diffstat (limited to 'compile.c')
-rw-r--r--compile.c15
1 files changed, 12 insertions, 3 deletions
diff --git a/compile.c b/compile.c
index 860c11b9..c0a5eed9 100644
--- a/compile.c
+++ b/compile.c
@@ -3912,6 +3912,16 @@ CORD compile_statement_definitions(env_t *env, ast_t *ast)
}
}
+typedef struct {
+ env_t *env;
+ CORD *header;
+} compile_typedef_info_t;
+
+static void _visit_typedef(compile_typedef_info_t *info, ast_t *ast)
+{
+ *info->header = CORD_all(*info->header, compile_statement_typedefs(info->env, ast));
+}
+
CORD compile_header(env_t *env, ast_t *ast)
{
CORD header = CORD_all(
@@ -3922,14 +3932,13 @@ CORD compile_header(env_t *env, ast_t *ast)
for (ast_list_t *stmt = Match(ast, Block)->statements; stmt; stmt = stmt->next)
header = CORD_all(header, compile_statement_imports(env, stmt->ast));
- for (ast_list_t *stmt = Match(ast, Block)->statements; stmt; stmt = stmt->next)
- header = CORD_all(header, compile_statement_typedefs(env, stmt->ast));
+ compile_typedef_info_t info = {.env=env, .header=&header};
+ visit_topologically(Match(ast, Block)->statements, (Closure_t){.fn=(void*)_visit_typedef, &info});
for (ast_list_t *stmt = Match(ast, Block)->statements; stmt; stmt = stmt->next)
header = CORD_all(header, compile_statement_definitions(env, stmt->ast));
header = CORD_all(header, "void ", env->namespace->name, "$$initialize(void);\n");
-
return header;
}