- break;
- case eval_exec:
- v = ao_lisp_poly_cons(stack->formals)->car;
- case eval_exec_direct:
- DBGI("exec: "); DBG_POLY(v); DBG(" formals "); DBG_POLY(stack->formals); DBG ("\n");
- if (ao_lisp_poly_type(v) == AO_LISP_BUILTIN) {
- struct ao_lisp_builtin *b = ao_lisp_poly_builtin(v);
-
- v = ao_lisp_func(b) (ao_lisp_poly_cons(ao_lisp_poly_cons(stack->formals)->cdr));
- DBGI("builtin result:"); DBG_POLY(v); DBG ("\n");
- if (ao_lisp_exception) {
- ao_lisp_stack_clear();
- return AO_LISP_NIL;
- }
- stack->state = eval_val;
- break;
- } else {
- v = ao_lisp_lambda(ao_lisp_poly_cons(stack->formals));
- ao_lisp_stack_reset(stack);
- }
- break;
+/*
+ * Start executing a function call
+ *
+ * Most builtins are easy, just call the function.
+ * 'cond' is magic; it sticks the list of clauses
+ * in 'sexprs' and switches to 'cond' state. That
+ * bit of magic is done in ao_lisp_set_cond.
+ *
+ * Lambdas build a new frame to hold the locals and
+ * then re-use the current stack context to evaluate
+ * the s-expression from the lambda.
+ */
+
+static int
+ao_lisp_eval_exec(void)
+{
+ ao_poly v;
+ struct ao_lisp_builtin *builtin;
+
+ DBGI("exec: "); DBG_POLY(ao_lisp_v); DBG(" values "); DBG_POLY(ao_lisp_stack->values); DBG ("\n");
+ ao_lisp_stack->sexprs = AO_LISP_NIL;
+ switch (ao_lisp_poly_type(ao_lisp_v)) {
+ case AO_LISP_BUILTIN:
+ ao_lisp_stack->state = eval_val;
+ builtin = ao_lisp_poly_builtin(ao_lisp_v);
+ v = ao_lisp_func(builtin) (
+ ao_lisp_poly_cons(ao_lisp_poly_cons(ao_lisp_stack->values)->cdr));
+ DBG_DO(if (!ao_lisp_exception && ao_lisp_poly_builtin(ao_lisp_v)->func == builtin_set) {
+ struct ao_lisp_cons *cons = ao_lisp_poly_cons(ao_lisp_stack->values);
+ ao_poly atom = ao_lisp_arg(cons, 1);
+ ao_poly val = ao_lisp_arg(cons, 2);
+ DBGI("set "); DBG_POLY(atom); DBG(" = "); DBG_POLY(val); DBG("\n");
+ });
+ builtin = ao_lisp_poly_builtin(ao_lisp_v);
+ if (builtin->args & AO_LISP_FUNC_FREE_ARGS)
+ ao_lisp_cons_free(ao_lisp_poly_cons(ao_lisp_stack->values));
+
+ ao_lisp_v = v;
+ DBGI(".. result "); DBG_POLY(ao_lisp_v); DBG ("\n");
+ DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
+ break;
+ case AO_LISP_LAMBDA:
+ ao_lisp_stack->state = eval_sexpr;
+ DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
+ ao_lisp_v = ao_lisp_lambda_eval();
+ DBGI(".. sexpr "); DBG_POLY(ao_lisp_v); DBG("\n");
+ DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
+ break;
+ }
+ ao_lisp_stack->values = AO_LISP_NIL;
+ ao_lisp_stack->values_tail = AO_LISP_NIL;
+ return 1;
+}
+
+/*
+ * Start evaluating the next cond clause
+ *
+ * If the list of clauses is empty, then
+ * the result of the cond is nil.
+ *
+ * Otherwise, set the current stack state to 'cond_test' and create a
+ * new stack context to evaluate the test s-expression. Once that's
+ * complete, we'll land in 'cond_test' to finish the clause.
+ */
+static int
+ao_lisp_eval_cond(void)
+{
+ DBGI("cond: "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
+ DBGI(".. frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
+ DBGI(".. saved frame "); DBG_POLY(ao_lisp_stack->frame); DBG("\n");
+ if (!ao_lisp_stack->sexprs) {
+ ao_lisp_v = AO_LISP_NIL;
+ ao_lisp_stack->state = eval_val;
+ } else {
+ ao_lisp_v = ao_lisp_poly_cons(ao_lisp_stack->sexprs)->car;
+ if (!ao_lisp_v || ao_lisp_poly_type(ao_lisp_v) != AO_LISP_CONS) {
+ ao_lisp_error(AO_LISP_INVALID, "invalid cond clause");
+ return 0;