-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymtab.c
More file actions
52 lines (46 loc) · 981 Bytes
/
symtab.c
File metadata and controls
52 lines (46 loc) · 981 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <stdlib.h>
#include "ast.h"
#include "symtab.h"
SymTab *symtab_new(SymTab *prev) {
SymTab *new;
ALLOC(new, SymTab);
new->prev = prev;
new->first_sym = NULL;
return new;
}
void symtab_free(SymTab *st) {
free(st);
}
Declr *symtab_find_one(SymTab *st, char *name) {
SymTabNode *stn;
stn = st->first_sym;
while(stn) {
if(strcmp(stn->name, name) == 0) return stn->symbol;
stn = stn->next;
}
return NULL;
}
int symtab_add(SymTab *st, Declr *sym) {
char *name;
SymTabNode *stn;
if(sym->tag == DECLR_VAR)
name = sym->u.name;
else
name = sym->u.func.name;
if(symtab_find_one(st, name)) return 0;
ALLOC(stn, SymTabNode);
stn->name = name;
stn->symbol = sym;
stn->next = st->first_sym;
st->first_sym = stn;
return 1;
}
Declr *symtab_find(SymTab *st, char* name) {
Declr *d;
if(!st) return NULL;
if(d = symtab_find_one(st, name))
return d;
else
return symtab_find(st->prev, name);
}