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.
18 struct ao_lisp_stack *ao_lisp_stack;
20 uint8_t ao_lisp_skip_cons_free;
23 ao_lisp_set_cond(struct ao_lisp_cons *c)
25 ao_lisp_stack->state = eval_cond;
26 ao_lisp_stack->sexprs = ao_lisp_cons_poly(c);
31 func_type(ao_poly func)
33 if (func == AO_LISP_NIL)
34 return ao_lisp_error(AO_LISP_INVALID, "func is nil");
35 switch (ao_lisp_poly_type(func)) {
37 return ao_lisp_poly_builtin(func)->args & AO_LISP_FUNC_MASK;
39 return ao_lisp_poly_lambda(func)->args;
41 return AO_LISP_FUNC_LAMBDA;
43 ao_lisp_error(AO_LISP_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_lisp_eval_sexpr(void)
71 DBGI("sexpr: "); DBG_POLY(ao_lisp_v); DBG("\n");
72 switch (ao_lisp_poly_type(ao_lisp_v)) {
74 if (ao_lisp_v == AO_LISP_NIL) {
75 if (!ao_lisp_stack->values) {
77 * empty list evaluates to empty list
79 ao_lisp_v = AO_LISP_NIL;
80 ao_lisp_stack->state = eval_val;
83 * done with arguments, go execute it
85 ao_lisp_v = ao_lisp_poly_cons(ao_lisp_stack->values)->car;
86 ao_lisp_stack->state = eval_exec;
89 if (!ao_lisp_stack->values)
90 ao_lisp_stack->list = ao_lisp_v;
92 * Evaluate another argument and then switch
93 * to 'formal' to add the value to the values
96 ao_lisp_stack->sexprs = ao_lisp_v;
97 ao_lisp_stack->state = eval_formal;
98 if (!ao_lisp_stack_push())
101 * push will reset the state to 'sexpr', which
102 * will evaluate the expression
104 ao_lisp_v = ao_lisp_poly_cons(ao_lisp_v)->car;
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);
114 case AO_LISP_BUILTIN:
116 ao_lisp_stack->state = eval_val;
119 DBGI(".. result "); DBG_POLY(ao_lisp_v); DBG("\n");
124 * A value has been computed.
126 * If the value was computed from a macro,
127 * then we want to reset the current context
128 * to evaluate the macro result again.
130 * If not a macro, then pop the stack.
131 * If the stack is empty, we're done.
132 * Otherwise, the stack will contain
137 ao_lisp_eval_val(void)
139 DBGI("val: "); DBG_POLY(ao_lisp_v); DBG("\n");
141 * Value computed, pop the stack
142 * to figure out what to do with the value
145 DBGI("..state %d\n", ao_lisp_stack ? ao_lisp_stack->state : -1);
150 * A formal has been computed.
152 * If this is the first formal, then check to see if we've got a
153 * lamda/lexpr or macro/nlambda.
155 * For lambda/lexpr, go compute another formal. This will terminate
156 * when the sexpr state sees nil.
158 * For macro/nlambda, we're done, so move the sexprs into the values
161 * Macros have an additional step of saving a stack frame holding the
162 * macro value execution context, which then gets the result of the
167 ao_lisp_eval_formal(void)
170 struct ao_lisp_stack *prev;
172 DBGI("formal: "); DBG_POLY(ao_lisp_v); DBG("\n");
174 /* Check what kind of function we've got */
175 if (!ao_lisp_stack->values) {
176 switch (func_type(ao_lisp_v)) {
177 case AO_LISP_FUNC_LAMBDA:
178 case AO_LISP_FUNC_LEXPR:
179 DBGI(".. lambda or lexpr\n");
181 case AO_LISP_FUNC_MACRO:
182 /* Evaluate the result once more */
183 ao_lisp_stack->state = eval_macro;
184 if (!ao_lisp_stack_push())
187 /* After the function returns, take that
188 * value and re-evaluate it
190 prev = ao_lisp_poly_stack(ao_lisp_stack->prev);
191 ao_lisp_stack->sexprs = prev->sexprs;
193 DBGI(".. start macro\n");
194 DBGI(".. sexprs "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
195 DBGI(".. values "); DBG_POLY(ao_lisp_stack->values); DBG("\n");
198 /* fall through ... */
199 case AO_LISP_FUNC_NLAMBDA:
200 DBGI(".. nlambda or macro\n");
202 /* use the raw sexprs as values */
203 ao_lisp_stack->values = ao_lisp_stack->sexprs;
204 ao_lisp_stack->values_tail = AO_LISP_NIL;
205 ao_lisp_stack->state = eval_exec;
207 /* ready to execute now */
214 /* Append formal to list of values */
215 formal = ao_lisp__cons(ao_lisp_v, AO_LISP_NIL);
219 if (ao_lisp_stack->values_tail)
220 ao_lisp_poly_cons(ao_lisp_stack->values_tail)->cdr = formal;
222 ao_lisp_stack->values = formal;
223 ao_lisp_stack->values_tail = formal;
225 DBGI(".. values "); DBG_POLY(ao_lisp_stack->values); DBG("\n");
228 * Step to the next argument, if this is last, then
229 * 'sexpr' will end up switching to 'exec'
231 ao_lisp_v = ao_lisp_poly_cons(ao_lisp_stack->sexprs)->cdr;
233 ao_lisp_stack->state = eval_sexpr;
235 DBGI(".. "); DBG_POLY(ao_lisp_v); DBG("\n");
240 * Start executing a function call
242 * Most builtins are easy, just call the function.
243 * 'cond' is magic; it sticks the list of clauses
244 * in 'sexprs' and switches to 'cond' state. That
245 * bit of magic is done in ao_lisp_set_cond.
247 * Lambdas build a new frame to hold the locals and
248 * then re-use the current stack context to evaluate
249 * the s-expression from the lambda.
253 ao_lisp_eval_exec(void)
256 struct ao_lisp_builtin *builtin;
258 DBGI("exec: "); DBG_POLY(ao_lisp_v); DBG(" values "); DBG_POLY(ao_lisp_stack->values); DBG ("\n");
259 ao_lisp_stack->sexprs = AO_LISP_NIL;
260 switch (ao_lisp_poly_type(ao_lisp_v)) {
261 case AO_LISP_BUILTIN:
262 ao_lisp_stack->state = eval_val;
263 builtin = ao_lisp_poly_builtin(ao_lisp_v);
264 v = ao_lisp_func(builtin) (
265 ao_lisp_poly_cons(ao_lisp_poly_cons(ao_lisp_stack->values)->cdr));
266 DBG_DO(if (!ao_lisp_exception && ao_lisp_poly_builtin(ao_lisp_v)->func == builtin_set) {
267 struct ao_lisp_cons *cons = ao_lisp_poly_cons(ao_lisp_stack->values);
268 ao_poly atom = ao_lisp_arg(cons, 1);
269 ao_poly val = ao_lisp_arg(cons, 2);
270 DBGI("set "); DBG_POLY(atom); DBG(" = "); DBG_POLY(val); DBG("\n");
272 builtin = ao_lisp_poly_builtin(ao_lisp_v);
273 if (builtin->args & AO_LISP_FUNC_FREE_ARGS && !ao_lisp_stack_marked(ao_lisp_stack) && !ao_lisp_skip_cons_free)
274 ao_lisp_cons_free(ao_lisp_poly_cons(ao_lisp_stack->values));
277 ao_lisp_stack->values = AO_LISP_NIL;
278 ao_lisp_stack->values_tail = AO_LISP_NIL;
279 DBGI(".. result "); DBG_POLY(ao_lisp_v); DBG ("\n");
280 DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
283 DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
284 ao_lisp_stack->state = eval_progn;
285 v = ao_lisp_lambda_eval();
286 ao_lisp_stack->sexprs = v;
287 ao_lisp_stack->values = AO_LISP_NIL;
288 ao_lisp_stack->values_tail = AO_LISP_NIL;
289 DBGI(".. sexprs "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
290 DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
293 DBGI(".. stack "); DBG_POLY(ao_lisp_v); DBG("\n");
294 ao_lisp_v = ao_lisp_stack_eval();
295 DBGI(".. value "); DBG_POLY(ao_lisp_v); DBG("\n");
296 DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
299 ao_lisp_skip_cons_free = 0;
304 * Finish setting up the apply evaluation
306 * The value is the list to execute
309 ao_lisp_eval_apply(void)
311 struct ao_lisp_cons *cons = ao_lisp_poly_cons(ao_lisp_v);
312 struct ao_lisp_cons *cdr, *prev;
314 /* Glue the arguments into the right shape. That's all but the last
315 * concatenated onto the last
320 cdr = ao_lisp_poly_cons(prev->cdr);
321 if (cdr->cdr == AO_LISP_NIL)
324 DBGI("before mangling: "); DBG_POLY(ao_lisp_v); DBG("\n");
325 prev->cdr = cdr->car;
326 ao_lisp_stack->values = ao_lisp_v;
327 ao_lisp_v = ao_lisp_poly_cons(ao_lisp_stack->values)->car;
328 DBGI("apply: "); DBG_POLY(ao_lisp_stack->values); DBG ("\n");
329 ao_lisp_stack->state = eval_exec;
330 ao_lisp_skip_cons_free = 1;
335 * Start evaluating the next cond clause
337 * If the list of clauses is empty, then
338 * the result of the cond is nil.
340 * Otherwise, set the current stack state to 'cond_test' and create a
341 * new stack context to evaluate the test s-expression. Once that's
342 * complete, we'll land in 'cond_test' to finish the clause.
345 ao_lisp_eval_cond(void)
347 DBGI("cond: "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
348 DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
349 DBGI(".. saved frame "); DBG_POLY(ao_lisp_stack->frame); DBG("\n");
350 if (!ao_lisp_stack->sexprs) {
351 ao_lisp_v = AO_LISP_NIL;
352 ao_lisp_stack->state = eval_val;
354 ao_lisp_v = ao_lisp_poly_cons(ao_lisp_stack->sexprs)->car;
355 if (!ao_lisp_v || ao_lisp_poly_type(ao_lisp_v) != AO_LISP_CONS) {
356 ao_lisp_error(AO_LISP_INVALID, "invalid cond clause");
359 ao_lisp_v = ao_lisp_poly_cons(ao_lisp_v)->car;
360 if (ao_lisp_v == _ao_lisp_atom_else)
361 ao_lisp_v = _ao_lisp_bool_true;
362 ao_lisp_stack->state = eval_cond_test;
363 if (!ao_lisp_stack_push())
370 * Finish a cond clause.
372 * Check the value from the test expression, if
373 * non-nil, then set up to evaluate the value expression.
375 * Otherwise, step to the next clause and go back to the 'cond'
379 ao_lisp_eval_cond_test(void)
381 DBGI("cond_test: "); DBG_POLY(ao_lisp_v); DBG(" sexprs "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
382 DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
383 DBGI(".. saved frame "); DBG_POLY(ao_lisp_stack->frame); DBG("\n");
384 if (ao_lisp_v != _ao_lisp_bool_false) {
385 struct ao_lisp_cons *car = ao_lisp_poly_cons(ao_lisp_poly_cons(ao_lisp_stack->sexprs)->car);
386 ao_poly c = car->cdr;
389 ao_lisp_stack->state = eval_progn;
390 ao_lisp_stack->sexprs = c;
392 ao_lisp_stack->state = eval_val;
394 ao_lisp_stack->sexprs = ao_lisp_poly_cons(ao_lisp_stack->sexprs)->cdr;
395 DBGI("next cond: "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
396 ao_lisp_stack->state = eval_cond;
402 * Evaluate a list of sexprs, returning the value from the last one.
404 * ao_lisp_progn records the list in stack->sexprs, so we just need to
405 * walk that list. Set ao_lisp_v to the car of the list and jump to
406 * eval_sexpr. When that's done, it will land in eval_val. For all but
407 * the last, leave a stack frame with eval_progn set so that we come
408 * back here. For the last, don't add a stack frame so that we can
412 ao_lisp_eval_progn(void)
414 DBGI("progn: "); DBG_POLY(ao_lisp_v); DBG(" sexprs "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
415 DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
416 DBGI(".. saved frame "); DBG_POLY(ao_lisp_stack->frame); DBG("\n");
418 if (!ao_lisp_stack->sexprs) {
419 ao_lisp_v = AO_LISP_NIL;
420 ao_lisp_stack->state = eval_val;
422 ao_lisp_v = ao_lisp_poly_cons(ao_lisp_stack->sexprs)->car;
423 ao_lisp_stack->sexprs = ao_lisp_poly_cons(ao_lisp_stack->sexprs)->cdr;
425 /* If there are more sexprs to do, then come back here, otherwise
426 * return the value of the last one by just landing in eval_sexpr
428 if (ao_lisp_stack->sexprs) {
429 ao_lisp_stack->state = eval_progn;
430 if (!ao_lisp_stack_push())
433 ao_lisp_stack->state = eval_sexpr;
439 * Conditionally execute a list of sexprs while the first is true
442 ao_lisp_eval_while(void)
444 DBGI("while: "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
445 DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
446 DBGI(".. saved frame "); DBG_POLY(ao_lisp_stack->frame); DBG("\n");
448 ao_lisp_stack->values = ao_lisp_v;
449 if (!ao_lisp_stack->sexprs) {
450 ao_lisp_v = AO_LISP_NIL;
451 ao_lisp_stack->state = eval_val;
453 ao_lisp_v = ao_lisp_poly_cons(ao_lisp_stack->sexprs)->car;
454 ao_lisp_stack->state = eval_while_test;
455 if (!ao_lisp_stack_push())
462 * Check the while condition, terminate the loop if nil. Otherwise keep going
465 ao_lisp_eval_while_test(void)
467 DBGI("while_test: "); DBG_POLY(ao_lisp_v); DBG(" sexprs "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
468 DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
469 DBGI(".. saved frame "); DBG_POLY(ao_lisp_stack->frame); DBG("\n");
471 if (ao_lisp_v != _ao_lisp_bool_false) {
472 ao_lisp_stack->values = ao_lisp_v;
473 ao_lisp_v = ao_lisp_poly_cons(ao_lisp_stack->sexprs)->cdr;
474 ao_lisp_stack->state = eval_while;
475 if (!ao_lisp_stack_push())
477 ao_lisp_stack->state = eval_progn;
478 ao_lisp_stack->sexprs = ao_lisp_v;
482 ao_lisp_stack->state = eval_val;
483 ao_lisp_v = ao_lisp_stack->values;
489 * Replace the original sexpr with the macro expansion, then
493 ao_lisp_eval_macro(void)
495 DBGI("macro: "); DBG_POLY(ao_lisp_v); DBG(" sexprs "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
497 if (ao_lisp_v == AO_LISP_NIL)
499 if (ao_lisp_poly_type(ao_lisp_v) == AO_LISP_CONS) {
500 *ao_lisp_poly_cons(ao_lisp_stack->sexprs) = *ao_lisp_poly_cons(ao_lisp_v);
501 ao_lisp_v = ao_lisp_stack->sexprs;
502 DBGI("sexprs rewritten to: "); DBG_POLY(ao_lisp_v); DBG("\n");
504 ao_lisp_stack->sexprs = AO_LISP_NIL;
505 ao_lisp_stack->state = eval_sexpr;
509 static int (*const evals[])(void) = {
510 [eval_sexpr] = ao_lisp_eval_sexpr,
511 [eval_val] = ao_lisp_eval_val,
512 [eval_formal] = ao_lisp_eval_formal,
513 [eval_exec] = ao_lisp_eval_exec,
514 [eval_apply] = ao_lisp_eval_apply,
515 [eval_cond] = ao_lisp_eval_cond,
516 [eval_cond_test] = ao_lisp_eval_cond_test,
517 [eval_progn] = ao_lisp_eval_progn,
518 [eval_while] = ao_lisp_eval_while,
519 [eval_while_test] = ao_lisp_eval_while_test,
520 [eval_macro] = ao_lisp_eval_macro,
523 const char *ao_lisp_state_names[] = {
524 [eval_sexpr] = "sexpr",
526 [eval_formal] = "formal",
527 [eval_exec] = "exec",
528 [eval_apply] = "apply",
529 [eval_cond] = "cond",
530 [eval_cond_test] = "cond_test",
531 [eval_progn] = "progn",
532 [eval_while] = "while",
533 [eval_while_test] = "while_test",
534 [eval_macro] = "macro",
538 * Called at restore time to reset all execution state
542 ao_lisp_eval_clear_globals(void)
544 ao_lisp_stack = NULL;
545 ao_lisp_frame_current = NULL;
546 ao_lisp_v = AO_LISP_NIL;
550 ao_lisp_eval_restart(void)
552 return ao_lisp_stack_push();
556 ao_lisp_eval(ao_poly _v)
560 if (!ao_lisp_stack_push())
563 while (ao_lisp_stack) {
564 if (!(*evals[ao_lisp_stack->state])() || ao_lisp_exception) {
565 ao_lisp_stack_clear();
569 DBG_DO(if (ao_lisp_frame_current) {DBGI("frame left as "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");});
570 ao_lisp_frame_current = NULL;