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