-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.c
362 lines (291 loc) · 9.21 KB
/
environment.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include <stdlib.h>
#include <assert.h>
#include "scheme.h"
#include "logger.h"
#include "memory.h"
#include "environment.h"
#define internal static
#define ENVIRONMENT_INITIAL_SIZE 511
object_p global_env;
static int global_env_size = ENVIRONMENT_INITIAL_SIZE;
static int fill_level = 0;
typedef enum {
ENTRY,
VALUE
} env_get_t;
typedef enum {
CREATE,
MODIFY
} env_store_t;
void
init_global_env()
{
scm_debug("Initializing global environment");
global_env_size = ENVIRONMENT_INITIAL_SIZE;
global_env = alloc_global_env(global_env_size);
memset((void*)(&(global_env->env.bindings)), 0, (sizeof(env_binding_t)*global_env_size));
}
internal void
global_env_rehash()
{
scm_debug("Rehash global environment");
int old_size = global_env_size;
int new_size = (old_size + 1) * 2 - 1;
int idx_old_env;
object_p old_env = global_env;
object_p new_env;
new_env = alloc_global_env(new_size);
for (idx_old_env = 0; idx_old_env < old_size; idx_old_env++) {
env_binding_t *old_entry = &(old_env->env.bindings[idx_old_env]);
// hash object for new table and try to insert into next free slot
if (old_entry != NULL) {
int new_hash = (int)(old_entry->car);
int start_idx = new_hash % new_size;
int next_idx = start_idx;
object_p peek;
for (;;) {
peek = new_env->env.bindings[next_idx].car;
if (peek == NULL) {
new_env->env.bindings[next_idx].car = old_entry->car;
new_env->env.bindings[next_idx].cdr = old_entry->cdr;
break;
}
// slot is occupied; try next
next_idx = (next_idx + 1) % new_size;
// looped through table but no free slots found!
assert(next_idx != start_idx && \
"[global_env_rehash]: Fatal, no free slots "
"found in global environment!");
}
}
}
free(old_env);
global_env = new_env;
global_env_size = new_size;
}
internal object_p global_env_get(env_get_t get_type, object_p key);
internal void global_env_store(env_store_t store_type, object_p key, object_p value);
internal object_p local_env_get(env_get_t get_type, object_p lookup_env, object_p key);
internal void local_env_store(env_store_t store_type, object_p lookup_env, object_p key, object_p value);
internal object_p
env_get(env_get_t get_type, object_p env, object_p key)
{
assert(env != NULL && "[env_get]: Passed environment must not be NULL!");
scm_check(IS_ENV(env), "[env_get]: Passed environment is invalid!");
if (IS_GLOBALENV(env))
return global_env_get(get_type, key);
if (IS_LOCALENV(env))
return local_env_get(get_type, env, key);
return NULL;
error:
longjmp(error_occured, 1);
}
internal void
env_store(env_store_t store_type, object_p env, object_p key, object_p value)
{
assert(env != NULL && "[env_store]: Passed environment must not be NULL!");
scm_check(IS_ENV(env), "[env_store]: Passed environment is invalid!");
if (IS_GLOBALENV(env))
return global_env_store(store_type, key, value);
if (IS_LOCALENV(env))
return local_env_store(store_type, env, key, value);
error:
longjmp(error_occured, 1);
}
// Retrieve the value of the global env entry
internal object_p
global_env_get(env_get_t get_type, object_p key)
{
scm_debug("Get from global environment");
int h = (int)key;
int start_idx = h % global_env_size;
int next_idx;
object_p peek;
next_idx = start_idx;
for (;;) {
peek = global_env->env.bindings[next_idx].car;
if (peek == key) {
if (get_type == ENTRY) {
// for whole entry (cons cell) in global env
return (object_p)&(global_env->env.bindings[next_idx]);
}
// only for the value in global env
return global_env->env.bindings[next_idx].cdr;
}
if (peek == NULL) {
if (get_type == ENTRY) {
return nil_object;
}
return NULL;
}
next_idx = (next_idx + 1) % global_env_size;
assert(next_idx != start_idx && \
"[global_env_rehash]: Fatal, no free slots "
"found in global environment!");
}
}
internal void
global_env_store(env_store_t store_type, object_p key, object_p value)
{
scm_debug("Store into global environment");
int h = (int)key;
int start_idx = h % global_env_size;
int next_idx;
object_p peek;
next_idx = start_idx;
for (;;) {
peek = global_env->env.bindings[next_idx].car;
if (peek == key) {
// key is present, replace value
global_env->env.bindings[next_idx].cdr = value;
return;
}
if (peek == NULL) {
scm_check(store_type == CREATE, "[global_env_set]: Key not found");
// CREATE flag: key not present, store key and value
global_env->env.bindings[next_idx].car = key;
global_env->env.bindings[next_idx].cdr = value;
fill_level++;
if (fill_level > (global_env_size * 3/4)) {
global_env_rehash();
}
return;
}
next_idx = (next_idx + 1) % global_env_size;
if (store_type == MODIFY) {
scm_check(next_idx == start_idx, "[global_env_set]: Key not found");
}
assert(next_idx != start_idx && \
"[global_env_rehash]: Fatal, no free slots "
"found in global environment!");
}
return;
error:
longjmp(error_occured, 1);
}
// Retrieve the value of the global env entry
internal object_p
local_env_get(env_get_t get_type, object_p lookup_env, object_p key)
{
scm_debug("Get from local environment");
object_p parent;
object_p binding_key;
int i;
for (i = 0; i < lookup_env->env.length; i++) {
binding_key = lookup_env->env.bindings[i].car;
if (binding_key == key) {
if (get_type == ENTRY) {
// for whole entry (cons cell)
return (object_p)&(lookup_env->env.bindings[i]);
}
// only for the value
return lookup_env->env.bindings[i].cdr;
}
}
// if it is a child env search the parent
if ((parent = lookup_env->env.parent) != NULL)
return env_get(get_type, parent, key);
scm_log_err("[local_env_get]: Key not found");
if (get_type == ENTRY)
return nil_object;
return NULL;
}
internal void
local_env_store(env_store_t store_type, object_p lookup_env, object_p key, object_p value)
{
assert(lookup_env->env.bindings != NULL &&
"[local_env_store]: There must be preallocated bindings");
scm_debug("Store into local environment");
object_p binding_key;
int i;
for (i = 0; i < lookup_env->env.length; i++) {
binding_key = lookup_env->env.bindings[i].car;
assert(binding_key != NULL && "[local_env_store]: Binding must be preallocated!");
if (binding_key == key) {
lookup_env->env.bindings[i].cdr = value;
return;
}
if (store_type == CREATE) {
if (binding_key == nil_object) {
// first free binding
lookup_env->env.bindings[i].car = key;
lookup_env->env.bindings[i].cdr = value;
return;
}
}
}
if (store_type == MODIFY) {
object_p parent;
parent = lookup_env->env.parent;
scm_check(parent != NULL, "[local_env_store]: Cannot look beyond global env for key.");
env_store(store_type, parent, key, value);
return;
}
// fallthrough
scm_log_err("local_env_store %s\n", (store_type == CREATE)
? "(CREATE): No free slot"
: "(MODIFY): Key not found");
error:
longjmp(error_occured, 1);
}
// Convenient API wrappers
object_p
env_get_entry(object_p env, object_p key)
{
return env_get(ENTRY, env, key);
}
object_p
env_get_value(object_p env, object_p key)
{
return env_get(VALUE, env, key);
}
void
env_set(object_p env, object_p key, object_p value)
{
env_store(MODIFY, env, key, value);
}
void
env_put(object_p env, object_p key, object_p value)
{
env_store(CREATE, env, key, value);
}
object_p
global_env_get_entry(object_p key)
{
return global_env_get(ENTRY, key);
}
object_p
global_env_get_value(object_p key)
{
return global_env_get(VALUE, key);
}
void
global_env_set(object_p key, object_p value)
{
global_env_store(MODIFY, key, value);
}
void
global_env_put(object_p key, object_p value)
{
global_env_store(CREATE, key, value);
}
object_p
local_env_get_entry(object_p lookup_env, object_p key)
{
return local_env_get(ENTRY, lookup_env, key);
}
object_p
local_env_get_value(object_p lookup_env, object_p key)
{
return local_env_get(VALUE, lookup_env, key);
}
void
local_env_set(object_p lookup_env, object_p key, object_p value)
{
local_env_store(MODIFY, lookup_env, key, value);
}
void
local_env_put(object_p lookup_env, object_p key, object_p value)
{
local_env_store(CREATE, lookup_env, key, value);
}