-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.c
360 lines (321 loc) · 12.4 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
/*
Evaluate an expression in an environment.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "alisp.h"
/*
--------------------------------------
eval
Evaluate an expression in an environment.
--------------------------------------
*/
atom_t* eval(atom_t* expr, atom_t* env, atom_t** ret) {
#ifdef DEBUG
char* dbg_s = atom_tostr(expr);
printf(".... eval: %s\n", dbg_s);
safe_free(dbg_s);
#endif
active_env = env;
if (!(expr && env)) {
printf("\x1b[95m" "Fatal error: eval: bad argument(s)!\n" "\x1b[0m");
exit(EXIT_FAILURE);
}
// -------------------------------------
// Primitive expressions
if (expr->type == NUMBER)
return expr; // number
if (expr->type == SYMBOL) {
if (expr->val.sym[0] == '"')
return expr; // quoted string
atom_t* v = dict_get(env, expr->val.sym);
if (v)
return v; // variable
char* o = atom_tostr(expr);
errmsg("Semantic", "undefined variable", o, o);
safe_free(o);
return NULL;
}
// -------------------------------------
// Compound expressions
if (expr->type == LIST) {
atom_t** items = expr->val.list->items;
int elen = list_len(expr);
// -------------------------------------
// empty list ()
if (elen == 0)
return list();
int is_sym = items[0]->type == SYMBOL;
// -------------------------------------
// cond (cond (clause expr)... [(else expr)])
if (is_sym && streq(items[0]->val.sym, "cond")) {
if (elen < 2) {
errmsg("Syntax", "poorly formed branching: (cond (clause expr)... [(else expr)])",
NULL, NULL);
list_print(expr, 0);
return NULL;
} else {
for (int i = 1; i < elen; ++i)
if (items[i]->type != LIST || list_len(items[i]) < 2) {
char* o = atom_tostr(items[i]);
errmsg("Syntax", "poorly formed conditional: (test expr [expr...])", o, o);
safe_free(o);
return NULL;
}
}
// Iterate through clauses until test passes or 'else' encountered
for (int i = 1; i < elen; ++i) {
int clen = list_len(items[i]);
atom_t** clause = items[i]->val.list->items;
atom_t* test = clause[0];
// Assemble clause body
atom_t* body;
if (clen > 2) { // provide a block for clause body
body = list();
list_add(body, sym("block"));
for (int j = 1; j < clen; ++j)
list_add(body, clause[j]);
} else // clause body is a single expression
body = clause[1];
// Check if 'else' is encountered
if (test->type == SYMBOL && streq(test->val.sym, "else")) {
atom_t* v = eval(body, env, ret);
active_env = env;
atom_del(body);
return v;
}
// Evaluate test. If it's true -- evaluate body
test = eval(test, env, NULL);
active_env = env;
if (!test)
return NULL;
if (!(test->type == NIL ||
(test->type == NUMBER && *test->val.num == 0) ||
(test->type == SYMBOL && strlen(test->val.sym) == 0) ||
(test->type == LIST && list_len(test) == 0))) {
atom_del(test);
atom_t* v = eval(body, env, ret);
active_env = env;
atom_del(body);
return v;
} else {
atom_del(test);
atom_del(body);
}
}
return &nilobj; // all clauses failed
// -------------------------------------
// if (if test pro [con])
} else if (is_sym && streq(items[0]->val.sym, "if")) {
if (elen < 3 || elen > 4) {
errmsg("Syntax", "poorly formed branching: (if test pro [con])", NULL, NULL);
list_print(expr, 0);
return NULL;
}
atom_t* test = eval(items[1], env, NULL);
active_env = env;
if (!test)
return NULL;
if (test->type == NIL ||
(test->type == NUMBER && *test->val.num == 0) ||
(test->type == SYMBOL && strlen(test->val.sym) == 0) ||
(test->type == LIST && list_len(test) == 0)) {
atom_del(test);
if (elen == 4)
return eval(items[3], env, ret);
return &nilobj;
} else {
atom_del(test);
return eval(items[2], env, ret);
}
// -------------------------------------
// def (def var [expr])
} else if (is_sym && streq(items[0]->val.sym, "def")) {
if (elen < 2 || elen > 3) {
errmsg("Syntax", "poorly formed definition: (def var [expr])", NULL, NULL);
list_print(expr, 0);
return NULL;
} else if (items[1]->type != SYMBOL) {
errmsg("Semantic", "argument is not a variable name", NULL, NULL);
list_print(expr, 0);
return NULL;
} else {
atom_t* e = dict_find(env, items[1]->val.sym);
if (e && e == env) {
errmsg("Semantic", "variable has already been defined", NULL, NULL);
list_print(expr, 0);
return NULL;
}
}
// TODO: check for reserved symbols
if (elen == 2) {
dict_add(env, items[1]->val.sym, &nilobj);
return &nilobj;
} else {
atom_t* v = eval(items[2], env, NULL);
active_env = env;
if (!v)
return NULL;
dict_add(env, items[1]->val.sym, v);
return v;
}
// -------------------------------------
// = (= var expr)
} else if (is_sym && streq(items[0]->val.sym, "=")) {
if (elen != 3) {
errmsg("Syntax", "poorly formed assignment: (= var expr)", NULL, NULL);
list_print(expr, 0);
return NULL;
} else if (items[1]->type != SYMBOL) {
errmsg("Semantic", "argument is not a variable name", NULL, NULL);
list_print(expr, 0);
return NULL;
}
// TODO: check for reserved symbols
atom_t* e = dict_find(env, items[1]->val.sym);
if (!e) {
char* o = atom_tostr(items[1]);
errmsg("Semantic", "undefined variable", o, o);
safe_free(o);
return NULL;
}
atom_t* v = eval(items[2], env, NULL);
active_env = env;
if (!v)
return NULL;
dict_add(e, items[1]->val.sym, v);
return v;
// -------------------------------------
// null? (null? expr)
} else if (is_sym && streq(items[0]->val.sym, "null?")) {
if (elen != 2) {
errmsg("Syntax", "poorly formed expression: (null? expr)", NULL, NULL);
list_print(expr, 0);
return NULL;
}
atom_t* v = eval(items[1], env, NULL);
active_env = env;
if (!v)
return NULL;
if (v->type == NIL)
return num(1);
else {
atom_del(v);
return num(0);
}
// -------------------------------------
// func (func (params) body)
} else if (is_sym && streq(items[0]->val.sym, "func")) {
if (elen < 3 || items[1]->type != LIST) {
errmsg("Syntax", "poorly formed function definition: (func ([var ...]) body)", NULL, NULL);
list_print(expr, 0);
return NULL;
}
// Check that all parameters are indeed symbols
int i;
for (i = 0; i < list_len(items[1]); ++i)
if (items[1]->val.list->items[i]->type != SYMBOL) {
errmsg("Semantic", "parameter is not a variable name", NULL, NULL);
list_print(expr, 0);
return NULL;
}
// Make function body
atom_t* body = list();
list_add(body, sym("block"));
for (i = 2; i < elen; ++i)
list_add(body, items[i]);
atom_t* v = func(items[1], body, env);
atom_del(body);
return v;
// -------------------------------------
// block (block expr [expr ...])
} else if (is_sym && streq(items[0]->val.sym, "block")) {
atom_t* block_ret = NULL;
atom_t* last_v = NULL;
for (int i = 1; !block_ret && i < elen; ++i) {
if (last_v)
atom_del(last_v);
if(!(last_v = eval(items[i], env, &block_ret)))
return NULL; // some eval encountered an error
active_env = env;
}
if (!block_ret)
return last_v; // value of the last expression in a block (default)
atom_del(last_v);
return block_ret; // value of explicit return statement
// -------------------------------------
// ret (ret expr)
} else if (is_sym && streq(items[0]->val.sym, "ret")) {
// Evaluate items[1] and pass it to ret
if (elen != 2) {
errmsg("Syntax", "poorly formed return statement: (ret expr)", NULL, NULL);
list_print(expr, 0);
return NULL;
} else if (!ret) {
errmsg("Semantic", "stray return statement", NULL, NULL);
list_print(expr, 0);
return NULL;
}
return *ret = eval(items[1], env, NULL);
// -------------------------------------
// apply procedure to arguments (proc [arg ...])
} else {
#ifdef DEBUG
printf(".... eval: Evaluating procedure and arguments\n");
#endif
// Evaluate procedure
atom_t* proc = eval(items[0], env, NULL);
active_env = env;
if (!proc)
return NULL;
// Evaluate arguments
atom_t* args = list();
atom_t* v;
for (int i = 1; i < elen; ++i) {
v = eval(items[i], env, NULL);
active_env = env;
if (v) {
list_add(args, v);
} else {
atom_del(proc);
atom_del(args);
return NULL;
}
}
// Protect procedure and arguments
atom_bind(proc, env);
atom_bind(args, env);
#ifdef DEBUG
dbg_s = atom_tostr(proc);
char* dbg_s2 = atom_tostr(args);
printf(".... eval: --> apply %s to %s\n", dbg_s, dbg_s2);
safe_free(dbg_s);
safe_free(dbg_s2);
#endif
// Apply
v = apply(expr, proc, args);
active_env = env;
#ifdef DEBUG
dbg_s = v ? atom_tostr(v) : "NULL";
printf(".... eval: %s <-- apply\n", dbg_s);
printf(".... eval: Deallocating procedure and arguments\n");
if (v) safe_free(dbg_s);
#endif
// Deallocate procedure and arguments
atom_unbind(proc, env);
atom_unbind(args, env);
if (v)
atom_bind(v, env); // protect returned value
atom_del(proc);
atom_del(args);
if (v)
atom_unbind(v, env);
return v;
}
}
char* o = atom_tostr(expr);
errmsg("Semantic", "unexpected object", o, o);
safe_free(o);
return NULL;
}