Commit 79fbbebb
79fbbebb33ceMerged
Ryan Seys <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
feat(block): synthetic-AST node construction API (foundation)
Details
- Commit
- 79fbbebb33ce
- Parent
- e25d8e3e2050
src/node_table.cmodifiedView file
@@ -44,6 +44,8 @@
4444
4545
/* ---- field append helpers ---- */4646
47+
static const SpNode *node_at(const NodeTable *nt, int id);48+
4749
static void ensure_cap(void **buf, int *cap, int need, size_t elem) {4850
if (need <= *cap) return;4951
int nc = *cap ? *cap * 2 : 4;@@ -219,6 +221,7 @@
219221
}220222
221223
nt->count = max_id + 1;224+
nt->node_cap = nt->count;222225
nt->nodes = calloc((size_t)nt->count, sizeof(SpNode));223226
if (!nt->nodes) { free(nt); return NULL; }224227
@@ -346,10 +349,70 @@
346349
}347350
int newroot = map[root];348351
nt->count = base + sn;352+
nt->node_cap = nt->count; /* realloc above sized the array exactly */349353
free(list); free(map);350354
return newroot;351355
}352356
357+
/* ---- synthetic node construction ---- */358+
359+
int nt_new_node(NodeTable *nt, const char *type) {360+
if (nt->count >= nt->node_cap) {361+
int nc = nt->node_cap ? nt->node_cap * 2 : 8;362+
if (nc <= nt->count) nc = nt->count + 1;363+
SpNode *grown = realloc(nt->nodes, sizeof(SpNode) * (size_t)nc);364+
if (!grown) return -1;365+
nt->nodes = grown;366+
nt->node_cap = nc;367+
}368+
int id = nt->count++;369+
SpNode *nd = &nt->nodes[id];370+
memset(nd, 0, sizeof(SpNode));371+
if (type) node_set_type(nd, type, strlen(type));372+
return id;373+
}374+
375+
void nt_node_set_str(NodeTable *nt, int id, const char *key, const char *val) {376+
SpNode *nd = (SpNode *)node_at(nt, id);377+
if (!nd) return;378+
size_t klen = strlen(key), vlen = strlen(val);379+
for (int j = 0; j < nd->ns; j++)380+
if (strcmp(nd->s[j].key, key) == 0) {381+
free(nd->s[j].val); nd->s[j].val = dup_n(val, vlen); nd->s[j].val_len = vlen; return;382+
}383+
node_add_str(nd, key, klen, dup_n(val, vlen), vlen);384+
}385+
386+
void nt_node_set_int(NodeTable *nt, int id, const char *key, long long val) {387+
SpNode *nd = (SpNode *)node_at(nt, id);388+
if (!nd) return;389+
for (int j = 0; j < nd->ni; j++)390+
if (strcmp(nd->i[j].key, key) == 0) { nd->i[j].val = val; return; }391+
node_add_int(nd, key, strlen(key), val);392+
}393+
394+
void nt_node_set_ref(NodeTable *nt, int id, const char *key, int child) {395+
SpNode *nd = (SpNode *)node_at(nt, id);396+
if (!nd) return;397+
for (int j = 0; j < nd->nr; j++)398+
if (strcmp(nd->r[j].key, key) == 0) { nd->r[j].ref = child; return; }399+
node_add_ref(nd, key, strlen(key), child);400+
}401+
402+
void nt_node_set_arr(NodeTable *nt, int id, const char *key, const int *ids, int n) {403+
SpNode *nd = (SpNode *)node_at(nt, id);404+
if (!nd) return;405+
int *copy = NULL;406+
if (n > 0) {407+
copy = malloc(sizeof(int) * (size_t)n);408+
if (copy) memcpy(copy, ids, sizeof(int) * (size_t)n);409+
else n = 0; /* OOM: store an empty array rather than a NULL/count mismatch */410+
}411+
for (int j = 0; j < nd->na; j++)412+
if (strcmp(nd->a[j].key, key) == 0) { free(nd->a[j].ids); nd->a[j].ids = copy; nd->a[j].n = n; return; }413+
node_add_arr(nd, key, strlen(key), copy, n);414+
}415+
353416
void nt_free(NodeTable *nt) {354417
if (!nt) return;355418
for (int k = 0; k < nt->count; k++) {src/node_table.hmodifiedView file
Loading diff…