forked from lwhjp/building-lisp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.c
402 lines (339 loc) · 8.73 KB
/
eval.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include "lisp.h"
#include <string.h>
Atom env_create(Atom parent)
{
return cons(parent, nil);
}
int env_define(Atom env, Atom symbol, Atom value)
{
Atom bs = cdr(env);
while (!nilp(bs)) {
Atom b = car(bs);
if (car(b).value.symbol == symbol.value.symbol) {
cdr(b) = value;
return Error_OK;
}
bs = cdr(bs);
}
cdr(env) = cons(cons(symbol, value), cdr(env));
return Error_OK;
}
int env_get(Atom env, Atom symbol, Atom *result)
{
Atom parent = car(env);
Atom bs = cdr(env);
while (!nilp(bs)) {
Atom b = car(bs);
if (car(b).value.symbol == symbol.value.symbol) {
*result = cdr(b);
return Error_OK;
}
bs = cdr(bs);
}
if (nilp(parent))
return Error_Unbound;
return env_get(parent, symbol, result);
}
int env_set(Atom env, Atom symbol, Atom value)
{
Atom parent = car(env);
Atom bs = cdr(env);
while (!nilp(bs)) {
Atom b = car(bs);
if (car(b).value.symbol == symbol.value.symbol) {
cdr(b) = value;
return Error_OK;
}
bs = cdr(bs);
}
if (nilp(parent))
return Error_Unbound;
return env_set(parent, symbol, value);
}
int make_closure(Atom env, Atom args, Atom body, Atom *result)
{
Atom p;
if (!listp(body))
return Error_Syntax;
/* Check argument names are all symbols */
p = args;
while (!nilp(p)) {
if (p.type == AtomType_Symbol)
break;
else if (p.type != AtomType_Pair
|| car(p).type != AtomType_Symbol)
return Error_Type;
p = cdr(p);
}
*result = cons(env, cons(args, body));
result->type = AtomType_Closure;
return Error_OK;
}
Atom make_frame(Atom parent, Atom env, Atom tail)
{
return cons(parent,
cons(env,
cons(nil, /* op */
cons(tail,
cons(nil, /* args */
cons(nil, /* body */
nil))))));
}
int eval_do_exec(Atom *stack, Atom *expr, Atom *env)
{
Atom body;
*env = list_get(*stack, 1);
body = list_get(*stack, 5);
*expr = car(body);
body = cdr(body);
if (nilp(body)) {
/* Finished function; pop the stack */
*stack = car(*stack);
} else {
list_set(*stack, 5, body);
}
return Error_OK;
}
int eval_do_bind(Atom *stack, Atom *expr, Atom *env)
{
Atom op, args, arg_names, body;
body = list_get(*stack, 5);
if (!nilp(body))
return eval_do_exec(stack, expr, env);
op = list_get(*stack, 2);
args = list_get(*stack, 4);
*env = env_create(car(op));
arg_names = car(cdr(op));
body = cdr(cdr(op));
list_set(*stack, 1, *env);
list_set(*stack, 5, body);
/* Bind the arguments */
while (!nilp(arg_names)) {
if (arg_names.type == AtomType_Symbol) {
env_define(*env, arg_names, args);
args = nil;
break;
}
if (nilp(args))
return Error_Args;
env_define(*env, car(arg_names), car(args));
arg_names = cdr(arg_names);
args = cdr(args);
}
if (!nilp(args))
return Error_Args;
list_set(*stack, 4, nil);
return eval_do_exec(stack, expr, env);
}
int eval_do_apply(Atom *stack, Atom *expr, Atom *env, Atom *result)
{
Atom op, args;
op = list_get(*stack, 2);
args = list_get(*stack, 4);
if (!nilp(args)) {
list_reverse(&args);
list_set(*stack, 4, args);
}
if (op.type == AtomType_Symbol) {
if (strcmp(op.value.symbol, "APPLY") == 0) {
/* Replace the current frame */
*stack = car(*stack);
*stack = make_frame(*stack, *env, nil);
op = car(args);
args = car(cdr(args));
if (!listp(args))
return Error_Syntax;
list_set(*stack, 2, op);
list_set(*stack, 4, args);
}
}
if (op.type == AtomType_Builtin) {
*stack = car(*stack);
*expr = cons(op, args);
return Error_OK;
} else if (op.type != AtomType_Closure) {
return Error_Type;
}
return eval_do_bind(stack, expr, env);
}
int eval_do_return(Atom *stack, Atom *expr, Atom *env, Atom *result)
{
Atom op, args, body;
*env = list_get(*stack, 1);
op = list_get(*stack, 2);
body = list_get(*stack, 5);
if (!nilp(body)) {
/* Still running a procedure; ignore the result */
return eval_do_apply(stack, expr, env, result);
}
if (nilp(op)) {
/* Finished evaluating operator */
op = *result;
list_set(*stack, 2, op);
if (op.type == AtomType_Macro) {
/* Don't evaluate macro arguments */
args = list_get(*stack, 3);
*stack = make_frame(*stack, *env, nil);
op.type = AtomType_Closure;
list_set(*stack, 2, op);
list_set(*stack, 4, args);
return eval_do_bind(stack, expr, env);
}
} else if (op.type == AtomType_Symbol) {
/* Finished working on special form */
if (strcmp(op.value.symbol, "DEFINE") == 0) {
Atom sym = list_get(*stack, 4);
(void) env_define(*env, sym, *result);
*stack = car(*stack);
*expr = cons(make_sym("QUOTE"), cons(sym, nil));
return Error_OK;
} else if (strcmp(op.value.symbol, "SET!") == 0) {
Atom sym = list_get(*stack, 4);
*stack = car(*stack);
*expr = cons(make_sym("QUOTE"), cons(sym, nil));
return env_set(*env, sym, *result);
} else if (strcmp(op.value.symbol, "IF") == 0) {
args = list_get(*stack, 3);
*expr = nilp(*result) ? car(cdr(args)) : car(args);
*stack = car(*stack);
return Error_OK;
} else {
goto store_arg;
}
} else if (op.type == AtomType_Macro) {
/* Finished evaluating macro */
*expr = *result;
*stack = car(*stack);
return Error_OK;
} else {
store_arg:
/* Store evaluated argument */
args = list_get(*stack, 4);
list_set(*stack, 4, cons(*result, args));
}
args = list_get(*stack, 3);
if (nilp(args)) {
/* No more arguments left to evaluate */
return eval_do_apply(stack, expr, env, result);
}
/* Evaluate next argument */
*expr = car(args);
list_set(*stack, 3, cdr(args));
return Error_OK;
}
int eval_expr(Atom expr, Atom env, Atom *result)
{
static int count = 0;
Error err = Error_OK;
Atom stack = nil;
do {
if (++count == 100000) {
gc_mark(expr);
gc_mark(env);
gc_mark(stack);
gc();
count = 0;
}
if (expr.type == AtomType_Symbol) {
err = env_get(env, expr, result);
} else if (expr.type != AtomType_Pair) {
*result = expr;
} else if (!listp(expr)) {
return Error_Syntax;
} else {
Atom op = car(expr);
Atom args = cdr(expr);
if (op.type == AtomType_Symbol) {
/* Handle special forms */
if (strcmp(op.value.symbol, "QUOTE") == 0) {
if (nilp(args) || !nilp(cdr(args)))
return Error_Args;
*result = car(args);
} else if (strcmp(op.value.symbol, "DEFINE") == 0) {
Atom sym;
if (nilp(args) || nilp(cdr(args)))
return Error_Args;
sym = car(args);
if (sym.type == AtomType_Pair) {
err = make_closure(env, cdr(sym), cdr(args), result);
sym = car(sym);
if (sym.type != AtomType_Symbol)
return Error_Type;
(void) env_define(env, sym, *result);
*result = sym;
} else if (sym.type == AtomType_Symbol) {
if (!nilp(cdr(cdr(args))))
return Error_Args;
stack = make_frame(stack, env, nil);
list_set(stack, 2, op);
list_set(stack, 4, sym);
expr = car(cdr(args));
continue;
} else {
return Error_Type;
}
} else if (strcmp(op.value.symbol, "LAMBDA") == 0) {
if (nilp(args) || nilp(cdr(args)))
return Error_Args;
err = make_closure(env, car(args), cdr(args), result);
} else if (strcmp(op.value.symbol, "IF") == 0) {
if (nilp(args) || nilp(cdr(args)) || nilp(cdr(cdr(args)))
|| !nilp(cdr(cdr(cdr(args)))))
return Error_Args;
stack = make_frame(stack, env, cdr(args));
list_set(stack, 2, op);
expr = car(args);
continue;
} else if (strcmp(op.value.symbol, "DEFMACRO") == 0) {
Atom name, macro;
if (nilp(args) || nilp(cdr(args)))
return Error_Args;
if (car(args).type != AtomType_Pair)
return Error_Syntax;
name = car(car(args));
if (name.type != AtomType_Symbol)
return Error_Type;
err = make_closure(env, cdr(car(args)),
cdr(args), ¯o);
if (!err) {
macro.type = AtomType_Macro;
*result = name;
(void) env_define(env, name, macro);
}
} else if (strcmp(op.value.symbol, "APPLY") == 0) {
if (nilp(args) || nilp(cdr(args)) || !nilp(cdr(cdr(args))))
return Error_Args;
stack = make_frame(stack, env, cdr(args));
list_set(stack, 2, op);
expr = car(args);
continue;
} else if (strcmp(op.value.symbol, "SET!") == 0) {
if (nilp(args) || nilp(cdr(args)) || !nilp(cdr(cdr(args))))
return Error_Args;
if (car(args).type != AtomType_Symbol)
return Error_Type;
stack = make_frame(stack, env, nil);
list_set(stack, 2, op);
list_set(stack, 4, car(args));
expr = car(cdr(args));
continue;
} else {
goto push;
}
} else if (op.type == AtomType_Builtin) {
err = (*op.value.builtin)(args, result);
} else {
push:
/* Handle function application */
stack = make_frame(stack, env, args);
expr = op;
continue;
}
}
if (nilp(stack))
break;
if (!err)
err = eval_do_return(&stack, &expr, &env, result);
} while (!err);
return err;
}