diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-02-18 02:17:44 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-02-18 02:17:44 -0500 |
| commit | 0d3022b34a79e3b2cecd6136e3c90f69881f1016 (patch) | |
| tree | 6fdfbe9cd02fccdfc21910ab25be95273f9b89b4 | |
| parent | 1b5a95c30d9e0698e401eadc57448cc875ba459b (diff) | |
Move file compilation into a separate file
| -rw-r--r-- | compile.c | 28 | ||||
| -rw-r--r-- | compile.h | 1 | ||||
| -rw-r--r-- | nextlang.c | 23 |
3 files changed, 31 insertions, 21 deletions
@@ -685,4 +685,32 @@ CORD compile_type_info(env_t *env, type_t *t) } } +CORD compile_file(ast_t *ast) +{ + env_t *env = new_compilation_unit(); + CORD_appendf(&env->code->imports, "#include \"nextlang.h\"\n"); + + for (ast_list_t *stmt = Match(ast, Block)->statements; stmt; stmt = stmt->next) { + CORD code = compile_statement(env, stmt->ast); + if (code) + CORD_appendf(&env->code->main, "%r\n", code); + bind_statement(env, stmt->ast); + } + + return CORD_all( + // CORD_asprintf("#line 0 %r\n", Str__quoted(f->filename, false)), + "// Generated code:\n", + env->code->imports, "\n", + env->code->typedefs, "\n", + env->code->typecode, "\n", + env->code->staticdefs, "\n", + env->code->funcs, "\n", + env->code->typeinfos, "\n", + "\n" + "static void $load(void) {\n", + env->code->main, + "}\n" + ); +} + // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 @@ -7,6 +7,7 @@ #include "util.h" #include "environment.h" +CORD compile_file(ast_t *ast); CORD compile_type_ast(env_t *env, type_ast_t *t); CORD compile(env_t *env, ast_t *ast); CORD compile_statement(env_t *env, ast_t *ast); @@ -44,31 +44,12 @@ int main(int argc, char *argv[]) fclose(out); } - env_t *env = new_compilation_unit(); - - CORD_appendf(&env->code->imports, "#include \"nextlang.h\"\n"); - - // Main body: - for (ast_list_t *stmt = Match(ast, Block)->statements; stmt; stmt = stmt->next) { - CORD code = compile_statement(env, stmt->ast); - if (code) - CORD_appendf(&env->code->main, "%r\n", code); - bind_statement(env, stmt->ast); - } + CORD module = compile_file(ast); CORD program = CORD_all( // CORD_asprintf("#line 0 %r\n", Str__quoted(f->filename, false)), "// Generated code:\n", - env->code->imports, "\n", - env->code->typedefs, "\n", - env->code->typecode, "\n", - env->code->staticdefs, "\n", - env->code->funcs, "\n", - env->code->typeinfos, "\n", - "\n" - "static void $load(void) {\n", - env->code->main, - "}\n" + module, "\n" "int main(int argc, const char *argv[]) {\n" "(void)argc;\n" |
