Move file compilation into a separate file

This commit is contained in:
Bruce Hill 2024-02-18 02:17:44 -05:00
parent 1b5a95c30d
commit 0d3022b34a
3 changed files with 31 additions and 21 deletions

View File

@ -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

View File

@ -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);

View File

@ -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"