-
Notifications
You must be signed in to change notification settings - Fork 0
/
vars.c
75 lines (63 loc) · 1.42 KB
/
vars.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "chibicc.h"
// for struct tags
typedef struct TagScope TagScope;
struct TagScope {
TagScope *next;
char *name;
Type *ty;
};
typedef struct VarScope VarScope;
struct VarScope {
VarScope *next;
Obj *var;
};
typedef struct Scope Scope;
struct Scope {
Scope *next;
VarScope *vars;
TagScope *tags;
};
static Scope *scope = &(Scope){};
Obj *find_var(Token *tok) {
for (Scope *sc = scope; sc; sc = sc -> next)
for (VarScope *v_sc = sc -> vars; v_sc; v_sc = v_sc -> next)
if (equal(tok, v_sc -> var -> name))
return v_sc->var;
return NULL;
}
void enter_scope(void) {
Scope *sc = calloc(1, sizeof(Scope));
sc->next = scope;
scope = sc;
}
static VarScope *push_var_to_scope(Obj *var) {
VarScope *sc = calloc(1, sizeof(VarScope));
sc -> var = var;
sc -> next = scope -> vars;
scope -> vars = sc;
return sc;
}
void leave_scope(void) {
scope = scope->next;
}
Obj *new_var(char *name, Type *ty) {
Obj *var = calloc(1, sizeof(Obj));
var -> name = name;
var -> ty = ty;
push_var_to_scope(var); // push to current scope;
return var;
}
Type *find_tag(Token *tok) {
for (Scope *sc = scope; sc; sc = sc->next)
for (TagScope *tsc = sc->tags; tsc; tsc = tsc->next)
if (equal(tok, tsc->name))
return tsc->ty;
return NULL;
}
void push_tag_scope(Token *tok, Type *ty) {
TagScope *sc = calloc(1, sizeof(TagScope));
sc->name = strndup(tok->loc, tok->len);
sc->ty = ty;
sc->next = scope->tags;
scope->tags = sc;
}