2 * Copyright © 2016 Keith Packard <keithp@keithp.com>
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.
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.
15 #include "ao_scheme.h"
18 struct ao_scheme_stack *ao_scheme_stack;
20 uint8_t ao_scheme_skip_cons_free;
23 ao_scheme_set_cond(struct ao_scheme_cons *c)
25 ao_scheme_stack->state = eval_cond;
26 ao_scheme_stack->sexprs = ao_scheme_cons_poly(c);
31 func_type(ao_poly func)
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;
41 return AO_SCHEME_FUNC_LAMBDA;
43 ao_scheme_error(AO_SCHEME_INVALID, "not a func");
49 * Flattened eval to avoid stack issues
53 * Evaluate an s-expression
55 * For a list, evaluate all of the elements and
56 * then execute the resulting function call.
58 * Each element of the list is evaluated in
59 * a clean stack context.
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.
65 * For other types, compute the value directly.
69 ao_scheme_eval_sexpr(void)
71 DBGI("sexpr: %v\n", ao_scheme_v);
72 switch (ao_scheme_poly_type(ao_scheme_v)) {
74 if (ao_scheme_v == AO_SCHEME_NIL) {
75 if (!ao_scheme_stack->values) {
77 * empty list evaluates to empty list
79 ao_scheme_v = AO_SCHEME_NIL;
80 ao_scheme_stack->state = eval_val;
83 * done with arguments, go execute it
85 ao_scheme_v = ao_scheme_poly_cons(ao_scheme_stack->values)->car;
86 ao_scheme_stack->state = eval_exec;
89 if (!ao_scheme_stack->values)
90 ao_scheme_stack->list = ao_scheme_v;
92 * Evaluate another argument and then switch
93 * to 'formal' to add the value to the values
96 ao_scheme_stack->sexprs = ao_scheme_v;
97 ao_scheme_stack->state = eval_formal;
98 if (!ao_scheme_stack_push())
101 * push will reset the state to 'sexpr', which
102 * will evaluate the expression
104 ao_scheme_v = ao_scheme_poly_cons(ao_scheme_v)->car;
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);
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;
121 DBGI(".. result "); DBG_POLY(ao_scheme_v); DBG("\n");
126 * A value has been computed.
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.
132 * If not a macro, then pop the stack.
133 * If the stack is empty, we're done.
134 * Otherwise, the stack will contain
139 ao_scheme_eval_val(void)
141 DBGI("val: "); DBG_POLY(ao_scheme_v); DBG("\n");
143 * Value computed, pop the stack
144 * to figure out what to do with the value
146 ao_scheme_stack_pop();
147 DBGI("..state %d\n", ao_scheme_stack ? ao_scheme_stack->state : -1);
152 * A formal has been computed.
154 * If this is the first formal, then check to see if we've got a
155 * lamda, macro or nlambda.
157 * For lambda, go compute another formal. This will terminate
158 * when the sexpr state sees nil.
160 * For macro/nlambda, we're done, so move the sexprs into the values
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
169 ao_scheme_eval_formal(void)
172 struct ao_scheme_stack *prev;
174 DBGI("formal: "); DBG_POLY(ao_scheme_v); DBG("\n");
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:
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())
188 /* After the function returns, take that
189 * value and re-evaluate it
191 prev = ao_scheme_poly_stack(ao_scheme_stack->prev);
192 ao_scheme_stack->sexprs = prev->sexprs;
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");
199 /* fall through ... */
200 case AO_SCHEME_FUNC_NLAMBDA:
201 DBGI(".. nlambda or macro\n");
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;
208 /* ready to execute now */
215 /* Append formal to list of values */
216 formal = ao_scheme__cons(ao_scheme_v, AO_SCHEME_NIL);
220 if (ao_scheme_stack->values_tail)
221 ao_scheme_poly_cons(ao_scheme_stack->values_tail)->cdr = formal;
223 ao_scheme_stack->values = formal;
224 ao_scheme_stack->values_tail = formal;
226 DBGI(".. values "); DBG_POLY(ao_scheme_stack->values); DBG("\n");
229 * Step to the next argument, if this is last, then
230 * 'sexpr' will end up switching to 'exec'
232 ao_scheme_v = ao_scheme_poly_cons(ao_scheme_stack->sexprs)->cdr;
234 ao_scheme_stack->state = eval_sexpr;
236 DBGI(".. "); DBG_POLY(ao_scheme_v); DBG("\n");
241 * Start executing a function call
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.
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.
254 ao_scheme_eval_exec(void)
257 struct ao_scheme_builtin *builtin;
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");
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);
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");
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");
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");
303 ao_scheme_skip_cons_free = 0;
308 * Finish setting up the apply evaluation
310 * The value is the list to execute
313 ao_scheme_eval_apply(void)
315 struct ao_scheme_cons *cons = ao_scheme_poly_cons(ao_scheme_v);
316 struct ao_scheme_cons *cdr, *prev;
318 /* Glue the arguments into the right shape. That's all but the last
319 * concatenated onto the last
324 cdr = ao_scheme_poly_cons(prev->cdr);
325 if (cdr->cdr == AO_SCHEME_NIL)
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;
339 * Start evaluating the next cond clause
341 * If the list of clauses is empty, then
342 * the result of the cond is nil.
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.
349 ao_scheme_eval_cond(void)
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;
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");
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())
374 * Finish a cond clause.
376 * Check the value from the test expression, if
377 * non-nil, then set up to evaluate the value expression.
379 * Otherwise, step to the next clause and go back to the 'cond'
383 ao_scheme_eval_cond_test(void)
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;
393 ao_scheme_stack->state = eval_begin;
394 ao_scheme_stack->sexprs = c;
396 ao_scheme_stack->state = eval_val;
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;
406 * Evaluate a list of sexprs, returning the value from the last one.
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
416 ao_scheme_eval_begin(void)
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");
422 if (!ao_scheme_stack->sexprs) {
423 ao_scheme_v = AO_SCHEME_NIL;
424 ao_scheme_stack->state = eval_val;
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;
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
432 if (ao_scheme_stack->sexprs) {
433 ao_scheme_stack->state = eval_begin;
434 if (!ao_scheme_stack_push())
437 ao_scheme_stack->state = eval_sexpr;
443 * Conditionally execute a list of sexprs while the first is true
446 ao_scheme_eval_while(void)
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");
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;
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())
466 * Check the while condition, terminate the loop if nil. Otherwise keep going
469 ao_scheme_eval_while_test(void)
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");
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())
481 ao_scheme_stack->state = eval_begin;
482 ao_scheme_stack->sexprs = ao_scheme_v;
486 ao_scheme_stack->state = eval_val;
487 ao_scheme_v = ao_scheme_stack->values;
493 * Replace the original sexpr with the macro expansion, then
497 ao_scheme_eval_macro(void)
499 DBGI("macro: "); DBG_POLY(ao_scheme_v); DBG(" sexprs "); DBG_POLY(ao_scheme_stack->sexprs); DBG("\n");
501 if (ao_scheme_v == AO_SCHEME_NIL)
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");
508 ao_scheme_stack->sexprs = AO_SCHEME_NIL;
509 ao_scheme_stack->state = eval_sexpr;
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,
527 const char * const ao_scheme_state_names[] = {
528 [eval_sexpr] = "sexpr",
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",
542 * Called at restore time to reset all execution state
546 ao_scheme_eval_clear_globals(void)
548 ao_scheme_stack = NULL;
549 ao_scheme_frame_current = NULL;
550 ao_scheme_v = AO_SCHEME_NIL;
554 ao_scheme_eval_restart(void)
556 return ao_scheme_stack_push();
560 ao_scheme_eval(ao_poly _v)
564 ao_scheme_frame_init();
566 if (!ao_scheme_stack_push())
567 return AO_SCHEME_NIL;
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;
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;