Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[fw/altos] / src / scheme / ao_scheme_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_scheme.h"
16 #include <assert.h>
17
18 struct ao_scheme_stack          *ao_scheme_stack;
19 ao_poly                         ao_scheme_v;
20 uint8_t                         ao_scheme_skip_cons_free;
21
22 ao_poly
23 ao_scheme_set_cond(struct ao_scheme_cons *c)
24 {
25         ao_scheme_stack->state = eval_cond;
26         ao_scheme_stack->sexprs = ao_scheme_cons_poly(c);
27         return AO_SCHEME_NIL;
28 }
29
30 static int
31 func_type(ao_poly func)
32 {
33         if (func == AO_SCHEME_NIL)
34                 return ao_scheme_error(AO_SCHEME_INVALID, "func is nil");
35         switch (ao_scheme_poly_type(func)) {
36         case AO_SCHEME_BUILTIN:
37                 return ao_scheme_poly_builtin(func)->args & AO_SCHEME_FUNC_MASK;
38         case AO_SCHEME_LAMBDA:
39                 return ao_scheme_poly_lambda(func)->args;
40         case AO_SCHEME_STACK:
41                 return AO_SCHEME_FUNC_LAMBDA;
42         default:
43                 ao_scheme_error(AO_SCHEME_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_scheme_eval_sexpr(void)
70 {
71         DBGI("sexpr: %v\n", ao_scheme_v);
72         switch (ao_scheme_poly_type(ao_scheme_v)) {
73         case AO_SCHEME_CONS:
74                 if (ao_scheme_v == AO_SCHEME_NIL) {
75                         if (!ao_scheme_stack->values) {
76                                 /*
77                                  * empty list evaluates to empty list
78                                  */
79                                 ao_scheme_v = AO_SCHEME_NIL;
80                                 ao_scheme_stack->state = eval_val;
81                         } else {
82                                 /*
83                                  * done with arguments, go execute it
84                                  */
85                                 ao_scheme_v = ao_scheme_poly_cons(ao_scheme_stack->values)->car;
86                                 ao_scheme_stack->state = eval_exec;
87                         }
88                 } else {
89                         if (!ao_scheme_stack->values)
90                                 ao_scheme_stack->list = ao_scheme_v;
91                         /*
92                          * Evaluate another argument and then switch
93                          * to 'formal' to add the value to the values
94                          * list
95                          */
96                         ao_scheme_stack->sexprs = ao_scheme_v;
97                         ao_scheme_stack->state = eval_formal;
98                         if (!ao_scheme_stack_push())
99                                 return 0;
100                         /*
101                          * push will reset the state to 'sexpr', which
102                          * will evaluate the expression
103                          */
104                         ao_scheme_v = ao_scheme_poly_cons(ao_scheme_v)->car;
105                 }
106                 break;
107         case AO_SCHEME_ATOM:
108                 DBGI("..frame "); DBG_POLY(ao_scheme_frame_poly(ao_scheme_frame_current)); DBG("\n");
109                 ao_scheme_v = ao_scheme_atom_get(ao_scheme_v);
110                 /* fall through */
111         case AO_SCHEME_BOOL:
112         case AO_SCHEME_INT:
113         case AO_SCHEME_BIGINT:
114         case AO_SCHEME_FLOAT:
115         case AO_SCHEME_STRING:
116         case AO_SCHEME_BUILTIN:
117         case AO_SCHEME_LAMBDA:
118                 ao_scheme_stack->state = eval_val;
119                 break;
120         }
121         DBGI(".. result "); DBG_POLY(ao_scheme_v); DBG("\n");
122         return 1;
123 }
124
125 /*
126  * A value has been computed.
127  *
128  * If the value was computed from a macro,
129  * then we want to reset the current context
130  * to evaluate the macro result again.
131  *
132  * If not a macro, then pop the stack.
133  * If the stack is empty, we're done.
134  * Otherwise, the stack will contain
135  * the next state.
136  */
137
138 static int
139 ao_scheme_eval_val(void)
140 {
141         DBGI("val: "); DBG_POLY(ao_scheme_v); DBG("\n");
142         /*
143          * Value computed, pop the stack
144          * to figure out what to do with the value
145          */
146         ao_scheme_stack_pop();
147         DBGI("..state %d\n", ao_scheme_stack ? ao_scheme_stack->state : -1);
148         return 1;
149 }
150
151 /*
152  * A formal has been computed.
153  *
154  * If this is the first formal, then check to see if we've got a
155  * lamda, macro or nlambda.
156  *
157  * For lambda, go compute another formal.  This will terminate
158  * when the sexpr state sees nil.
159  *
160  * For macro/nlambda, we're done, so move the sexprs into the values
161  * and go execute it.
162  *
163  * Macros have an additional step of saving a stack frame holding the
164  * macro value execution context, which then gets the result of the
165  * macro to run
166  */
167
168 static int
169 ao_scheme_eval_formal(void)
170 {
171         ao_poly                 formal;
172         struct ao_scheme_stack  *prev;
173
174         DBGI("formal: "); DBG_POLY(ao_scheme_v); DBG("\n");
175
176         /* Check what kind of function we've got */
177         if (!ao_scheme_stack->values) {
178                 switch (func_type(ao_scheme_v)) {
179                 case AO_SCHEME_FUNC_LAMBDA:
180                         DBGI(".. lambda\n");
181                         break;
182                 case AO_SCHEME_FUNC_MACRO:
183                         /* Evaluate the result once more */
184                         ao_scheme_stack->state = eval_macro;
185                         if (!ao_scheme_stack_push())
186                                 return 0;
187
188                         /* After the function returns, take that
189                          * value and re-evaluate it
190                          */
191                         prev = ao_scheme_poly_stack(ao_scheme_stack->prev);
192                         ao_scheme_stack->sexprs = prev->sexprs;
193
194                         DBGI(".. start macro\n");
195                         DBGI("\t.. sexprs       "); DBG_POLY(ao_scheme_stack->sexprs); DBG("\n");
196                         DBGI("\t.. values       "); DBG_POLY(ao_scheme_stack->values); DBG("\n");
197                         DBG_FRAMES();
198
199                         /* fall through ... */
200                 case AO_SCHEME_FUNC_NLAMBDA:
201                         DBGI(".. nlambda or macro\n");
202
203                         /* use the raw sexprs as values */
204                         ao_scheme_stack->values = ao_scheme_stack->sexprs;
205                         ao_scheme_stack->values_tail = AO_SCHEME_NIL;
206                         ao_scheme_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_scheme__cons(ao_scheme_v, AO_SCHEME_NIL);
217         if (!formal)
218                 return 0;
219
220         if (ao_scheme_stack->values_tail)
221                 ao_scheme_poly_cons(ao_scheme_stack->values_tail)->cdr = formal;
222         else
223                 ao_scheme_stack->values = formal;
224         ao_scheme_stack->values_tail = formal;
225
226         DBGI(".. values "); DBG_POLY(ao_scheme_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_scheme_v = ao_scheme_poly_cons(ao_scheme_stack->sexprs)->cdr;
233
234         ao_scheme_stack->state = eval_sexpr;
235
236         DBGI(".. "); DBG_POLY(ao_scheme_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_scheme_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_scheme_eval_exec(void)
255 {
256         ao_poly v;
257         struct ao_scheme_builtin        *builtin;
258
259         DBGI("exec: "); DBG_POLY(ao_scheme_v); DBG(" values "); DBG_POLY(ao_scheme_stack->values); DBG ("\n");
260         ao_scheme_stack->sexprs = AO_SCHEME_NIL;
261         switch (ao_scheme_poly_type(ao_scheme_v)) {
262         case AO_SCHEME_BUILTIN:
263                 ao_scheme_stack->state = eval_val;
264                 builtin = ao_scheme_poly_builtin(ao_scheme_v);
265                 v = ao_scheme_func(builtin) (
266                         ao_scheme_poly_cons(ao_scheme_poly_cons(ao_scheme_stack->values)->cdr));
267                 DBG_DO(if (!ao_scheme_exception && ao_scheme_poly_builtin(ao_scheme_v)->func == builtin_set) {
268                                 struct ao_scheme_cons *cons = ao_scheme_poly_cons(ao_scheme_stack->values);
269                                 ao_poly atom = ao_scheme_arg(cons, 1);
270                                 ao_poly val = ao_scheme_arg(cons, 2);
271                                 DBGI("set "); DBG_POLY(atom); DBG(" = "); DBG_POLY(val); DBG("\n");
272                         });
273                 builtin = ao_scheme_poly_builtin(ao_scheme_v);
274                 if (builtin && (builtin->args & AO_SCHEME_FUNC_FREE_ARGS) && !ao_scheme_stack_marked(ao_scheme_stack) && !ao_scheme_skip_cons_free) {
275                         struct ao_scheme_cons *cons = ao_scheme_poly_cons(ao_scheme_stack->values);
276                         ao_scheme_stack->values = AO_SCHEME_NIL;
277                         ao_scheme_cons_free(cons);
278                 }
279
280                 ao_scheme_v = v;
281                 ao_scheme_stack->values = AO_SCHEME_NIL;
282                 ao_scheme_stack->values_tail = AO_SCHEME_NIL;
283                 DBGI(".. result "); DBG_POLY(ao_scheme_v); DBG ("\n");
284                 DBGI(".. frame "); DBG_POLY(ao_scheme_frame_poly(ao_scheme_frame_current)); DBG("\n");
285                 break;
286         case AO_SCHEME_LAMBDA:
287                 DBGI(".. frame "); DBG_POLY(ao_scheme_frame_poly(ao_scheme_frame_current)); DBG("\n");
288                 ao_scheme_stack->state = eval_begin;
289                 v = ao_scheme_lambda_eval();
290                 ao_scheme_stack->sexprs = v;
291                 ao_scheme_stack->values = AO_SCHEME_NIL;
292                 ao_scheme_stack->values_tail = AO_SCHEME_NIL;
293                 DBGI(".. sexprs "); DBG_POLY(ao_scheme_stack->sexprs); DBG("\n");
294                 DBGI(".. frame "); DBG_POLY(ao_scheme_frame_poly(ao_scheme_frame_current)); DBG("\n");
295                 break;
296         case AO_SCHEME_STACK:
297                 DBGI(".. stack "); DBG_POLY(ao_scheme_v); DBG("\n");
298                 ao_scheme_v = ao_scheme_stack_eval();
299                 DBGI(".. value "); DBG_POLY(ao_scheme_v); DBG("\n");
300                 DBGI(".. frame "); DBG_POLY(ao_scheme_frame_poly(ao_scheme_frame_current)); DBG("\n");
301                 break;
302         }
303         ao_scheme_skip_cons_free = 0;
304         return 1;
305 }
306
307 /*
308  * Finish setting up the apply evaluation
309  *
310  * The value is the list to execute
311  */
312 static int
313 ao_scheme_eval_apply(void)
314 {
315         struct ao_scheme_cons   *cons = ao_scheme_poly_cons(ao_scheme_v);
316         struct ao_scheme_cons   *cdr, *prev;
317
318         /* Glue the arguments into the right shape. That's all but the last
319          * concatenated onto the last
320          */
321         cdr = cons;
322         for (;;) {
323                 prev = cdr;
324                 cdr = ao_scheme_poly_cons(prev->cdr);
325                 if (cdr->cdr == AO_SCHEME_NIL)
326                         break;
327         }
328         DBGI("before mangling: "); DBG_POLY(ao_scheme_v); DBG("\n");
329         prev->cdr = cdr->car;
330         ao_scheme_stack->values = ao_scheme_v;
331         ao_scheme_v = ao_scheme_poly_cons(ao_scheme_stack->values)->car;
332         DBGI("apply: "); DBG_POLY(ao_scheme_stack->values); DBG ("\n");
333         ao_scheme_stack->state = eval_exec;
334         ao_scheme_skip_cons_free = 1;
335         return 1;
336 }
337
338 /*
339  * Start evaluating the next cond clause
340  *
341  * If the list of clauses is empty, then
342  * the result of the cond is nil.
343  *
344  * Otherwise, set the current stack state to 'cond_test' and create a
345  * new stack context to evaluate the test s-expression. Once that's
346  * complete, we'll land in 'cond_test' to finish the clause.
347  */
348 static int
349 ao_scheme_eval_cond(void)
350 {
351         DBGI("cond: "); DBG_POLY(ao_scheme_stack->sexprs); DBG("\n");
352         DBGI(".. frame "); DBG_POLY(ao_scheme_frame_poly(ao_scheme_frame_current)); DBG("\n");
353         DBGI(".. saved frame "); DBG_POLY(ao_scheme_stack->frame); DBG("\n");
354         if (!ao_scheme_stack->sexprs) {
355                 ao_scheme_v = _ao_scheme_bool_false;
356                 ao_scheme_stack->state = eval_val;
357         } else {
358                 ao_scheme_v = ao_scheme_poly_cons(ao_scheme_stack->sexprs)->car;
359                 if (!ao_scheme_v || ao_scheme_poly_type(ao_scheme_v) != AO_SCHEME_CONS) {
360                         ao_scheme_error(AO_SCHEME_INVALID, "invalid cond clause");
361                         return 0;
362                 }
363                 ao_scheme_v = ao_scheme_poly_cons(ao_scheme_v)->car;
364                 if (ao_scheme_v == _ao_scheme_atom_else)
365                         ao_scheme_v = _ao_scheme_bool_true;
366                 ao_scheme_stack->state = eval_cond_test;
367                 if (!ao_scheme_stack_push())
368                         return 0;
369         }
370         return 1;
371 }
372
373 /*
374  * Finish a cond clause.
375  *
376  * Check the value from the test expression, if
377  * non-nil, then set up to evaluate the value expression.
378  *
379  * Otherwise, step to the next clause and go back to the 'cond'
380  * state
381  */
382 static int
383 ao_scheme_eval_cond_test(void)
384 {
385         DBGI("cond_test: "); DBG_POLY(ao_scheme_v); DBG(" sexprs "); DBG_POLY(ao_scheme_stack->sexprs); DBG("\n");
386         DBGI(".. frame "); DBG_POLY(ao_scheme_frame_poly(ao_scheme_frame_current)); DBG("\n");
387         DBGI(".. saved frame "); DBG_POLY(ao_scheme_stack->frame); DBG("\n");
388         if (ao_scheme_v != _ao_scheme_bool_false) {
389                 struct ao_scheme_cons *car = ao_scheme_poly_cons(ao_scheme_poly_cons(ao_scheme_stack->sexprs)->car);
390                 ao_poly c = car->cdr;
391
392                 if (c) {
393                         ao_scheme_stack->state = eval_begin;
394                         ao_scheme_stack->sexprs = c;
395                 } else
396                         ao_scheme_stack->state = eval_val;
397         } else {
398                 ao_scheme_stack->sexprs = ao_scheme_poly_cons(ao_scheme_stack->sexprs)->cdr;
399                 DBGI("next cond: "); DBG_POLY(ao_scheme_stack->sexprs); DBG("\n");
400                 ao_scheme_stack->state = eval_cond;
401         }
402         return 1;
403 }
404
405 /*
406  * Evaluate a list of sexprs, returning the value from the last one.
407  *
408  * ao_scheme_begin records the list in stack->sexprs, so we just need to
409  * walk that list. Set ao_scheme_v to the car of the list and jump to
410  * eval_sexpr. When that's done, it will land in eval_val. For all but
411  * the last, leave a stack frame with eval_begin set so that we come
412  * back here. For the last, don't add a stack frame so that we can
413  * just continue on.
414  */
415 static int
416 ao_scheme_eval_begin(void)
417 {
418         DBGI("begin: "); DBG_POLY(ao_scheme_v); DBG(" sexprs "); DBG_POLY(ao_scheme_stack->sexprs); DBG("\n");
419         DBGI(".. frame "); DBG_POLY(ao_scheme_frame_poly(ao_scheme_frame_current)); DBG("\n");
420         DBGI(".. saved frame "); DBG_POLY(ao_scheme_stack->frame); DBG("\n");
421
422         if (!ao_scheme_stack->sexprs) {
423                 ao_scheme_v = AO_SCHEME_NIL;
424                 ao_scheme_stack->state = eval_val;
425         } else {
426                 ao_scheme_v = ao_scheme_poly_cons(ao_scheme_stack->sexprs)->car;
427                 ao_scheme_stack->sexprs = ao_scheme_poly_cons(ao_scheme_stack->sexprs)->cdr;
428
429                 /* If there are more sexprs to do, then come back here, otherwise
430                  * return the value of the last one by just landing in eval_sexpr
431                  */
432                 if (ao_scheme_stack->sexprs) {
433                         ao_scheme_stack->state = eval_begin;
434                         if (!ao_scheme_stack_push())
435                                 return 0;
436                 }
437                 ao_scheme_stack->state = eval_sexpr;
438         }
439         return 1;
440 }
441
442 /*
443  * Conditionally execute a list of sexprs while the first is true
444  */
445 static int
446 ao_scheme_eval_while(void)
447 {
448         DBGI("while: "); DBG_POLY(ao_scheme_stack->sexprs); DBG("\n");
449         DBGI(".. frame "); DBG_POLY(ao_scheme_frame_poly(ao_scheme_frame_current)); DBG("\n");
450         DBGI(".. saved frame "); DBG_POLY(ao_scheme_stack->frame); DBG("\n");
451
452         ao_scheme_stack->values = ao_scheme_v;
453         if (!ao_scheme_stack->sexprs) {
454                 ao_scheme_v = AO_SCHEME_NIL;
455                 ao_scheme_stack->state = eval_val;
456         } else {
457                 ao_scheme_v = ao_scheme_poly_cons(ao_scheme_stack->sexprs)->car;
458                 ao_scheme_stack->state = eval_while_test;
459                 if (!ao_scheme_stack_push())
460                         return 0;
461         }
462         return 1;
463 }
464
465 /*
466  * Check the while condition, terminate the loop if nil. Otherwise keep going
467  */
468 static int
469 ao_scheme_eval_while_test(void)
470 {
471         DBGI("while_test: "); DBG_POLY(ao_scheme_v); DBG(" sexprs "); DBG_POLY(ao_scheme_stack->sexprs); DBG("\n");
472         DBGI(".. frame "); DBG_POLY(ao_scheme_frame_poly(ao_scheme_frame_current)); DBG("\n");
473         DBGI(".. saved frame "); DBG_POLY(ao_scheme_stack->frame); DBG("\n");
474
475         if (ao_scheme_v != _ao_scheme_bool_false) {
476                 ao_scheme_stack->values = ao_scheme_v;
477                 ao_scheme_v = ao_scheme_poly_cons(ao_scheme_stack->sexprs)->cdr;
478                 ao_scheme_stack->state = eval_while;
479                 if (!ao_scheme_stack_push())
480                         return 0;
481                 ao_scheme_stack->state = eval_begin;
482                 ao_scheme_stack->sexprs = ao_scheme_v;
483         }
484         else
485         {
486                 ao_scheme_stack->state = eval_val;
487                 ao_scheme_v = ao_scheme_stack->values;
488         }
489         return 1;
490 }
491
492 /*
493  * Replace the original sexpr with the macro expansion, then
494  * execute that
495  */
496 static int
497 ao_scheme_eval_macro(void)
498 {
499         DBGI("macro: "); DBG_POLY(ao_scheme_v); DBG(" sexprs "); DBG_POLY(ao_scheme_stack->sexprs); DBG("\n");
500
501         if (ao_scheme_v == AO_SCHEME_NIL)
502                 ao_scheme_abort();
503         if (ao_scheme_poly_type(ao_scheme_v) == AO_SCHEME_CONS) {
504                 *ao_scheme_poly_cons(ao_scheme_stack->sexprs) = *ao_scheme_poly_cons(ao_scheme_v);
505                 ao_scheme_v = ao_scheme_stack->sexprs;
506                 DBGI("sexprs rewritten to: "); DBG_POLY(ao_scheme_v); DBG("\n");
507         }
508         ao_scheme_stack->sexprs = AO_SCHEME_NIL;
509         ao_scheme_stack->state = eval_sexpr;
510         return 1;
511 }
512
513 static int (*const evals[])(void) = {
514         [eval_sexpr] = ao_scheme_eval_sexpr,
515         [eval_val] = ao_scheme_eval_val,
516         [eval_formal] = ao_scheme_eval_formal,
517         [eval_exec] = ao_scheme_eval_exec,
518         [eval_apply] = ao_scheme_eval_apply,
519         [eval_cond] = ao_scheme_eval_cond,
520         [eval_cond_test] = ao_scheme_eval_cond_test,
521         [eval_begin] = ao_scheme_eval_begin,
522         [eval_while] = ao_scheme_eval_while,
523         [eval_while_test] = ao_scheme_eval_while_test,
524         [eval_macro] = ao_scheme_eval_macro,
525 };
526
527 const char * const ao_scheme_state_names[] = {
528         [eval_sexpr] = "sexpr",
529         [eval_val] = "val",
530         [eval_formal] = "formal",
531         [eval_exec] = "exec",
532         [eval_apply] = "apply",
533         [eval_cond] = "cond",
534         [eval_cond_test] = "cond_test",
535         [eval_begin] = "begin",
536         [eval_while] = "while",
537         [eval_while_test] = "while_test",
538         [eval_macro] = "macro",
539 };
540
541 /*
542  * Called at restore time to reset all execution state
543  */
544
545 void
546 ao_scheme_eval_clear_globals(void)
547 {
548         ao_scheme_stack = NULL;
549         ao_scheme_frame_current = NULL;
550         ao_scheme_v = AO_SCHEME_NIL;
551 }
552
553 int
554 ao_scheme_eval_restart(void)
555 {
556         return ao_scheme_stack_push();
557 }
558
559 ao_poly
560 ao_scheme_eval(ao_poly _v)
561 {
562         ao_scheme_v = _v;
563
564         ao_scheme_frame_init();
565
566         if (!ao_scheme_stack_push())
567                 return AO_SCHEME_NIL;
568
569         while (ao_scheme_stack) {
570                 if (!(*evals[ao_scheme_stack->state])() || ao_scheme_exception) {
571                         ao_scheme_stack_clear();
572                         return AO_SCHEME_NIL;
573                 }
574         }
575         DBG_DO(if (ao_scheme_frame_current) {DBGI("frame left as "); DBG_POLY(ao_scheme_frame_poly(ao_scheme_frame_current)); DBG("\n");});
576         ao_scheme_frame_current = NULL;
577         return ao_scheme_v;
578 }