#include #include #include #include #include #include "node.h" #include "sym.h" #include "type.h" static struct sym *__sym_alloc(struct symtab *st, const char *name, const struct func *func) { struct sym *sym; st->syms = realloc(st->syms, ++st->len * sizeof(*st->syms)); assert(st->syms); st->syms[st->len - 1] = calloc(1, sizeof(struct sym)); sym = st->syms[st->len - 1]; sym->st = st; sym->name = name; sym->func = func; return 0; } static struct sym *sym_alloc_ident(struct symtab *st, struct node *n, const struct func *func) { struct sym **sym; symtab_foreach(st, sym) { if (!strcmp((*sym)->name, n->ident.name)) return *sym; } return __sym_alloc(st, n->ident.name, func); } struct sym *sym_alloc(struct symtab *st, struct node *n, const struct func *func) { switch (n->ntype) { case N_EXPR: case N_NUM: case N_STRING: return __sym_alloc(st, NULL, func); case N_IDENT: return sym_alloc_ident(st, n, func); } assert(0); return NULL; } void sym_dump(struct sym *sym, FILE *fp) { type_dump(sym->type, sym->name, fp); } void symtab_dump(struct symtab *st, FILE *fp) { struct sym **sym; symtab_foreach(st, sym) { if (!(*sym)->name) continue; sym_dump(*sym, fp); fputc('\n', fp); } }