-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbol.c
52 lines (41 loc) · 1.02 KB
/
symbol.c
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
52
#include "9cc.h"
#include <stdlib.h>
SYM_TAB *append_sym_tab(char *func_name) {
SYM_TAB *child;
child = malloc(sizeof(SYM_TAB));
child->parent = sym_tab;
child->children = new_map();
child->var_cnt = 0;
child->body = new_map();
// root sym_tab
if (sym_tab != NULL)
map_put(sym_tab->children, func_name, child);
return child;
}
void entry_reserved(char *name, int ty) {
SYM_REC *rec = malloc(sizeof(SYM_REC));
rec->token = ty;
map_put(sym_tab->body, name, rec);
}
SYM_REC *query_reserved(char *name) {
return (SYM_REC *)map_get(sym_tab->body, name);
}
/*
* name - variable name
*/
void entry_var(char *name, Type *ty) {
SYM_REC *rec = malloc(sizeof(SYM_REC));
rec->addr = 8 * (sym_tab->var_cnt + 1);
sym_tab->var_cnt++;
rec->ty = ty;
map_put(sym_tab->body, name, rec);
}
SYM_REC *query_var(char *name) {
SYM_REC *rec = NULL;
for (SYM_TAB *tab = sym_tab; tab != NULL; tab = tab->parent) {
rec = (SYM_REC *)map_get(tab->body, name);
if (rec != NULL)
break;
}
return rec;
}