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,167 @@
|
||||
#include "spl_namespace.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static char *ns_strdup(const char *s) {
|
||||
if (!s)
|
||||
return NULL;
|
||||
usize n = strlen(s);
|
||||
char *r = malloc(n + 1);
|
||||
if (!r)
|
||||
return NULL;
|
||||
memcpy(r, s, n + 1);
|
||||
return r;
|
||||
}
|
||||
|
||||
static const char *ns_name_or_default(const char *name, spl_ns_id_t id, char *buf, usize cap) {
|
||||
if (name && *name)
|
||||
return name;
|
||||
snprintf(buf, cap, "$ns%zu", id);
|
||||
return buf;
|
||||
}
|
||||
|
||||
void spl_ns_init(spl_namespace_t *ns) {
|
||||
vec_init(ns->nodes);
|
||||
spl_ns_node_t dummy = {0};
|
||||
vec_push(ns->nodes, dummy);
|
||||
ns->root = 0;
|
||||
ns->current = 0;
|
||||
ns->root = spl_ns_create(ns, 0, SPL_NS_ROOT, "pkg", 0, 0);
|
||||
ns->current = ns->root;
|
||||
}
|
||||
|
||||
void spl_ns_drop(spl_namespace_t *ns) {
|
||||
for (usize i = 0; i < vec_size(ns->nodes); ++i) {
|
||||
free((char *)vec_at(ns->nodes, i).name);
|
||||
free((char *)vec_at(ns->nodes, i).qualified);
|
||||
map_free(vec_at(ns->nodes, i).symbols);
|
||||
}
|
||||
vec_free(ns->nodes);
|
||||
ns->root = 0;
|
||||
ns->current = 0;
|
||||
}
|
||||
|
||||
spl_ns_id_t spl_ns_root(spl_namespace_t *ns) {
|
||||
if (!ns)
|
||||
return 0;
|
||||
if (ns->root == 0)
|
||||
ns->root = spl_ns_create(ns, 0, SPL_NS_ROOT, "pkg", 0, 0);
|
||||
return ns->root;
|
||||
}
|
||||
|
||||
spl_ns_id_t spl_ns_create(spl_namespace_t *ns, spl_ns_id_t parent, spl_ns_kind_t kind,
|
||||
const char *name, usize owner_kind, usize owner_id) {
|
||||
if (!ns)
|
||||
return 0;
|
||||
|
||||
spl_ns_id_t id = vec_size(ns->nodes);
|
||||
char name_buf[32];
|
||||
const char *resolved_name = ns_name_or_default(name, id, name_buf, sizeof name_buf);
|
||||
|
||||
const char *parent_qualified = NULL;
|
||||
if (parent && parent < vec_size(ns->nodes))
|
||||
parent_qualified = vec_at(ns->nodes, parent).qualified;
|
||||
|
||||
usize cap = strlen(resolved_name) + (parent_qualified ? strlen(parent_qualified) + 2 : 1);
|
||||
char *qualified = malloc(cap);
|
||||
if (!qualified)
|
||||
return 0;
|
||||
if (parent_qualified && *parent_qualified)
|
||||
snprintf(qualified, cap, "%s.%s", parent_qualified, resolved_name);
|
||||
else
|
||||
snprintf(qualified, cap, "%s", resolved_name);
|
||||
|
||||
spl_ns_node_t node = {0};
|
||||
node.id = id;
|
||||
node.parent = parent;
|
||||
node.kind = kind;
|
||||
node.name = ns_strdup(resolved_name);
|
||||
node.qualified = qualified;
|
||||
node.owner_kind = owner_kind;
|
||||
node.owner_id = owner_id;
|
||||
map_init(node.symbols, MAP_HASH_STR, MAP_CMP_STR);
|
||||
vec_push(ns->nodes, node);
|
||||
|
||||
if (!ns->root)
|
||||
ns->root = id;
|
||||
return id;
|
||||
}
|
||||
|
||||
spl_ns_id_t spl_ns_alloc(spl_namespace_t *ns, spl_ns_kind_t kind, const char *name) {
|
||||
if (!ns)
|
||||
return 0;
|
||||
spl_ns_id_t parent = ns->current ? ns->current : spl_ns_root(ns);
|
||||
return spl_ns_create(ns, parent, kind, name, 0, 0);
|
||||
}
|
||||
|
||||
bool spl_ns_enter(spl_namespace_t *ns, spl_ns_id_t id) {
|
||||
if (!ns || id == 0 || id >= vec_size(ns->nodes))
|
||||
return false;
|
||||
ns->current = id;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool spl_ns_leave(spl_namespace_t *ns) {
|
||||
if (!ns || ns->current == 0)
|
||||
return false;
|
||||
ns->current = vec_at(ns->nodes, ns->current).parent;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool spl_ns_insert(spl_namespace_t *ns, spl_ns_id_t id, spl_symbol_t symbol) {
|
||||
if (!ns || id == 0 || id >= vec_size(ns->nodes) || !symbol.name)
|
||||
return false;
|
||||
spl_ns_node_t *node = &vec_at(ns->nodes, id);
|
||||
spl_symbol_t old = {0};
|
||||
if (map_get(node->symbols, symbol.name, &old))
|
||||
return false;
|
||||
map_put(node->symbols, symbol.name, symbol);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool spl_ns_lookup(spl_namespace_t *ns, const char *name, spl_symbol_t *out) {
|
||||
if (!ns || !name || !out)
|
||||
return false;
|
||||
spl_ns_id_t id = ns->current ? ns->current : ns->root;
|
||||
for (; id != 0 && id < vec_size(ns->nodes); id = vec_at(ns->nodes, id).parent) {
|
||||
if (map_get(vec_at(ns->nodes, id).symbols, name, out))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool spl_ns_lookup_local(spl_namespace_t *ns, spl_ns_id_t id, const char *name,
|
||||
spl_symbol_t *out) {
|
||||
if (!ns || id == 0 || id >= vec_size(ns->nodes) || !name || !out)
|
||||
return false;
|
||||
return map_get(vec_at(ns->nodes, id).symbols, name, out);
|
||||
}
|
||||
|
||||
const char *spl_ns_qualified(spl_namespace_t *ns, spl_ns_id_t id) {
|
||||
if (!ns || id == 0 || id >= vec_size(ns->nodes))
|
||||
return NULL;
|
||||
return vec_at(ns->nodes, id).qualified;
|
||||
}
|
||||
|
||||
bool spl_ns_symbol_qualified(spl_namespace_t *ns, spl_ns_id_t id, const char *local_name,
|
||||
char *out, usize cap) {
|
||||
if (!out || cap == 0)
|
||||
return false;
|
||||
const char *prefix = spl_ns_qualified(ns, id);
|
||||
if (!prefix || !local_name)
|
||||
return false;
|
||||
int n = snprintf(out, cap, "%s.%s", prefix, local_name);
|
||||
return n >= 0 && (usize)n < cap;
|
||||
}
|
||||
|
||||
spl_ns_id_t spl_ns_child(spl_namespace_t *ns, spl_ns_id_t parent, const char *name) {
|
||||
if (!ns || !name)
|
||||
return 0;
|
||||
for (usize i = 1; i < vec_size(ns->nodes); ++i) {
|
||||
spl_ns_node_t *n = &vec_at(ns->nodes, i);
|
||||
if (n->parent == parent && n->name && strcmp(n->name, name) == 0)
|
||||
return n->id;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user