-
Notifications
You must be signed in to change notification settings - Fork 1
/
env.c
222 lines (167 loc) · 4.91 KB
/
env.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
#include "env.h"
/* base_env established separately to
persist through repl */
Env* base_env;
/* env builders */
// returns pointer to base_env
Env* makeBaseEnv(void) {
Prim primitives[] = LIST_OF_PRIMITIVES;
Frame* falseFrame = makeFrame(FALSE_NAME, FALSEOBJ);
falseFrame->next = NULL;
Frame* trueFrame = makeFrame(TRUE_NAME, TRUEOBJ);
trueFrame->next = falseFrame;
Frame* next_frame = trueFrame;
Frame* prim_frame;
Prim prim;
Obj val;
char* key;
// cons together primitives back to front
for (int i = PRIM_LEN - 1; i >= 0; i--) {
prim = primitives[i];
val = PRIMOBJ(prim);
key = prim.name;
prim_frame = makeFrame(key, val);
prim_frame->next = next_frame;
next_frame = prim_frame;
}
Env* env = makeEnv(prim_frame, NULL);
return env;
}
// returns new env obj with vars bound to vals
Obj extendEnv(Obj vars_obj, Obj vals_obj, Obj base_env_obj) {
List* vars;
List* vals;
/* switch allows for variable-arity functions,
eg ((lambda s s) 4 5 6) */
switch (GETTAG(vars_obj)) {
case LIST:
vars = GETLIST(vars_obj);
vals = GETLIST(vals_obj);
break;
case NAME:
vars = makeList(vars_obj, NULL);
vals = makeList(vals_obj, NULL);
break;
default:
printf("\n%s\n",
"extendEnv -- Unknown parameter type! "
"Returning env unextended...");
return base_env_obj;
}
Env* base_env = GETENV(base_env_obj);
Frame* frame = zipFrame(vars, vals);
Env* ext_env = makeEnv(frame, base_env);
// memory management
append_to_envs(ext_env);
return ENVOBJ(ext_env);
}
/* lookup */
// returns val bound to var in env
Obj lookup(Obj var_obj, Obj env_obj) {
if (DEBUG) printf("looking up \"%s\"\n", GETNAME(var_obj));
char* var = GETNAME(var_obj);
Env* env = GETENV(env_obj);
lookup_count++;
return lookup_in_env(var, env);
}
// lookup helpers
Obj lookup_in_env(char* var, Env* env) { // lookup in env
if (DEBUG) printf("%s\n", "looking up in env...");
if (env == NULL) {
printf("lookup -- %s isn't there! Returning DUMMY...\n", var);
return DUMMYOBJ;
}
Frame* frame = env->frame;
Obj checkFrame = lookup_in_frame(var, frame);
if (checkFrame.tag != DUMMY)
return checkFrame;
else {
envs_traversed++;
return lookup_in_env(var, env->enclosure);
}
}
Obj lookup_in_frame(char* var, Frame* frame) { // helper for lookup
if (DEBUG) printf("%s\n", "looking up in frame...");
if (frame == NULL) return DUMMYOBJ;
char* key = frame->key;
if (strcmp(var, key) == 0)
return (frame->val);
else {
frames_traversed++;
return lookup_in_frame(var, frame->next);
}
}
/* modify env */
/* adds new var/val binding to env */
void defineVar(Obj var_obj, Obj val_obj, Obj env_obj) {
char* var = GETNAME(var_obj);
Env* env = GETENV(env_obj);
if (DEBUG) printf("\nVAR: %s\n", var);
Frame* frame = env->frame;
char* key = frame->key;
while (frame->next) {
if (DEBUG) printf("%s\n", key);
if (strcmp(var, key) == 0) {
printf("defineVar -- variable %s already defined!\n", var);
return;
} else {
frame = frame->next;
key = frame->key;
}
}
if (DEBUG) printf("%s\n", key);
Frame* newFrame = malloc(sizeof(Frame));
newFrame->key = var;
newFrame->val = val_obj;
newFrame->next = NULL;
frame->next = newFrame;
if (DEBUG) printf("new frame\n");
return;
}
// sets first occurence of var to val
void setVar(Obj var_obj, Obj val_obj, Obj env_obj) {
char* var = GETNAME(var_obj);
Env* env = GETENV(env_obj);
if (env == NULL) {
printf("setVar -- variable %s unbound!\n", var);
return;
}
Frame* frame = env->frame;
while (frame != NULL) {
if (strcmp(var, frame->key) == 0) {
frame->val = val_obj;
return;
}
frame = frame->next;
}
Obj enclosure = ENVOBJ(env->enclosure);
setVar(var_obj, val_obj, enclosure);
}
/* constructors */
Frame* zipFrame(List* vars, List* vals) {
if (vars == NULL) return NULL;
char* key = GETNAME(vars->car);
Obj val = vals->car;
Frame* frame = makeFrame(key, val);
frame->next = zipFrame(vars->cdr, vals->cdr);
return frame;
}
Env* makeEnv(Frame* frame, Env* enclosure) {
Env* env = malloc(sizeof(Env));
env->frame = frame;
env->enclosure = enclosure;
return env;
}
Frame* makeFrame(char* key, Obj val) {
Frame* frame = malloc(sizeof(Frame));
frame->key = key;
frame->val = val;
return frame;
}
// cons-like (declaration in objects.h)
List* makeList(Obj car, List* cdr) {
List* list = malloc(sizeof(List));
list->car = car;
list->cdr = cdr;
return list;
}