ai: 实现mod语法 提供namespace框架
This commit is contained in:
1 parent
e531aab2d3
commit
663adf6cae
11 files changed
+1001
-187
No files matched your search
+24
-4
@@ -21,6 +21,7 @@
|
||||
#include "spl_ir2c.h"
|
||||
#include "spl_ir2vm.h"
|
||||
#include "spl_lexer.h"
|
||||
#include "spl_module.h"
|
||||
#include "spl_parser.h"
|
||||
#include "spl_sema.h"
|
||||
#include "spl_tok.h"
|
||||
@@ -49,10 +50,17 @@ typedef struct {
|
||||
|
||||
spl_driver_module_vec_t modules;
|
||||
MAP(const char *, usize) path_to_idx;
|
||||
spl_module_registry_t module_registry;
|
||||
|
||||
spl_diag_t *diag;
|
||||
} spl_driver_t;
|
||||
|
||||
static spl_module_id_t driver_register_module(spl_driver_t *d, spl_ast_t *ast, const char *path,
|
||||
spl_diag_t *diag) {
|
||||
return spl_module_register(&d->module_registry, ast, path, spl_module_declared_name(ast),
|
||||
diag);
|
||||
}
|
||||
|
||||
static char *read_file(const char *path, long *out_len) {
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) {
|
||||
@@ -219,6 +227,8 @@ static spl_ast_t *driver_load(void *user, const char *from_fname, const char *pa
|
||||
|
||||
vec_at(d->modules, idx).ast = ast;
|
||||
vec_at(d->modules, idx).src = src;
|
||||
if (!driver_register_module(d, ast, key, diag))
|
||||
return NULL;
|
||||
return ast;
|
||||
}
|
||||
|
||||
@@ -236,6 +246,7 @@ static int compile_spl(const char *src, const char *fname, const char *outpath,
|
||||
spl_ast_init(&drv.main_ast, &drv.main_toks);
|
||||
map_init(drv.path_to_idx, MAP_HASH_STR, MAP_CMP_STR);
|
||||
vec_init(drv.modules);
|
||||
spl_module_registry_init(&drv.module_registry);
|
||||
|
||||
int rc = 0;
|
||||
|
||||
@@ -245,6 +256,7 @@ static int compile_spl(const char *src, const char *fname, const char *outpath,
|
||||
dump_tokens(drv.main_toks);
|
||||
free(drv.main_path);
|
||||
map_free(drv.path_to_idx);
|
||||
spl_module_registry_drop(&drv.module_registry);
|
||||
spl_diag_drop(&diag);
|
||||
return 0;
|
||||
}
|
||||
@@ -253,7 +265,7 @@ static int compile_spl(const char *src, const char *fname, const char *outpath,
|
||||
spl_lang_type_t lang_type;
|
||||
spl_layout_target_t layout_target;
|
||||
spl_layout_t layout;
|
||||
spl_scope_t scope;
|
||||
spl_namespace_t ns;
|
||||
spl_sema_t sema;
|
||||
spl_ast2ir_t a2ir;
|
||||
spl_ir_builder_t ir_builder;
|
||||
@@ -279,6 +291,12 @@ static int compile_spl(const char *src, const char *fname, const char *outpath,
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (!driver_register_module(&drv, &drv.main_ast, drv.main_path, &diag)) {
|
||||
spl_diag_summary(&diag);
|
||||
rc = 1;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
spl_attr_init(&attr);
|
||||
attr_ok = 1;
|
||||
spl_lang_type_init(&lang_type);
|
||||
@@ -292,10 +310,11 @@ static int compile_spl(const char *src, const char *fname, const char *outpath,
|
||||
spl_layout_init(&layout, &layout_target);
|
||||
layout_ok = 1;
|
||||
|
||||
spl_scope_init(&scope);
|
||||
spl_ns_init(&ns);
|
||||
scope_ok = 1;
|
||||
|
||||
spl_sema_init(&sema, &drv.main_ast, &lang_type, &layout, &scope, &attr);
|
||||
spl_sema_init(&sema, &drv.main_ast, &lang_type, &layout, &ns, &attr);
|
||||
sema.modules = &drv.module_registry;
|
||||
sema_ok = 1;
|
||||
spl_module_loader_t loader = {.load = driver_load, .user = &drv};
|
||||
sema.loader = &loader;
|
||||
@@ -397,7 +416,7 @@ cleanup:
|
||||
if (sema_ok)
|
||||
spl_sema_drop(&sema);
|
||||
if (scope_ok)
|
||||
spl_scope_drop(&scope);
|
||||
spl_ns_drop(&ns);
|
||||
if (layout_ok)
|
||||
spl_layout_drop(&layout);
|
||||
if (lang_type_ok)
|
||||
@@ -418,6 +437,7 @@ cleanup:
|
||||
vec_free(drv.modules);
|
||||
/* map 的 key 是 module 里 strdup 出的 abs_path,已随 module 释放 */
|
||||
map_free(drv.path_to_idx);
|
||||
spl_module_registry_drop(&drv.module_registry);
|
||||
free(drv.main_path);
|
||||
spl_diag_drop(&diag);
|
||||
return rc;
|
||||
|
||||
Reference in new issue
Block a user