altos/lisp: Finish first pass through r7rs
[fw/altos] / src / lisp / ao_lisp_eval.c
1 /*
2  * Copyright © 2016 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  */
14
15 #include "ao_lisp.h"
16 #include <assert.h>
17
18 struct ao_lisp_stack            *ao_lisp_stack;
19 ao_poly                         ao_lisp_v;
20 uint8_t                         ao_lisp_skip_cons_free;
21
22 ao_poly
23 ao_lisp_set_cond(struct ao_lisp_cons *c)
24 {
25         ao_lisp_stack->state = eval_cond;
26         ao_lisp_stack->sexprs = ao_lisp_cons_poly(c);
27         return AO_LISP_NIL;
28 }
29
30 static int
31 func_type(ao_poly func)
32 {
33         if (func == AO_LISP_NIL)
34                 return ao_lisp_error(AO_LISP_INVALID, "func is nil");
35         switch (ao_lisp_poly_type(func)) {
36         case AO_LISP_BUILTIN:
37                 return ao_lisp_poly_builtin(func)->args & AO_LISP_FUNC_MASK;
38         case AO_LISP_LAMBDA:
39                 return ao_lisp_poly_lambda(func)->args;
40         case AO_LISP_STACK:
41                 return AO_LISP_FUNC_LAMBDA;
42         default:
43                 ao_lisp_error(AO_LISP_INVALID, "not a func");
44                 return -1;
45         }
46 }
47
48 /*
49  * Flattened eval to avoid stack issues
50  */
51
52 /*
53  * Evaluate an s-expression
54  *
55  * For a list, evaluate all of the elements and
56  * then execute the resulting function call.
57  *
58  * Each element of the list is evaluated in
59  * a clean stack context.
60  *
61  * The current stack state is set to 'formal' so that
62  * when the evaluation is complete, the value
63  * will get appended to the values list.
64  *
65  * For other types, compute the value directly.
66  */
67
68 static int
69 ao_lisp_eval_sexpr(void)
70 {
71         DBGI("sexpr: "); DBG_POLY(ao_lisp_v); DBG("\n");
72         switch (ao_lisp_poly_type(ao_lisp_v)) {
73         case AO_LISP_CONS:
74                 if (ao_lisp_v == AO_LISP_NIL) {
75                         if (!ao_lisp_stack->values) {
76                                 /*
77                                  * empty list evaluates to empty list
78                                  */
79                                 ao_lisp_v = AO_LISP_NIL;
80                                 ao_lisp_stack->state = eval_val;
81                         } else {
82                                 /*
83                                  * done with arguments, go execute it
84                                  */
85                                 ao_lisp_v = ao_lisp_poly_cons(ao_lisp_stack->values)->car;
86                                 ao_lisp_stack->state = eval_exec;
87                         }
88                 } else {
89                         if (!ao_lisp_stack->values)
90                                 ao_lisp_stack->list = ao_lisp_v;
91                         /*
92                          * Evaluate another argument and then switch
93                          * to 'formal' to add the value to the values
94                          * list
95                          */
96                         ao_lisp_stack->sexprs = ao_lisp_v;
97                         ao_lisp_stack->state = eval_formal;
98                         if (!ao_lisp_stack_push())
99                                 return 0;
100                         /*
101                          * push will reset the state to 'sexpr', which
102                          * will evaluate the expression
103                          */
104                         ao_lisp_v = ao_lisp_poly_cons(ao_lisp_v)->car;
105                 }
106                 break;
107         case AO_LISP_ATOM:
108                 DBGI("..frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
109                 ao_lisp_v = ao_lisp_atom_get(ao_lisp_v);
110                 /* fall through */
111         case AO_LISP_BOOL:
112         case AO_LISP_INT:
113         case AO_LISP_STRING:
114         case AO_LISP_BUILTIN:
115         case AO_LISP_LAMBDA:
116                 ao_lisp_stack->state = eval_val;
117                 break;
118         }
119         DBGI(".. result "); DBG_POLY(ao_lisp_v); DBG("\n");
120         return 1;
121 }
122
123 /*
124  * A value has been computed.
125  *
126  * If the value was computed from a macro,
127  * then we want to reset the current context
128  * to evaluate the macro result again.
129  *
130  * If not a macro, then pop the stack.
131  * If the stack is empty, we're done.
132  * Otherwise, the stack will contain
133  * the next state.
134  */
135
136 static int
137 ao_lisp_eval_val(void)
138 {
139         DBGI("val: "); DBG_POLY(ao_lisp_v); DBG("\n");
140         /*
141          * Value computed, pop the stack
142          * to figure out what to do with the value
143          */
144         ao_lisp_stack_pop();
145         DBGI("..state %d\n", ao_lisp_stack ? ao_lisp_stack->state : -1);
146         return 1;
147 }
148
149 /*
150  * A formal has been computed.
151  *
152  * If this is the first formal, then check to see if we've got a
153  * lamda/lexpr or macro/nlambda.
154  *
155  * For lambda/lexpr, go compute another formal.  This will terminate
156  * when the sexpr state sees nil.
157  *
158  * For macro/nlambda, we're done, so move the sexprs into the values
159  * and go execute it.
160  *
161  * Macros have an additional step of saving a stack frame holding the
162  * macro value execution context, which then gets the result of the
163  * macro to run
164  */
165
166 static int
167 ao_lisp_eval_formal(void)
168 {
169         ao_poly                 formal;
170         struct ao_lisp_stack    *prev;
171
172         DBGI("formal: "); DBG_POLY(ao_lisp_v); DBG("\n");
173
174         /* Check what kind of function we've got */
175         if (!ao_lisp_stack->values) {
176                 switch (func_type(ao_lisp_v)) {
177                 case AO_LISP_FUNC_LAMBDA:
178                 case AO_LISP_FUNC_LEXPR:
179                         DBGI(".. lambda or lexpr\n");
180                         break;
181                 case AO_LISP_FUNC_MACRO:
182                         /* Evaluate the result once more */
183                         ao_lisp_stack->state = eval_macro;
184                         if (!ao_lisp_stack_push())
185                                 return 0;
186
187                         /* After the function returns, take that
188                          * value and re-evaluate it
189                          */
190                         prev = ao_lisp_poly_stack(ao_lisp_stack->prev);
191                         ao_lisp_stack->sexprs = prev->sexprs;
192
193                         DBGI(".. start macro\n");
194                         DBGI(".. sexprs       "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
195                         DBGI(".. values       "); DBG_POLY(ao_lisp_stack->values); DBG("\n");
196                         DBG_FRAMES();
197
198                         /* fall through ... */
199                 case AO_LISP_FUNC_NLAMBDA:
200                         DBGI(".. nlambda or macro\n");
201
202                         /* use the raw sexprs as values */
203                         ao_lisp_stack->values = ao_lisp_stack->sexprs;
204                         ao_lisp_stack->values_tail = AO_LISP_NIL;
205                         ao_lisp_stack->state = eval_exec;
206
207                         /* ready to execute now */
208                         return 1;
209                 case -1:
210                         return 0;
211                 }
212         }
213
214         /* Append formal to list of values */
215         formal = ao_lisp__cons(ao_lisp_v, AO_LISP_NIL);
216         if (!formal)
217                 return 0;
218
219         if (ao_lisp_stack->values_tail)
220                 ao_lisp_poly_cons(ao_lisp_stack->values_tail)->cdr = formal;
221         else
222                 ao_lisp_stack->values = formal;
223         ao_lisp_stack->values_tail = formal;
224
225         DBGI(".. values "); DBG_POLY(ao_lisp_stack->values); DBG("\n");
226
227         /*
228          * Step to the next argument, if this is last, then
229          * 'sexpr' will end up switching to 'exec'
230          */
231         ao_lisp_v = ao_lisp_poly_cons(ao_lisp_stack->sexprs)->cdr;
232
233         ao_lisp_stack->state = eval_sexpr;
234
235         DBGI(".. "); DBG_POLY(ao_lisp_v); DBG("\n");
236         return 1;
237 }
238
239 /*
240  * Start executing a function call
241  *
242  * Most builtins are easy, just call the function.
243  * 'cond' is magic; it sticks the list of clauses
244  * in 'sexprs' and switches to 'cond' state. That
245  * bit of magic is done in ao_lisp_set_cond.
246  *
247  * Lambdas build a new frame to hold the locals and
248  * then re-use the current stack context to evaluate
249  * the s-expression from the lambda.
250  */
251
252 static int
253 ao_lisp_eval_exec(void)
254 {
255         ao_poly v;
256         struct ao_lisp_builtin  *builtin;
257
258         DBGI("exec: "); DBG_POLY(ao_lisp_v); DBG(" values "); DBG_POLY(ao_lisp_stack->values); DBG ("\n");
259         ao_lisp_stack->sexprs = AO_LISP_NIL;
260         switch (ao_lisp_poly_type(ao_lisp_v)) {
261         case AO_LISP_BUILTIN:
262                 ao_lisp_stack->state = eval_val;
263                 builtin = ao_lisp_poly_builtin(ao_lisp_v);
264                 v = ao_lisp_func(builtin) (
265                         ao_lisp_poly_cons(ao_lisp_poly_cons(ao_lisp_stack->values)->cdr));
266                 DBG_DO(if (!ao_lisp_exception && ao_lisp_poly_builtin(ao_lisp_v)->func == builtin_set) {
267                                 struct ao_lisp_cons *cons = ao_lisp_poly_cons(ao_lisp_stack->values);
268                                 ao_poly atom = ao_lisp_arg(cons, 1);
269                                 ao_poly val = ao_lisp_arg(cons, 2);
270                                 DBGI("set "); DBG_POLY(atom); DBG(" = "); DBG_POLY(val); DBG("\n");
271                         });
272                 builtin = ao_lisp_poly_builtin(ao_lisp_v);
273                 if (builtin && builtin->args & AO_LISP_FUNC_FREE_ARGS && !ao_lisp_stack_marked(ao_lisp_stack) && !ao_lisp_skip_cons_free)
274                         ao_lisp_cons_free(ao_lisp_poly_cons(ao_lisp_stack->values));
275
276                 ao_lisp_v = v;
277                 ao_lisp_stack->values = AO_LISP_NIL;
278                 ao_lisp_stack->values_tail = AO_LISP_NIL;
279                 DBGI(".. result "); DBG_POLY(ao_lisp_v); DBG ("\n");
280                 DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
281                 break;
282         case AO_LISP_LAMBDA:
283                 DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
284                 ao_lisp_stack->state = eval_progn;
285                 v = ao_lisp_lambda_eval();
286                 ao_lisp_stack->sexprs = v;
287                 ao_lisp_stack->values = AO_LISP_NIL;
288                 ao_lisp_stack->values_tail = AO_LISP_NIL;
289                 DBGI(".. sexprs "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
290                 DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
291                 break;
292         case AO_LISP_STACK:
293                 DBGI(".. stack "); DBG_POLY(ao_lisp_v); DBG("\n");
294                 ao_lisp_v = ao_lisp_stack_eval();
295                 DBGI(".. value "); DBG_POLY(ao_lisp_v); DBG("\n");
296                 DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
297                 break;
298         }
299         ao_lisp_skip_cons_free = 0;
300         return 1;
301 }
302
303 /*
304  * Finish setting up the apply evaluation
305  *
306  * The value is the list to execute
307  */
308 static int
309 ao_lisp_eval_apply(void)
310 {
311         struct ao_lisp_cons     *cons = ao_lisp_poly_cons(ao_lisp_v);
312         struct ao_lisp_cons     *cdr, *prev;
313
314         /* Glue the arguments into the right shape. That's all but the last
315          * concatenated onto the last
316          */
317         cdr = cons;
318         for (;;) {
319                 prev = cdr;
320                 cdr = ao_lisp_poly_cons(prev->cdr);
321                 if (cdr->cdr == AO_LISP_NIL)
322                         break;
323         }
324         DBGI("before mangling: "); DBG_POLY(ao_lisp_v); DBG("\n");
325         prev->cdr = cdr->car;
326         ao_lisp_stack->values = ao_lisp_v;
327         ao_lisp_v = ao_lisp_poly_cons(ao_lisp_stack->values)->car;
328         DBGI("apply: "); DBG_POLY(ao_lisp_stack->values); DBG ("\n");
329         ao_lisp_stack->state = eval_exec;
330         ao_lisp_skip_cons_free = 1;
331         return 1;
332 }
333
334 /*
335  * Start evaluating the next cond clause
336  *
337  * If the list of clauses is empty, then
338  * the result of the cond is nil.
339  *
340  * Otherwise, set the current stack state to 'cond_test' and create a
341  * new stack context to evaluate the test s-expression. Once that's
342  * complete, we'll land in 'cond_test' to finish the clause.
343  */
344 static int
345 ao_lisp_eval_cond(void)
346 {
347         DBGI("cond: "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
348         DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
349         DBGI(".. saved frame "); DBG_POLY(ao_lisp_stack->frame); DBG("\n");
350         if (!ao_lisp_stack->sexprs) {
351                 ao_lisp_v = AO_LISP_NIL;
352                 ao_lisp_stack->state = eval_val;
353         } else {
354                 ao_lisp_v = ao_lisp_poly_cons(ao_lisp_stack->sexprs)->car;
355                 if (!ao_lisp_v || ao_lisp_poly_type(ao_lisp_v) != AO_LISP_CONS) {
356                         ao_lisp_error(AO_LISP_INVALID, "invalid cond clause");
357                         return 0;
358                 }
359                 ao_lisp_v = ao_lisp_poly_cons(ao_lisp_v)->car;
360                 if (ao_lisp_v == _ao_lisp_atom_else)
361                         ao_lisp_v = _ao_lisp_bool_true;
362                 ao_lisp_stack->state = eval_cond_test;
363                 if (!ao_lisp_stack_push())
364                         return 0;
365         }
366         return 1;
367 }
368
369 /*
370  * Finish a cond clause.
371  *
372  * Check the value from the test expression, if
373  * non-nil, then set up to evaluate the value expression.
374  *
375  * Otherwise, step to the next clause and go back to the 'cond'
376  * state
377  */
378 static int
379 ao_lisp_eval_cond_test(void)
380 {
381         DBGI("cond_test: "); DBG_POLY(ao_lisp_v); DBG(" sexprs "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
382         DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
383         DBGI(".. saved frame "); DBG_POLY(ao_lisp_stack->frame); DBG("\n");
384         if (ao_lisp_v != _ao_lisp_bool_false) {
385                 struct ao_lisp_cons *car = ao_lisp_poly_cons(ao_lisp_poly_cons(ao_lisp_stack->sexprs)->car);
386                 ao_poly c = car->cdr;
387
388                 if (c) {
389                         ao_lisp_stack->state = eval_progn;
390                         ao_lisp_stack->sexprs = c;
391                 } else
392                         ao_lisp_stack->state = eval_val;
393         } else {
394                 ao_lisp_stack->sexprs = ao_lisp_poly_cons(ao_lisp_stack->sexprs)->cdr;
395                 DBGI("next cond: "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
396                 ao_lisp_stack->state = eval_cond;
397         }
398         return 1;
399 }
400
401 /*
402  * Evaluate a list of sexprs, returning the value from the last one.
403  *
404  * ao_lisp_progn records the list in stack->sexprs, so we just need to
405  * walk that list. Set ao_lisp_v to the car of the list and jump to
406  * eval_sexpr. When that's done, it will land in eval_val. For all but
407  * the last, leave a stack frame with eval_progn set so that we come
408  * back here. For the last, don't add a stack frame so that we can
409  * just continue on.
410  */
411 static int
412 ao_lisp_eval_progn(void)
413 {
414         DBGI("progn: "); DBG_POLY(ao_lisp_v); DBG(" sexprs "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
415         DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
416         DBGI(".. saved frame "); DBG_POLY(ao_lisp_stack->frame); DBG("\n");
417
418         if (!ao_lisp_stack->sexprs) {
419                 ao_lisp_v = AO_LISP_NIL;
420                 ao_lisp_stack->state = eval_val;
421         } else {
422                 ao_lisp_v = ao_lisp_poly_cons(ao_lisp_stack->sexprs)->car;
423                 ao_lisp_stack->sexprs = ao_lisp_poly_cons(ao_lisp_stack->sexprs)->cdr;
424
425                 /* If there are more sexprs to do, then come back here, otherwise
426                  * return the value of the last one by just landing in eval_sexpr
427                  */
428                 if (ao_lisp_stack->sexprs) {
429                         ao_lisp_stack->state = eval_progn;
430                         if (!ao_lisp_stack_push())
431                                 return 0;
432                 }
433                 ao_lisp_stack->state = eval_sexpr;
434         }
435         return 1;
436 }
437
438 /*
439  * Conditionally execute a list of sexprs while the first is true
440  */
441 static int
442 ao_lisp_eval_while(void)
443 {
444         DBGI("while: "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
445         DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
446         DBGI(".. saved frame "); DBG_POLY(ao_lisp_stack->frame); DBG("\n");
447
448         ao_lisp_stack->values = ao_lisp_v;
449         if (!ao_lisp_stack->sexprs) {
450                 ao_lisp_v = AO_LISP_NIL;
451                 ao_lisp_stack->state = eval_val;
452         } else {
453                 ao_lisp_v = ao_lisp_poly_cons(ao_lisp_stack->sexprs)->car;
454                 ao_lisp_stack->state = eval_while_test;
455                 if (!ao_lisp_stack_push())
456                         return 0;
457         }
458         return 1;
459 }
460
461 /*
462  * Check the while condition, terminate the loop if nil. Otherwise keep going
463  */
464 static int
465 ao_lisp_eval_while_test(void)
466 {
467         DBGI("while_test: "); DBG_POLY(ao_lisp_v); DBG(" sexprs "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
468         DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
469         DBGI(".. saved frame "); DBG_POLY(ao_lisp_stack->frame); DBG("\n");
470
471         if (ao_lisp_v != _ao_lisp_bool_false) {
472                 ao_lisp_stack->values = ao_lisp_v;
473                 ao_lisp_v = ao_lisp_poly_cons(ao_lisp_stack->sexprs)->cdr;
474                 ao_lisp_stack->state = eval_while;
475                 if (!ao_lisp_stack_push())
476                         return 0;
477                 ao_lisp_stack->state = eval_progn;
478                 ao_lisp_stack->sexprs = ao_lisp_v;
479         }
480         else
481         {
482                 ao_lisp_stack->state = eval_val;
483                 ao_lisp_v = ao_lisp_stack->values;
484         }
485         return 1;
486 }
487
488 /*
489  * Replace the original sexpr with the macro expansion, then
490  * execute that
491  */
492 static int
493 ao_lisp_eval_macro(void)
494 {
495         DBGI("macro: "); DBG_POLY(ao_lisp_v); DBG(" sexprs "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
496
497         if (ao_lisp_v == AO_LISP_NIL)
498                 ao_lisp_abort();
499         if (ao_lisp_poly_type(ao_lisp_v) == AO_LISP_CONS) {
500                 *ao_lisp_poly_cons(ao_lisp_stack->sexprs) = *ao_lisp_poly_cons(ao_lisp_v);
501                 ao_lisp_v = ao_lisp_stack->sexprs;
502                 DBGI("sexprs rewritten to: "); DBG_POLY(ao_lisp_v); DBG("\n");
503         }
504         ao_lisp_stack->sexprs = AO_LISP_NIL;
505         ao_lisp_stack->state = eval_sexpr;
506         return 1;
507 }
508
509 static int (*const evals[])(void) = {
510         [eval_sexpr] = ao_lisp_eval_sexpr,
511         [eval_val] = ao_lisp_eval_val,
512         [eval_formal] = ao_lisp_eval_formal,
513         [eval_exec] = ao_lisp_eval_exec,
514         [eval_apply] = ao_lisp_eval_apply,
515         [eval_cond] = ao_lisp_eval_cond,
516         [eval_cond_test] = ao_lisp_eval_cond_test,
517         [eval_progn] = ao_lisp_eval_progn,
518         [eval_while] = ao_lisp_eval_while,
519         [eval_while_test] = ao_lisp_eval_while_test,
520         [eval_macro] = ao_lisp_eval_macro,
521 };
522
523 const char *ao_lisp_state_names[] = {
524         [eval_sexpr] = "sexpr",
525         [eval_val] = "val",
526         [eval_formal] = "formal",
527         [eval_exec] = "exec",
528         [eval_apply] = "apply",
529         [eval_cond] = "cond",
530         [eval_cond_test] = "cond_test",
531         [eval_progn] = "progn",
532         [eval_while] = "while",
533         [eval_while_test] = "while_test",
534         [eval_macro] = "macro",
535 };
536
537 /*
538  * Called at restore time to reset all execution state
539  */
540
541 void
542 ao_lisp_eval_clear_globals(void)
543 {
544         ao_lisp_stack = NULL;
545         ao_lisp_frame_current = NULL;
546         ao_lisp_v = AO_LISP_NIL;
547 }
548
549 int
550 ao_lisp_eval_restart(void)
551 {
552         return ao_lisp_stack_push();
553 }
554
555 ao_poly
556 ao_lisp_eval(ao_poly _v)
557 {
558         ao_lisp_v = _v;
559
560         if (!ao_lisp_stack_push())
561                 return AO_LISP_NIL;
562
563         while (ao_lisp_stack) {
564                 if (!(*evals[ao_lisp_stack->state])() || ao_lisp_exception) {
565                         ao_lisp_stack_clear();
566                         return AO_LISP_NIL;
567                 }
568         }
569         DBG_DO(if (ao_lisp_frame_current) {DBGI("frame left as "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");});
570         ao_lisp_frame_current = NULL;
571         return ao_lisp_v;
572 }