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