added: import statement

This commit is contained in:
Sven Vogel 2024-06-11 17:59:25 +02:00
parent 41ee308fd4
commit 09871cce76
3 changed files with 39 additions and 6 deletions

View File

@ -316,3 +316,10 @@ AST_NODE_PTR AST_get_node_by_kind(AST_NODE_PTR owner, enum AST_SyntaxElement_t k
return NULL;
}
void AST_merge_modules(AST_NODE_PTR dst, AST_NODE_PTR src) {
for (size_t i = 0; i < src->child_count; i++) {
AST_push_node(dst, AST_remove_child(src, i));
}
AST_delete_node(src);
}

View File

@ -221,4 +221,7 @@ void AST_fprint_graphviz(FILE* stream, const struct AST_Node_t* node);
AST_NODE_PTR AST_get_node_by_kind(AST_NODE_PTR owner, enum AST_SyntaxElement_t kind);
[[gnu::nonnull(1), gnu::nonnull(2)]]
void AST_merge_modules(AST_NODE_PTR dst, AST_NODE_PTR src);
#endif

View File

@ -164,6 +164,29 @@ static void run_backend_codegen(const Module* module, const TargetConfig* target
err = deinit_backend();
}
static AST_NODE_PTR compile_module_with_dependencies(ModuleFileStack *unit, ModuleFile* file) {
AST_NODE_PTR root_module = AST_new_node(empty_location(), AST_Module, NULL);
if (compile_file_to_ast(root_module, file) == EXIT_SUCCESS) {
for (size_t i = 0; i < root_module->child_count; i++) {
AST_NODE_PTR child = AST_get_node(root_module, i);
if (child->kind == AST_Import) {
AST_NODE_PTR imported_module = AST_new_node(empty_location(), AST_Module, NULL);
ModuleFile *imported_file = push_file(unit, child->value);
if (compile_file_to_ast(imported_module, imported_file) == EXIT_SUCCESS) {
AST_merge_modules(root_module, imported_module);
}
}
}
}
return root_module;
}
/**
* @brief Build the given target
* @param unit
@ -172,22 +195,22 @@ static void run_backend_codegen(const Module* module, const TargetConfig* target
static void build_target(ModuleFileStack *unit, const TargetConfig *target) {
print_message(Info, "Building target: %s", target->name);
AST_NODE_PTR ast = AST_new_node(empty_location(), AST_Module, NULL);
ModuleFile *file = push_file(unit, target->root_module);
AST_NODE_PTR ast = compile_module_with_dependencies(unit, file);
if (compile_file_to_ast(ast, file) == EXIT_SUCCESS) {
if (ast != NULL) {
if (setup_target_environment(target) == 0) {
print_ast_to_file(ast, target);
Module* module = create_set(ast);
Module *module = create_set(ast);
if (module != NULL) {
run_backend_codegen(module, target);
}
}
}
AST_delete_node(ast);
AST_delete_node(ast);
}
mem_purge_namespace(MemoryNamespaceLex);
mem_purge_namespace(MemoryNamespaceAst);