Used Variables instead of Options, in SConstruct
[mcc.git] / symtab.c
blob42d0e00ca544ff11497d47ff4003b328ea2256ac
1 #include "symtab.h"
3 static char *symbol_type_names[] = {
4 "ST_FUNC",
5 "ST_VAR",
6 "ST_LABEL",
9 static char *symbol_scope_names[] = {
10 "SS_LOCAL",
11 "SS_LEXICAL",
12 "SS_GLOBAL",
15 static void symbol_free(struct symbol *sym)
17 free(sym);
20 void symbol_table_create(struct symbol_table *st)
22 st->first = NULL;
25 void symbol_table_free(struct symbol_table *st)
27 struct symbol *sym, *sym_next;
28 sym = st->first;
29 while (sym){
30 sym_next = sym->next;
31 symbol_free(sym);
32 sym = sym_next;
34 st->first = NULL;
37 void symbol_table_dump(struct symbol_table *st, FILE *f)
39 struct symbol *sym;
40 sym = st->first;
41 while (sym){
42 fprintf(f, "%s (%s | %s)\n", lex_get_tok_str(sym->name, NULL),
43 symbol_type_names[st->type],
44 symbol_scope_names[st->scope]);
45 sym = sym->next;
49 struct symbol *symbol_table_add(struct symbol_table *st, tok_t name)
51 struct symbol *sym;
52 sym = emalloc(sizeof *sym);
53 memset(sym, 0, sizeof *sym);
55 // linked list insert
56 sym->prev = NULL;
57 sym->next = st->first;
58 if (st->first)
59 st->first->prev = sym;
60 st->first = sym;
62 sym->name = name;
64 return sym;
67 void symbol_table_remove(struct symbol_table *st, struct symbol *sym)
69 *(sym->prev ? &sym->prev->next : &st->first) = sym->next;
70 *(sym->next ? &sym->next->prev : &st->last) + sym->prev;