ai: 实现mod语法 提供namespace框架
This commit is contained in:
1 parent
e531aab2d3
commit
663adf6cae
11 files changed
+1001
-187
No files matched your search
@@ -0,0 +1,166 @@
|
||||
#include "spl_module.h"
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static const char *path_basename(const char *path) {
|
||||
const char *base = path;
|
||||
for (const char *p = path; *p; ++p)
|
||||
if (*p == '/' || *p == '\\')
|
||||
base = p + 1;
|
||||
return base;
|
||||
}
|
||||
|
||||
bool spl_module_name_from_path(const char *path, char *out, usize cap) {
|
||||
if (!path || !out || cap == 0)
|
||||
return false;
|
||||
|
||||
const char *base = path_basename(path);
|
||||
const char *dot = strrchr(base, '.');
|
||||
usize len = dot && dot != base ? (usize)(dot - base) : strlen(base);
|
||||
if (len == 0)
|
||||
return false;
|
||||
|
||||
usize w = 0;
|
||||
if (isdigit((unsigned char)base[0])) {
|
||||
if (w + 1 >= cap)
|
||||
return false;
|
||||
out[w++] = '_';
|
||||
}
|
||||
for (usize i = 0; i < len && w + 1 < cap; ++i) {
|
||||
unsigned char c = (unsigned char)base[i];
|
||||
out[w++] = (isalnum(c) || c == '_') ? (char)c : '_';
|
||||
}
|
||||
out[w] = '\0';
|
||||
return w > 0;
|
||||
}
|
||||
|
||||
const char *spl_module_declared_name(spl_ast_t *ast) {
|
||||
if (!ast || !ast->root)
|
||||
return NULL;
|
||||
spl_ast_node_t *root = spl_ast_node(ast, ast->root);
|
||||
if (!root || root->kind != SPL_AST_ROOT || !root->root.members)
|
||||
return NULL;
|
||||
spl_ast_node_t *cm = spl_ast_node(ast, root->root.members);
|
||||
if (!cm || cm->kind != SPL_AST_CONTAINER_MEMBERS || !cm->container_members.module)
|
||||
return NULL;
|
||||
spl_ast_node_t *mod = spl_ast_node(ast, cm->container_members.module);
|
||||
if (!mod || mod->kind != SPL_AST_MOD_DECL)
|
||||
return NULL;
|
||||
return mod->mod_decl.path;
|
||||
}
|
||||
|
||||
spl_ast_node_ref_t spl_module_container(spl_module_t *mod) {
|
||||
if (!mod || !mod->ast || !mod->ast->root)
|
||||
return 0;
|
||||
spl_ast_node_t *root = spl_ast_node(mod->ast, mod->ast->root);
|
||||
if (!root || root->kind != SPL_AST_ROOT)
|
||||
return 0;
|
||||
return root->root.members;
|
||||
}
|
||||
|
||||
const char *spl_module_last_segment(const char *name) {
|
||||
if (!name)
|
||||
return NULL;
|
||||
const char *last = name;
|
||||
for (const char *p = name; *p; ++p)
|
||||
if (*p == '.')
|
||||
last = p + 1;
|
||||
return last;
|
||||
}
|
||||
|
||||
void spl_module_registry_init(spl_module_registry_t *reg) {
|
||||
vec_init(reg->modules);
|
||||
map_init(reg->by_name, MAP_HASH_STR, MAP_CMP_STR);
|
||||
map_init(reg->by_path, MAP_HASH_STR, MAP_CMP_STR);
|
||||
}
|
||||
|
||||
void spl_module_registry_drop(spl_module_registry_t *reg) {
|
||||
for (usize i = 0; i < vec_size(reg->modules); ++i) {
|
||||
free(vec_at(reg->modules, i).name);
|
||||
free(vec_at(reg->modules, i).file_path);
|
||||
}
|
||||
vec_free(reg->modules);
|
||||
map_free(reg->by_name);
|
||||
map_free(reg->by_path);
|
||||
}
|
||||
|
||||
spl_module_t *spl_module_get(spl_module_registry_t *reg, spl_module_id_t id) {
|
||||
if (!reg || id == 0 || id > vec_size(reg->modules))
|
||||
return NULL;
|
||||
return &vec_at(reg->modules, id - 1);
|
||||
}
|
||||
|
||||
spl_module_id_t spl_module_find_name(spl_module_registry_t *reg, const char *name) {
|
||||
spl_module_id_t id = 0;
|
||||
if (!reg || !name)
|
||||
return 0;
|
||||
if (map_get(reg->by_name, name, &id))
|
||||
return id;
|
||||
return 0;
|
||||
}
|
||||
|
||||
spl_module_id_t spl_module_find_ast(spl_module_registry_t *reg, spl_ast_t *ast) {
|
||||
if (!reg || !ast)
|
||||
return 0;
|
||||
for (usize i = 0; i < vec_size(reg->modules); ++i)
|
||||
if (vec_at(reg->modules, i).ast == ast)
|
||||
return vec_at(reg->modules, i).id;
|
||||
return 0;
|
||||
}
|
||||
|
||||
spl_module_id_t spl_module_register(spl_module_registry_t *reg, spl_ast_t *ast,
|
||||
const char *file_path, const char *declared_name,
|
||||
spl_diag_t *diag) {
|
||||
if (!reg || !ast)
|
||||
return 0;
|
||||
|
||||
char derived[256];
|
||||
const char *name = declared_name;
|
||||
if (!name || !*name) {
|
||||
if (!file_path || !spl_module_name_from_path(file_path, derived, sizeof derived)) {
|
||||
spl_diag_error(diag, SPL_DIAG_KIND_INTERNAL, NULL, "cannot derive module name");
|
||||
return 0;
|
||||
}
|
||||
name = derived;
|
||||
}
|
||||
|
||||
spl_module_id_t existing_path = 0;
|
||||
if (file_path && map_get(reg->by_path, file_path, &existing_path))
|
||||
return existing_path;
|
||||
|
||||
spl_module_id_t existing_name = 0;
|
||||
if (map_get(reg->by_name, name, &existing_name)) {
|
||||
spl_module_t *old = spl_module_get(reg, existing_name);
|
||||
spl_diag_error(diag, SPL_DIAG_KIND_DUPLICATE_SYMBOL, NULL,
|
||||
"duplicate module `%s` (already loaded from `%s`)", name,
|
||||
old && old->file_path ? old->file_path : "?");
|
||||
return 0;
|
||||
}
|
||||
|
||||
spl_module_t mod = {0};
|
||||
mod.id = vec_size(reg->modules) + 1;
|
||||
mod.name = strdup(name);
|
||||
mod.file_path = file_path ? strdup(file_path) : NULL;
|
||||
mod.ast = ast;
|
||||
mod.state = SPL_MODULE_LOADED;
|
||||
mod.sema_state = 0;
|
||||
|
||||
spl_ast_node_t *root = spl_ast_node(ast, ast->root);
|
||||
if (root && root->kind == SPL_AST_ROOT && root->root.members) {
|
||||
mod.container = root->root.members;
|
||||
spl_ast_node_t *cm = spl_ast_node(ast, mod.container);
|
||||
if (cm && cm->kind == SPL_AST_CONTAINER_MEMBERS && cm->container_members.module) {
|
||||
spl_ast_node_t *header = spl_ast_node(ast, cm->container_members.module);
|
||||
if (header && header->kind == SPL_AST_MOD_DECL)
|
||||
mod.state = SPL_MODULE_LOADED;
|
||||
}
|
||||
}
|
||||
|
||||
vec_push(reg->modules, mod);
|
||||
spl_module_t *stored = &vec_at(reg->modules, vec_size(reg->modules) - 1);
|
||||
map_put(reg->by_name, stored->name, stored->id);
|
||||
if (stored->file_path)
|
||||
map_put(reg->by_path, stored->file_path, stored->id);
|
||||
return stored->id;
|
||||
}
|
||||
Reference in new issue
Block a user