-
Notifications
You must be signed in to change notification settings - Fork 1
/
scope.c
79 lines (61 loc) · 1.78 KB
/
scope.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
76
77
78
79
#include <stdlib.h>
#include <string.h>
#include "scope.h"
#include "error.h"
#include "debug.h"
/*
* Guess we'll use a linked list of linked lists for now, though that certainly
* isn't the best option and should at some point be replaced by a hash table or
* something.
* TODO
*/
void free_layer(scope_layer *layer);
void free_layer(scope_layer *layer) {
for (scope_entry *entry = layer->head; entry;) {
scope_entry *x = entry;
entry = entry->prev;
free(x->id);
free(x);
}
free(layer);
}
void scope_push_layer(scope *sc) {
scope_layer *new_layer = malloc(sizeof (*new_layer));
if (!new_layer) error(GENERAL_ERROR, "cannot malloc?");
new_layer->prev = *sc;
new_layer->head = 0;
*sc = new_layer;
}
void scope_pop_layer(scope *sc) {
scope new_sc = (**sc).prev;
free_layer(*sc);
*sc = new_sc;
}
scope_entry *scope_add_entry(scope sc, char *id, LLVMValueRef val,
LLVMTypeRef type) {
scope_entry *new_entry = malloc(sizeof (*new_entry));
if (!new_entry) error(GENERAL_ERROR, "cannot malloc?");
debug("adding entry for %s\n", id);
new_entry->id = strdup(id);
new_entry->value = val;
new_entry->type = type;
new_entry->has_filler = 0;
new_entry->filler.id = 0;
new_entry->filler.value = 0;
new_entry->filler.type = 0;
new_entry->filler.constructs = 0;
scope_layer *layer = sc;
new_entry->prev = layer->head;
layer->head = new_entry;
return new_entry;
}
scope_entry *scope_find(scope sc, char *id) {
for (scope_layer *layer = sc; layer; layer = layer->prev) {
for (scope_entry *entry = layer->head; entry; entry = entry->prev) {
if (!strcmp(entry->id, id)) {
return entry;
}
}
}
return 0;
}