-
Notifications
You must be signed in to change notification settings - Fork 1
/
scope_stack.c
61 lines (43 loc) · 1.1 KB
/
scope_stack.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
#include "nocc.h"
ScopeStack *scope_stack_new(void) {
ScopeStack *s;
s = malloc(sizeof(*s));
s->scopes = vec_new();
scope_stack_push(s);
return s;
}
int scope_stack_depth(ScopeStack *s) {
assert(s != NULL);
return s->scopes->size;
}
void scope_stack_push(ScopeStack *s) {
assert(s != NULL);
vec_push(s->scopes, map_new());
}
void scope_stack_pop(ScopeStack *s) {
assert(s != NULL);
assert(s->scopes->size > 1);
vec_pop(s->scopes);
}
void *scope_stack_find(ScopeStack *s, const char *name, bool recursive) {
void *value;
int i;
assert(s != NULL);
assert(s->scopes->size > 0);
assert(name != NULL);
i = s->scopes->size - 1;
do {
value = map_get(s->scopes->data[i], name);
if (value != NULL) {
return value;
}
} while (--i >= 0 && recursive);
return NULL;
}
void scope_stack_register(ScopeStack *s, const char *name, void *value) {
assert(s != NULL);
assert(s->scopes->size > 0);
assert(name != NULL);
assert(value != NULL);
map_add(vec_back(s->scopes), name, value);
}