-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.cpp
More file actions
322 lines (265 loc) · 5.27 KB
/
eval.cpp
File metadata and controls
322 lines (265 loc) · 5.27 KB
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
//
// eval() and friends
//
#include "doctest.h"
#include "consVM.h"
static void assoc(Cell* x, Cell* e);
static void assoc();
static void evcon(Cell* c, Cell* e);
static void evcon();
static void pairlis(Cell* v, Cell* a, Cell* e);
static void evlis(Cell* a, Cell* b);
static void evlis();
static void apply(Cell* fn, Cell* x, Cell* a);
static void apply();
static void audit_env(Cell* a);
static void unpack(Cell*& arg1, Cell*& arg2)
{
arg1 = down(1);
arg2 = down(0);
}
static void unpack(Cell*& arg1, Cell*& arg2, Cell*& arg3)
{
arg1 = down(2);
arg2 = down(1);
arg3 = down(0);
}
void eval(Cell* e, Cell* a)
{
push(e);
push(a);
eval();
}
void eval() // eval(e, a)
{
Cell* e;
Cell* a;
unpack(e, a);
trace("eval", e, a);
if (is_atom(e)) {
assoc();
return;
} else if (is_atom(car(e))) {
if (car(e) == a_quote) {
push(car(cdr(e)));
} else if (car(e) == a_cond) {
evcon(cdr(e), a);
} else {
// apply(car(e), evlis(cdr(e), a), a);
push(car(e));
evlis(cdr(e), a);
push(a);
apply();
}
} else {
// apply(car(e), evlis(cdr(e), a), a);
push(car(e));
evlis(cdr(e), a);
push(a);
apply();
}
collapse(2);
}
static void apply(Cell* fn, Cell* x, Cell* a)
{
push(fn);
push(x);
push(a);
apply();
}
static void apply()
{
Cell* fn; Cell* x; Cell* a;
unpack(fn, x, a);
trace("apply", fn, (is_atom(x) ? x : car(x)));
if (is_atom(fn)) {
if (fn == a_car) {
push(car(car(x)));
} else if (fn == a_cdr) {
push(cdr(car(x)));
} else if (fn == a_atom) {
push(is_atom(car(x)) ? a_t : nil);
} else if (fn == a_cons) {
cons(car(x), car(cdr(x)));
} else if (fn == a_eq) {
push(car(x) == car(cdr(x)) ? a_t : nil);
} else {
eval(fn, a);
apply(pop(), x, a);
}
} else if (car(fn) == a_lambda) {
push(car(cdr(cdr(fn))));
pairlis(car(cdr(fn)), x, a);
eval();
} else {
throw LispError("apply: Not a function");
}
collapse(3);
}
static void assoc(Cell* a, Cell* e)
{
push(a);
push(e);
assoc();
}
static void assoc()
{
Cell* a; Cell* e;
unpack(a, e);
trace("assoc", a);
while (e != nil) {
// Sanity checks
// if (!is_cons(e)) {
// println(e);
// fatal("assoc: Non-cons environment");
// }
// if (!is_cons(car(e))) {
// println(e);
// fatal("assoc: Ill-formed environment");
// }
if (a == car(car(e))) {
push(cdr(car(e)));
collapse(2);
return;
}
e = cdr(e);
}
throw LispError("assoc: Undefined variable");
}
static void evcon(Cell* c, Cell* e)
{
push(c);
push(e);
evcon();
}
static void evcon()
{
Cell* c;
Cell* e;
unpack(c, e);
trace("evcon");
while (c != nil) {
eval(car(car(c)), e);
if (is_true(pop())) {
eval(car(cdr(car(c))), e);
collapse(2);
return;
}
c = cdr(c);
}
throw LispError("evcon: Conditionals exhausted");
}
// a.k.a "zip"
static void pairlis(Cell* x, Cell* y, Cell* e)
{
push(x);
push(y);
push(e);
trace("pairlis",
(is_cons(x) ? car(x) : x),
(is_cons(y) ? car(y) : x) );
int nPairs = 0;
// Build a stack of <name,value> pairs
while (x != nil && y != nil) {
cons(car(x), car(y));
nPairs++;
x = cdr(x);
y = cdr(y);
}
if (x != nil) {
throw LispError("pairlis: Missing arguments");
}
if (y != nil) {
throw LispError("pairlis: Too many arguments");
}
// Unwind the stack into a list, prepended onto 'e'
push(e);
while (nPairs-- > 0) {
cons();
}
collapse(3);
}
static void evlis(Cell* m, Cell* a)
{
push(m);
push(a);
evlis();
}
static void evlis() {
Cell* m;
Cell* a;
unpack(m, a);
trace("evlis");
if (m == nil) {
push(nil);
} else {
// cons(eval(car(m), a), evlis(cdr(m), a) );
eval(car(m), a);
evlis(cdr(m), a);
cons();
}
collapse(2);
}
// ------------------------------------
static void audit_env(Cell* a)
{
while (a != nil)
{
if (!is_cons(a)) {
println(a);
throw LispError("audit_env: Ill-formed environment");
}
if (!is_cons(car(a))) {
println(a);
throw LispError("audit_env: Ill-formed pair");
}
if (!is_atom(car(car(a)))) {
println(a);
throw LispError("audit_env: Non-atom in environment");
}
a = cdr(a);
}
}
// -------------------------------------
static void make_assoc(Cell* name, Cell* value)
{
cons(name, value);
push(nil);
cons();
}
TEST_CASE("assoc() works") {
Cell* name = atom("foo");
Cell* value = atom("fum");
make_assoc(name, value);
Cell* env = pop();
assoc(name, env);
REQUIRE(top() == value);
drop(1);
}
TEST_CASE("pairlis() works for simple environment") {
Cell* name = atom("x");
cons(name, nil);
Cell* params = top();
Cell* value = atom("foo");
cons(value, nil);
Cell* args = top();
pairlis(params, args, nil);
Cell* env = top();
assoc(name, env);
REQUIRE(top() == value);
drop(4);
}
TEST_CASE("eval can evaluate an atom") {
Cell* name = atom("x");
Cell* value = atom("foo");
push(name);
make_assoc(name, value);
eval();
REQUIRE(top() == value);
drop(1);
}
// ((lambda () (quote foo))) should yield 'foo'
// TEST_CASE("apply can handle a nullary function") {
// Cell* foo = atom("foo");
// Cell* expr = cons(...);
// }