-
Notifications
You must be signed in to change notification settings - Fork 0
/
Symbols.c
183 lines (139 loc) · 3.53 KB
/
Symbols.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# include <stdlib.h>
# include <string.h>
# include "Symbols.h"
# ifndef __cplusplus
typedef struct pt_context_node pt_context_node;
typedef struct pt_context_elem pt_context_elem;
# endif /* C++ */
struct pt_context {
pt_context_node *head;
pt_context_node *tail;
};
struct pt_context_node {
pt_context_node *next;
pt_context_node *prev;
pt_context_elem *head;
pt_context_elem *tail;
};
struct pt_context_elem {
long int hash;
pt_value value;
pt_context_elem *next;
};
pt_context *createContext() {
pt_context_node *createContextNode();
pt_context_node *node;
pt_context *context = malloc(sizeof(pt_context));
if(context == NULL) return NULL;
node = createContextNode();
context->head = context->tail = node;
return context;
}
void deleteContext(pt_context *context) {
void deleteContextNode(pt_context_node *);
if(context == NULL) return;
if(context->head != NULL)
deleteContextNode(context->head);
free(context);
}
pt_context_node *createContextNode() {
pt_context_node *node = malloc(sizeof(pt_context_node));
if(node == NULL) return NULL;
node->next = node->prev = NULL;
node->head = node->tail = NULL;
return node;
}
void deleteContextNode(pt_context_node *node) {
void deleteContextElem(pt_context_elem *);
if(node == NULL) return;
if(node->next != NULL) deleteContextNode(node->next);
if(node->head != NULL) deleteContextElem(node->head);
free(node);
}
pt_context_elem *createContextElem(pt_value value) {
pt_context_elem *elem = malloc(sizeof(pt_context_elem));
if(elem == NULL) return NULL;
elem->next = NULL;
memcpy(&elem->value, &value, sizeof(pt_value));
return elem;
}
void deleteContextElem(pt_context_elem *elem) {
if(elem == NULL) return;
if(elem->next != NULL)
deleteContextElem(elem->next);
free(elem);
}
pt_context *pushContext(pt_context *context) {
pt_context_node *node;
if(context == NULL) return NULL;
node = createContextNode();
if(node == NULL) return NULL;
if(context->tail == NULL) {
context->head = context->tail = NULL;
} else {
context->tail->next = node;
context->tail = node;
}
return context;
}
pt_context *popContext(pt_context *context) {
if(context == NULL) return NULL;
if(context->tail->prev != NULL) {
pt_context_node *node = context->tail;
context->tail = node->prev;
context->tail->next = NULL;
deleteContextNode(node);
}
return NULL;
}
static
long int computeHash(char *name) {
int hash = 7;
while(*name)
hash += 17 * *name++;
return hash;
}
static
pt_value *contextElement(pt_context *context, char *name) {
extern pt_value UNDEF;
pt_context_node *node = context->tail;
pt_context_elem *elem = node->head;
int hash = computeHash(name);
while(elem != NULL) {
if(elem->hash == hash) {
return &elem->value;
}
}
elem = createContextElem(UNDEF);
if(elem == NULL) return NULL;
elem->hash = hash;
if(node->tail != NULL) {
node->head = node->tail = elem;
} else {
node->tail->next = elem;
node->tail = elem;
}
return &elem->value;
}
void registerSymbol(pt_context *context, char *name, pt_value value) {
pt_value *value_ref;
if(context == NULL || name == NULL) return;
value_ref = contextElement(context, name);
if(value_ref == NULL) return;
memcpy(value_ref, &value, sizeof(pt_value));
}
pt_value resolveSymbol(pt_context *context, char *name) {
extern pt_value UNDEF;
pt_context_node *node = context->tail;
int hash = computeHash(name);
while(node != NULL) {
pt_context_elem *elem = node->head;
while(elem != NULL) {
if(elem->hash == hash) {
return elem->value;
}
}
node = node->prev;
}
return UNDEF;
}