| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- #include <assert.h>
- #include <errno.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #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 sym;
- }
- static struct sym *sym_alloc_ident(struct symtab *st, struct node *n,
- const struct func *func)
- {
- struct sym **sym;
- symtab_foreach(st, sym) {
- if ((*sym)->name && !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);
- }
- }
|