altos/lisp: Add 'big' ints -- 24 bits wide
[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_BIGINT:
114         case AO_LISP_STRING:
115         case AO_LISP_BUILTIN:
116         case AO_LISP_LAMBDA:
117                 ao_lisp_stack->state = eval_val;
118                 break;
119         }
120         DBGI(".. result "); DBG_POLY(ao_lisp_v); DBG("\n");
121         return 1;
122 }
123
124 /*
125  * A value has been computed.
126  *
127  * If the value was computed from a macro,
128  * then we want to reset the current context
129  * to evaluate the macro result again.
130  *
131  * If not a macro, then pop the stack.
132  * If the stack is empty, we're done.
133  * Otherwise, the stack will contain
134  * the next state.
135  */
136
137 static int
138 ao_lisp_eval_val(void)
139 {
140         DBGI("val: "); DBG_POLY(ao_lisp_v); DBG("\n");
141         /*
142          * Value computed, pop the stack
143          * to figure out what to do with the value
144          */
145         ao_lisp_stack_pop();
146         DBGI("..state %d\n", ao_lisp_stack ? ao_lisp_stack->state : -1);
147         return 1;
148 }
149
150 /*
151  * A formal has been computed.
152  *
153  * If this is the first formal, then check to see if we've got a
154  * lamda/lexpr or macro/nlambda.
155  *
156  * For lambda/lexpr, go compute another formal.  This will terminate
157  * when the sexpr state sees nil.
158  *
159  * For macro/nlambda, we're done, so move the sexprs into the values
160  * and go execute it.
161  *
162  * Macros have an additional step of saving a stack frame holding the
163  * macro value execution context, which then gets the result of the
164  * macro to run
165  */
166
167 static int
168 ao_lisp_eval_formal(void)
169 {
170         ao_poly                 formal;
171         struct ao_lisp_stack    *prev;
172
173         DBGI("formal: "); DBG_POLY(ao_lisp_v); DBG("\n");
174
175         /* Check what kind of function we've got */
176         if (!ao_lisp_stack->values) {
177                 switch (func_type(ao_lisp_v)) {
178                 case AO_LISP_FUNC_LAMBDA:
179                 case AO_LISP_FUNC_LEXPR:
180                         DBGI(".. lambda or lexpr\n");
181                         break;
182                 case AO_LISP_FUNC_MACRO:
183                         /* Evaluate the result once more */
184                         ao_lisp_stack->state = eval_macro;
185                         if (!ao_lisp_stack_push())
186                                 return 0;
187
188                         /* After the function returns, take that
189                          * value and re-evaluate it
190                          */
191                         prev = ao_lisp_poly_stack(ao_lisp_stack->prev);
192                         ao_lisp_stack->sexprs = prev->sexprs;
193
194                         DBGI(".. start macro\n");
195                         DBGI(".. sexprs       "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
196                         DBGI(".. values       "); DBG_POLY(ao_lisp_stack->values); DBG("\n");
197                         DBG_FRAMES();
198
199                         /* fall through ... */
200                 case AO_LISP_FUNC_NLAMBDA:
201                         DBGI(".. nlambda or macro\n");
202
203                         /* use the raw sexprs as values */
204                         ao_lisp_stack->values = ao_lisp_stack->sexprs;
205                         ao_lisp_stack->values_tail = AO_LISP_NIL;
206                         ao_lisp_stack->state = eval_exec;
207
208                         /* ready to execute now */
209                         return 1;
210                 case -1:
211                         return 0;
212                 }
213         }
214
215         /* Append formal to list of values */
216         formal = ao_lisp__cons(ao_lisp_v, AO_LISP_NIL);
217         if (!formal)
218                 return 0;
219
220         if (ao_lisp_stack->values_tail)
221                 ao_lisp_poly_cons(ao_lisp_stack->values_tail)->cdr = formal;
222         else
223                 ao_lisp_stack->values = formal;
224         ao_lisp_stack->values_tail = formal;
225
226         DBGI(".. values "); DBG_POLY(ao_lisp_stack->values); DBG("\n");
227
228         /*
229          * Step to the next argument, if this is last, then
230          * 'sexpr' will end up switching to 'exec'
231          */
232         ao_lisp_v = ao_lisp_poly_cons(ao_lisp_stack->sexprs)->cdr;
233
234         ao_lisp_stack->state = eval_sexpr;
235
236         DBGI(".. "); DBG_POLY(ao_lisp_v); DBG("\n");
237         return 1;
238 }
239
240 /*
241  * Start executing a function call
242  *
243  * Most builtins are easy, just call the function.
244  * 'cond' is magic; it sticks the list of clauses
245  * in 'sexprs' and switches to 'cond' state. That
246  * bit of magic is done in ao_lisp_set_cond.
247  *
248  * Lambdas build a new frame to hold the locals and
249  * then re-use the current stack context to evaluate
250  * the s-expression from the lambda.
251  */
252
253 static int
254 ao_lisp_eval_exec(void)
255 {
256         ao_poly v;
257         struct ao_lisp_builtin  *builtin;
258
259         DBGI("exec: "); DBG_POLY(ao_lisp_v); DBG(" values "); DBG_POLY(ao_lisp_stack->values); DBG ("\n");
260         ao_lisp_stack->sexprs = AO_LISP_NIL;
261         switch (ao_lisp_poly_type(ao_lisp_v)) {
262         case AO_LISP_BUILTIN:
263                 ao_lisp_stack->state = eval_val;
264                 builtin = ao_lisp_poly_builtin(ao_lisp_v);
265                 v = ao_lisp_func(builtin) (
266                         ao_lisp_poly_cons(ao_lisp_poly_cons(ao_lisp_stack->values)->cdr));
267                 DBG_DO(if (!ao_lisp_exception && ao_lisp_poly_builtin(ao_lisp_v)->func == builtin_set) {
268                                 struct ao_lisp_cons *cons = ao_lisp_poly_cons(ao_lisp_stack->values);
269                                 ao_poly atom = ao_lisp_arg(cons, 1);
270                                 ao_poly val = ao_lisp_arg(cons, 2);
271                                 DBGI("set "); DBG_POLY(atom); DBG(" = "); DBG_POLY(val); DBG("\n");
272                         });
273                 builtin = ao_lisp_poly_builtin(ao_lisp_v);
274                 if (builtin && builtin->args & AO_LISP_FUNC_FREE_ARGS && !ao_lisp_stack_marked(ao_lisp_stack) && !ao_lisp_skip_cons_free)
275                         ao_lisp_cons_free(ao_lisp_poly_cons(ao_lisp_stack->values));
276
277                 ao_lisp_v = v;
278                 ao_lisp_stack->values = AO_LISP_NIL;
279                 ao_lisp_stack->values_tail = AO_LISP_NIL;
280                 DBGI(".. result "); DBG_POLY(ao_lisp_v); DBG ("\n");
281                 DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
282                 break;
283         case AO_LISP_LAMBDA:
284                 DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
285                 ao_lisp_stack->state = eval_progn;
286                 v = ao_lisp_lambda_eval();
287                 ao_lisp_stack->sexprs = v;
288                 ao_lisp_stack->values = AO_LISP_NIL;
289                 ao_lisp_stack->values_tail = AO_LISP_NIL;
290                 DBGI(".. sexprs "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
291                 DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
292                 break;
293         case AO_LISP_STACK:
294                 DBGI(".. stack "); DBG_POLY(ao_lisp_v); DBG("\n");
295                 ao_lisp_v = ao_lisp_stack_eval();
296                 DBGI(".. value "); DBG_POLY(ao_lisp_v); DBG("\n");
297                 DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
298                 break;
299         }
300         ao_lisp_skip_cons_free = 0;
301         return 1;
302 }
303
304 /*
305  * Finish setting up the apply evaluation
306  *
307  * The value is the list to execute
308  */
309 static int
310 ao_lisp_eval_apply(void)
311 {
312         struct ao_lisp_cons     *cons = ao_lisp_poly_cons(ao_lisp_v);
313         struct ao_lisp_cons     *cdr, *prev;
314
315         /* Glue the arguments into the right shape. That's all but the last
316          * concatenated onto the last
317          */
318         cdr = cons;
319         for (;;) {
320                 prev = cdr;
321                 cdr = ao_lisp_poly_cons(prev->cdr);
322                 if (cdr->cdr == AO_LISP_NIL)
323                         break;
324         }
325         DBGI("before mangling: "); DBG_POLY(ao_lisp_v); DBG("\n");
326         prev->cdr = cdr->car;
327         ao_lisp_stack->values = ao_lisp_v;
328         ao_lisp_v = ao_lisp_poly_cons(ao_lisp_stack->values)->car;
329         DBGI("apply: "); DBG_POLY(ao_lisp_stack->values); DBG ("\n");
330         ao_lisp_stack->state = eval_exec;
331         ao_lisp_skip_cons_free = 1;
332         return 1;
333 }
334
335 /*
336  * Start evaluating the next cond clause
337  *
338  * If the list of clauses is empty, then
339  * the result of the cond is nil.
340  *
341  * Otherwise, set the current stack state to 'cond_test' and create a
342  * new stack context to evaluate the test s-expression. Once that's
343  * complete, we'll land in 'cond_test' to finish the clause.
344  */
345 static int
346 ao_lisp_eval_cond(void)
347 {
348         DBGI("cond: "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
349         DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
350         DBGI(".. saved frame "); DBG_POLY(ao_lisp_stack->frame); DBG("\n");
351         if (!ao_lisp_stack->sexprs) {
352                 ao_lisp_v = AO_LISP_NIL;
353                 ao_lisp_stack->state = eval_val;
354         } else {
355                 ao_lisp_v = ao_lisp_poly_cons(ao_lisp_stack->sexprs)->car;
356                 if (!ao_lisp_v || ao_lisp_poly_type(ao_lisp_v) != AO_LISP_CONS) {
357                         ao_lisp_error(AO_LISP_INVALID, "invalid cond clause");
358                         return 0;
359                 }
360                 ao_lisp_v = ao_lisp_poly_cons(ao_lisp_v)->car;
361                 if (ao_lisp_v == _ao_lisp_atom_else)
362                         ao_lisp_v = _ao_lisp_bool_true;
363                 ao_lisp_stack->state = eval_cond_test;
364                 if (!ao_lisp_stack_push())
365                         return 0;
366         }
367         return 1;
368 }
369
370 /*
371  * Finish a cond clause.
372  *
373  * Check the value from the test expression, if
374  * non-nil, then set up to evaluate the value expression.
375  *
376  * Otherwise, step to the next clause and go back to the 'cond'
377  * state
378  */
379 static int
380 ao_lisp_eval_cond_test(void)
381 {
382         DBGI("cond_test: "); DBG_POLY(ao_lisp_v); DBG(" sexprs "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
383         DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
384         DBGI(".. saved frame "); DBG_POLY(ao_lisp_stack->frame); DBG("\n");
385         if (ao_lisp_v != _ao_lisp_bool_false) {
386                 struct ao_lisp_cons *car = ao_lisp_poly_cons(ao_lisp_poly_cons(ao_lisp_stack->sexprs)->car);
387                 ao_poly c = car->cdr;
388
389                 if (c) {
390                         ao_lisp_stack->state = eval_progn;
391                         ao_lisp_stack->sexprs = c;
392                 } else
393                         ao_lisp_stack->state = eval_val;
394         } else {
395                 ao_lisp_stack->sexprs = ao_lisp_poly_cons(ao_lisp_stack->sexprs)->cdr;
396                 DBGI("next cond: "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
397                 ao_lisp_stack->state = eval_cond;
398         }
399         return 1;
400 }
401
402 /*
403  * Evaluate a list of sexprs, returning the value from the last one.
404  *
405  * ao_lisp_progn records the list in stack->sexprs, so we just need to
406  * walk that list. Set ao_lisp_v to the car of the list and jump to
407  * eval_sexpr. When that's done, it will land in eval_val. For all but
408  * the last, leave a stack frame with eval_progn set so that we come
409  * back here. For the last, don't add a stack frame so that we can
410  * just continue on.
411  */
412 static int
413 ao_lisp_eval_progn(void)
414 {
415         DBGI("progn: "); DBG_POLY(ao_lisp_v); DBG(" sexprs "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
416         DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
417         DBGI(".. saved frame "); DBG_POLY(ao_lisp_stack->frame); DBG("\n");
418
419         if (!ao_lisp_stack->sexprs) {
420                 ao_lisp_v = AO_LISP_NIL;
421                 ao_lisp_stack->state = eval_val;
422         } else {
423                 ao_lisp_v = ao_lisp_poly_cons(ao_lisp_stack->sexprs)->car;
424                 ao_lisp_stack->sexprs = ao_lisp_poly_cons(ao_lisp_stack->sexprs)->cdr;
425
426                 /* If there are more sexprs to do, then come back here, otherwise
427                  * return the value of the last one by just landing in eval_sexpr
428                  */
429                 if (ao_lisp_stack->sexprs) {
430                         ao_lisp_stack->state = eval_progn;
431                         if (!ao_lisp_stack_push())
432                                 return 0;
433                 }
434                 ao_lisp_stack->state = eval_sexpr;
435         }
436         return 1;
437 }
438
439 /*
440  * Conditionally execute a list of sexprs while the first is true
441  */
442 static int
443 ao_lisp_eval_while(void)
444 {
445         DBGI("while: "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
446         DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
447         DBGI(".. saved frame "); DBG_POLY(ao_lisp_stack->frame); DBG("\n");
448
449         ao_lisp_stack->values = ao_lisp_v;
450         if (!ao_lisp_stack->sexprs) {
451                 ao_lisp_v = AO_LISP_NIL;
452                 ao_lisp_stack->state = eval_val;
453         } else {
454                 ao_lisp_v = ao_lisp_poly_cons(ao_lisp_stack->sexprs)->car;
455                 ao_lisp_stack->state = eval_while_test;
456                 if (!ao_lisp_stack_push())
457                         return 0;
458         }
459         return 1;
460 }
461
462 /*
463  * Check the while condition, terminate the loop if nil. Otherwise keep going
464  */
465 static int
466 ao_lisp_eval_while_test(void)
467 {
468         DBGI("while_test: "); DBG_POLY(ao_lisp_v); DBG(" sexprs "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
469         DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
470         DBGI(".. saved frame "); DBG_POLY(ao_lisp_stack->frame); DBG("\n");
471
472         if (ao_lisp_v != _ao_lisp_bool_false) {
473                 ao_lisp_stack->values = ao_lisp_v;
474                 ao_lisp_v = ao_lisp_poly_cons(ao_lisp_stack->sexprs)->cdr;
475                 ao_lisp_stack->state = eval_while;
476                 if (!ao_lisp_stack_push())
477                         return 0;
478                 ao_lisp_stack->state = eval_progn;
479                 ao_lisp_stack->sexprs = ao_lisp_v;
480         }
481         else
482         {
483                 ao_lisp_stack->state = eval_val;
484                 ao_lisp_v = ao_lisp_stack->values;
485         }
486         return 1;
487 }
488
489 /*
490  * Replace the original sexpr with the macro expansion, then
491  * execute that
492  */
493 static int
494 ao_lisp_eval_macro(void)
495 {
496         DBGI("macro: "); DBG_POLY(ao_lisp_v); DBG(" sexprs "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
497
498         if (ao_lisp_v == AO_LISP_NIL)
499                 ao_lisp_abort();
500         if (ao_lisp_poly_type(ao_lisp_v) == AO_LISP_CONS) {
501                 *ao_lisp_poly_cons(ao_lisp_stack->sexprs) = *ao_lisp_poly_cons(ao_lisp_v);
502                 ao_lisp_v = ao_lisp_stack->sexprs;
503                 DBGI("sexprs rewritten to: "); DBG_POLY(ao_lisp_v); DBG("\n");
504         }
505         ao_lisp_stack->sexprs = AO_LISP_NIL;
506         ao_lisp_stack->state = eval_sexpr;
507         return 1;
508 }
509
510 static int (*const evals[])(void) = {
511         [eval_sexpr] = ao_lisp_eval_sexpr,
512         [eval_val] = ao_lisp_eval_val,
513         [eval_formal] = ao_lisp_eval_formal,
514         [eval_exec] = ao_lisp_eval_exec,
515         [eval_apply] = ao_lisp_eval_apply,
516         [eval_cond] = ao_lisp_eval_cond,
517         [eval_cond_test] = ao_lisp_eval_cond_test,
518         [eval_progn] = ao_lisp_eval_progn,
519         [eval_while] = ao_lisp_eval_while,
520         [eval_while_test] = ao_lisp_eval_while_test,
521         [eval_macro] = ao_lisp_eval_macro,
522 };
523
524 const char *ao_lisp_state_names[] = {
525         [eval_sexpr] = "sexpr",
526         [eval_val] = "val",
527         [eval_formal] = "formal",
528         [eval_exec] = "exec",
529         [eval_apply] = "apply",
530         [eval_cond] = "cond",
531         [eval_cond_test] = "cond_test",
532         [eval_progn] = "progn",
533         [eval_while] = "while",
534         [eval_while_test] = "while_test",
535         [eval_macro] = "macro",
536 };
537
538 /*
539  * Called at restore time to reset all execution state
540  */
541
542 void
543 ao_lisp_eval_clear_globals(void)
544 {
545         ao_lisp_stack = NULL;
546         ao_lisp_frame_current = NULL;
547         ao_lisp_v = AO_LISP_NIL;
548 }
549
550 int
551 ao_lisp_eval_restart(void)
552 {
553         return ao_lisp_stack_push();
554 }
555
556 ao_poly
557 ao_lisp_eval(ao_poly _v)
558 {
559         ao_lisp_v = _v;
560
561         if (!ao_lisp_stack_push())
562                 return AO_LISP_NIL;
563
564         while (ao_lisp_stack) {
565                 if (!(*evals[ao_lisp_stack->state])() || ao_lisp_exception) {
566                         ao_lisp_stack_clear();
567                         return AO_LISP_NIL;
568                 }
569         }
570         DBG_DO(if (ao_lisp_frame_current) {DBGI("frame left as "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");});
571         ao_lisp_frame_current = NULL;
572         return ao_lisp_v;
573 }