altos/lisp: working on lexical scoping
[fw/altos] / src / lisp / ao_lisp_eval.c
index 2374fdb2139fadfcaa61bd230fbe01c04c600f67..f41962195ee7bd4227d1ce91e5cbc87138d807ae 100644 (file)
  * General Public License for more details.
  */
 
+#define DBG_EVAL 1
 #include "ao_lisp.h"
+#include <assert.h>
 
-/*
- * Non-recursive eval
- *
- * Plan: walk actuals, construct formals
- *
- * stack >  save  > actuals > actual_1
- *           v         v
- *        formals     .    > actual_2
- */
+static int
+stack_size(void *addr)
+{
+       (void) addr;
+       return sizeof (struct ao_lisp_stack);
+}
+
+static void
+stack_mark(void *addr)
+{
+       struct ao_lisp_stack    *stack = addr;
+       for (;;) {
+               ao_lisp_poly_mark(stack->sexprs, 0);
+               ao_lisp_poly_mark(stack->values, 0);
+               /* no need to mark values_tail */
+               ao_lisp_poly_mark(stack->frame, 0);
+               ao_lisp_poly_mark(stack->macro_frame, 0);
+               stack = ao_lisp_poly_stack(stack->prev);
+               if (ao_lisp_mark_memory(stack, sizeof (struct ao_lisp_stack)))
+                       break;
+       }
+}
 
-static struct ao_lisp_cons     *stack;
-static struct ao_lisp_cons     *actuals;
-static struct ao_lisp_cons     *formals;
-static struct ao_lisp_cons     *formals_tail;
-static uint8_t been_here;
-
-#if 0
-#define DBG(...) printf(__VA_ARGS__)
-#define DBG_CONS(a)    ao_lisp_cons_print(a)
-#define DBG_POLY(a)    ao_lisp_poly_print(a)
-#else
-#define DBG(...)
-#define DBG_CONS(a)
-#define DBG_POLY(a)
-#endif
+static const struct ao_lisp_type ao_lisp_stack_type;
+
+static void
+stack_move(void *addr)
+{
+       struct ao_lisp_stack    *stack = addr;
+
+       while (stack) {
+               void    *prev;
+               int     ret;
+               (void) ao_lisp_poly_move(&stack->sexprs, 0);
+               (void) ao_lisp_poly_move(&stack->values, 0);
+               (void) ao_lisp_poly_move(&stack->values_tail, 0);
+               (void) ao_lisp_poly_move(&stack->frame, 0);
+               (void) ao_lisp_poly_move(&stack->macro_frame, 0);
+               prev = ao_lisp_poly_stack(stack->prev);
+               ret = ao_lisp_move(&ao_lisp_stack_type, &prev);
+               if (prev != ao_lisp_poly_stack(stack->prev))
+                       stack->prev = ao_lisp_stack_poly(prev);
+               if (ret)
+                       break;
+               stack = ao_lisp_poly_stack(stack->prev);
+       }
+}
+
+static const struct ao_lisp_type ao_lisp_stack_type = {
+       .size = stack_size,
+       .mark = stack_mark,
+       .move = stack_move
+};
+
+struct ao_lisp_stack           *ao_lisp_stack;
+ao_poly                                ao_lisp_v;
 
 ao_poly
-ao_lisp_eval(ao_poly v)
+ao_lisp_set_cond(struct ao_lisp_cons *c)
 {
-       struct ao_lisp_cons     *formal;
-       int                     cons = 0;
+       ao_lisp_stack->state = eval_cond;
+       ao_lisp_stack->sexprs = ao_lisp_cons_poly(c);
+       return AO_LISP_NIL;
+}
 
-       if (!been_here) {
-               been_here = 1;
-               ao_lisp_root_add(&ao_lisp_cons_type, &stack);
-               ao_lisp_root_add(&ao_lisp_cons_type, &actuals);
-               ao_lisp_root_add(&ao_lisp_cons_type, &formals);
-               ao_lisp_root_add(&ao_lisp_cons_type, &formals_tail);
+static void
+ao_lisp_stack_reset(struct ao_lisp_stack *stack)
+{
+       stack->state = eval_sexpr;
+       stack->macro = 0;
+       stack->sexprs = AO_LISP_NIL;
+       stack->values = AO_LISP_NIL;
+       stack->values_tail = AO_LISP_NIL;
+}
+
+static void
+ao_lisp_frames_dump(void)
+{
+       struct ao_lisp_stack *s;
+       DBGI(".. current frame: "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
+       for (s = ao_lisp_stack; s; s = ao_lisp_poly_stack(s->prev)) {
+               DBGI(".. stack frame: "); DBG_POLY(s->frame); DBG("\n");
+               DBGI(".. macro frame: "); DBG_POLY(s->frame); DBG("\n");
        }
-       stack = 0;
-       actuals = 0;
-       formals = 0;
-       formals_tail = 0;
-       for (;;) {
+}
 
-       restart:
-               /* Build stack frames for each list */
-               while (ao_lisp_poly_type(v) == AO_LISP_CONS) {
-                       if (v == AO_LISP_NIL)
-                               break;
+static int
+ao_lisp_stack_push(void)
+{
+       DBGI("stack push\n");
+       DBG_IN();
+       struct ao_lisp_stack    *stack = ao_lisp_alloc(sizeof (struct ao_lisp_stack));
+       if (!stack)
+               return 0;
+       stack->prev = ao_lisp_stack_poly(ao_lisp_stack);
+       stack->frame = ao_lisp_frame_poly(ao_lisp_frame_current);
+       stack->list = AO_LISP_NIL;
+       ao_lisp_stack = stack;
+       ao_lisp_stack_reset(stack);
+       ao_lisp_frames_dump();
+       return 1;
+}
 
-                       /* Push existing frame on the stack */
-                       if (cons++) {
-                               struct ao_lisp_cons *frame;
+static void
+ao_lisp_stack_pop(void)
+{
+       if (!ao_lisp_stack)
+               return;
+       ao_lisp_frame_current = ao_lisp_poly_frame(ao_lisp_stack->frame);
+       ao_lisp_stack = ao_lisp_poly_stack(ao_lisp_stack->prev);
+       DBG_OUT();
+       DBGI("stack pop\n");
+       ao_lisp_frames_dump();
+}
+
+static void
+ao_lisp_stack_clear(void)
+{
+       ao_lisp_stack = NULL;
+       ao_lisp_frame_current = NULL;
+       ao_lisp_v = AO_LISP_NIL;
+}
+
+static int
+func_type(ao_poly func)
+{
+       if (func == AO_LISP_NIL)
+               return ao_lisp_error(AO_LISP_INVALID, "func is nil");
+       switch (ao_lisp_poly_type(func)) {
+       case AO_LISP_BUILTIN:
+               return ao_lisp_poly_builtin(func)->args;
+       case AO_LISP_LAMBDA:
+               return ao_lisp_poly_lambda(func)->args;
+       default:
+               ao_lisp_error(AO_LISP_INVALID, "not a func");
+               return -1;
+       }
+}
+
+/*
+ * Flattened eval to avoid stack issues
+ */
+
+/*
+ * Evaluate an s-expression
+ *
+ * For a list, evaluate all of the elements and
+ * then execute the resulting function call.
+ *
+ * Each element of the list is evaluated in
+ * a clean stack context.
+ *
+ * The current stack state is set to 'formal' so that
+ * when the evaluation is complete, the value
+ * will get appended to the values list.
+ *
+ * For other types, compute the value directly.
+ */
 
-                               frame = ao_lisp_cons_cons(ao_lisp_cons_poly(actuals), formals);
-                               stack = ao_lisp_cons_cons(ao_lisp_cons_poly(frame), stack);
+static int
+ao_lisp_eval_sexpr(void)
+{
+       DBGI("sexpr: "); DBG_POLY(ao_lisp_v); DBG("\n");
+       switch (ao_lisp_poly_type(ao_lisp_v)) {
+       case AO_LISP_CONS:
+               if (ao_lisp_v == AO_LISP_NIL) {
+                       if (!ao_lisp_stack->values) {
+                               /*
+                                * empty list evaluates to empty list
+                                */
+                               ao_lisp_v = AO_LISP_NIL;
+                               ao_lisp_stack->state = eval_val;
+                       } else {
+                               /*
+                                * done with arguments, go execute it
+                                */
+                               ao_lisp_v = ao_lisp_poly_cons(ao_lisp_stack->values)->car;
+                               ao_lisp_stack->state = eval_exec;
                        }
-                       actuals = ao_lisp_poly_cons(v);
-                       formals = NULL;
-                       formals_tail = NULL;
-                       v = actuals->car;
-
-                       DBG("start: stack"); DBG_CONS(stack); DBG("\n");
-                       DBG("start: actuals"); DBG_CONS(actuals); DBG("\n");
-                       DBG("start: formals"); DBG_CONS(formals); DBG("\n");
+               } else {
+                       if (!ao_lisp_stack->values)
+                               ao_lisp_stack->list = ao_lisp_v;
+                       /*
+                        * Evaluate another argument and then switch
+                        * to 'formal' to add the value to the values
+                        * list
+                        */
+                       ao_lisp_stack->sexprs = ao_lisp_v;
+                       ao_lisp_stack->state = eval_formal;
+                       if (!ao_lisp_stack_push())
+                               return 0;
+                       /*
+                        * push will reset the state to 'sexpr', which
+                        * will evaluate the expression
+                        */
+                       ao_lisp_v = ao_lisp_poly_cons(ao_lisp_v)->car;
                }
+               break;
+       case AO_LISP_ATOM:
+               DBGI("..frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
+               ao_lisp_v = ao_lisp_atom_get(ao_lisp_v);
+               /* fall through */
+       case AO_LISP_INT:
+       case AO_LISP_STRING:
+       case AO_LISP_BUILTIN:
+       case AO_LISP_LAMBDA:
+               ao_lisp_stack->state = eval_val;
+               break;
+       }
+       DBGI(".. result "); DBG_POLY(ao_lisp_v); DBG("\n");
+       return 1;
+}
 
-               /* Evaluate primitive types */
+/*
+ * A value has been computed.
+ *
+ * If the value was computed from a macro,
+ * then we want to reset the current context
+ * to evaluate the macro result again.
+ *
+ * If not a macro, then pop the stack.
+ * If the stack is empty, we're done.
+ * Otherwise, the stack will contain
+ * the next state.
+ */
 
-               DBG ("actual: "); DBG_POLY(v); DBG("\n");
+static int
+ao_lisp_eval_val(void)
+{
+       DBGI("val: "); DBG_POLY(ao_lisp_v); DBG("\n");
+       if (ao_lisp_stack->macro) {
+               DBGI("..macro %d\n", ao_lisp_stack->macro);
+               DBGI("..current frame "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
+               DBGI("..saved frame   "); DBG_POLY(ao_lisp_stack->frame); DBG("\n");
+               DBGI("..macro frame   "); DBG_POLY(ao_lisp_stack->macro_frame); DBG("\n");
+               DBGI("..sexprs       "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
+               DBGI("..values       "); DBG_POLY(ao_lisp_stack->values); DBG("\n");
+               /*
+                * Re-use the current stack to evaluate
+                * the value from the macro
+                */
+               ao_lisp_stack->state = eval_sexpr;
+//             assert(ao_lisp_stack->frame == ao_lisp_stack->macro_frame);
+               ao_lisp_frame_current = ao_lisp_poly_frame(ao_lisp_stack->macro_frame);
+               ao_lisp_stack->frame = ao_lisp_stack->macro_frame;
+               ao_lisp_stack->macro = 0;
+               ao_lisp_stack->macro_frame = AO_LISP_NIL;
+               ao_lisp_stack->sexprs = AO_LISP_NIL;
+               ao_lisp_stack->values = AO_LISP_NIL;
+               ao_lisp_stack->values_tail = AO_LISP_NIL;
+       } else {
+               /*
+                * Value computed, pop the stack
+                * to figure out what to do with the value
+                */
+               ao_lisp_stack_pop();
+       }
+       DBGI("..state %d\n", ao_lisp_stack ? ao_lisp_stack->state : -1);
+       return 1;
+}
 
-               switch (ao_lisp_poly_type(v)) {
-               case AO_LISP_INT:
-               case AO_LISP_STRING:
-                       break;
-               case AO_LISP_ATOM:
-                       v = ao_lisp_poly_atom(v)->val;
+/*
+ * A formal has been computed.
+ *
+ * If this is the first formal, then
+ * check to see if we've got a lamda/lexpr or
+ * macro/nlambda.
+ *
+ * For lambda/lexpr, go compute another formal.
+ * This will terminate when the sexpr state
+ * sees nil.
+ *
+ * For macro/nlambda, we're done, so move the
+ * sexprs into the values and go execute it.
+ */
+
+static int
+ao_lisp_eval_formal(void)
+{
+       ao_poly formal;
+
+       DBGI("formal: "); DBG_POLY(ao_lisp_v); DBG("\n");
+
+       /* Check what kind of function we've got */
+       if (!ao_lisp_stack->values) {
+               switch (func_type(ao_lisp_v)) {
+               case AO_LISP_FUNC_LAMBDA:
+               case AO_LISP_FUNC_LEXPR:
+                       DBGI(".. lambda or lexpr\n");
                        break;
+               case AO_LISP_FUNC_MACRO:
+                       ao_lisp_stack->macro = 1;
+                       DBGI(".. macro %d\n", ao_lisp_stack->macro);
+                       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");
+                       ao_lisp_stack->macro_frame = ao_lisp_stack->frame;
+                       /* fall through ... */
+               case AO_LISP_FUNC_NLAMBDA:
+                       DBGI(".. nlambda or macro\n");
+                       ao_lisp_stack->values = ao_lisp_stack->sexprs;
+                       ao_lisp_stack->values_tail = AO_LISP_NIL;
+                       ao_lisp_stack->state = eval_exec;
+                       return 1;
+               case -1:
+                       return 0;
                }
+       }
 
-               if (!cons)
-                       break;
+       /* Append formal to list of values */
+       formal = ao_lisp_cons_poly(ao_lisp_cons_cons(ao_lisp_v, NULL));
+       if (!formal)
+               return 0;
 
-               for (;;) {
-                       DBG("add formal: "); DBG_POLY(v); DBG("\n");
-
-                       if (formals == NULL) {
-                               if (ao_lisp_poly_type(v) == AO_LISP_BUILTIN) {
-                                       struct ao_lisp_builtin *b = ao_lisp_poly_builtin(v);
-                                       switch (b->args) {
-                                       case AO_LISP_NLAMBDA:
-                                               v = ao_lisp_func(b)(ao_lisp_poly_cons(actuals->cdr));
-                                               goto done_eval;
-
-                                       case AO_LISP_MACRO:
-                                               v = ao_lisp_func(b)(ao_lisp_poly_cons(actuals->cdr));
-                                               DBG("macro "); DBG_POLY(ao_lisp_cons_poly(actuals));
-                                               DBG(" -> "); DBG_POLY(v);
-                                               DBG("\n");
-                                               if (ao_lisp_poly_type(v) != AO_LISP_CONS) {
-                                                       ao_lisp_exception |= AO_LISP_INVALID;
-                                                       return AO_LISP_NIL;
-                                               }
-
-                                               /* Reset frame to the new list */
-                                               actuals = ao_lisp_poly_cons(v);
-                                               v = actuals->car;
-                                               goto restart;
-                                       }
-                               }
-                       }
+       if (ao_lisp_stack->values_tail)
+               ao_lisp_poly_cons(ao_lisp_stack->values_tail)->cdr = formal;
+       else
+               ao_lisp_stack->values = formal;
+       ao_lisp_stack->values_tail = formal;
 
-                       formal = ao_lisp_cons_cons(v, NULL);
-                       if (formals_tail)
-                               formals_tail->cdr = ao_lisp_cons_poly(formal);
-                       else
-                               formals = formal;
-                       formals_tail = formal;
-                       actuals = ao_lisp_poly_cons(actuals->cdr);
-
-                       DBG("formals: ");
-                       DBG_CONS(formals);
-                       DBG("\n");
-                       DBG("actuals: ");
-                       DBG_CONS(actuals);
-                       DBG("\n");
-
-                       /* Process all of the arguments */
-                       if (actuals) {
-                               v = actuals->car;
-                               break;
-                       }
+       DBGI(".. values "); DBG_POLY(ao_lisp_stack->values); DBG("\n");
+
+       /*
+        * Step to the next argument, if this is last, then
+        * 'sexpr' will end up switching to 'exec'
+        */
+       ao_lisp_v = ao_lisp_poly_cons(ao_lisp_stack->sexprs)->cdr;
 
-                       v = formals->car;
+       ao_lisp_stack->state = eval_sexpr;
 
-                       /* Evaluate the resulting list */
-                       if (ao_lisp_poly_type(v) == AO_LISP_BUILTIN) {
-                               struct ao_lisp_builtin *b = ao_lisp_poly_builtin(v);
+       DBGI(".. "); DBG_POLY(ao_lisp_v); DBG("\n");
+       return 1;
+}
 
-                               v = ao_lisp_func(b) (ao_lisp_poly_cons(formals->cdr));
+/*
+ * 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.
+ */
 
-                               DBG ("eval: ");
-                               DBG_CONS(formals);
-                               DBG(" -> ");
-                               DBG_POLY(v);
-                               DBG ("\n");
-                       } else {
-                               ao_lisp_exception |= AO_LISP_INVALID;
-                       }
-                       if (ao_lisp_exception)
-                               return AO_LISP_NIL;
-               done_eval:
-                       if (--cons) {
-                               struct ao_lisp_cons     *frame;
-
-                               /* Pop the previous frame off the stack */
-                               frame = ao_lisp_poly_cons(stack->car);
-                               actuals = ao_lisp_poly_cons(frame->car);
-                               formals = ao_lisp_poly_cons(frame->cdr);
-                               formals_tail = NULL;
-
-                               /* Recompute the tail of the formals list */
-                               if (formals) {
-                                       for (formal = formals; formal->cdr != AO_LISP_NIL; formal = ao_lisp_poly_cons(formal->cdr));
-                                       formals_tail = formal;
-                               }
-
-                               stack = ao_lisp_poly_cons(stack->cdr);
-                               DBG("stack pop: stack"); DBG_CONS(stack); DBG("\n");
-                               DBG("stack pop: actuals"); DBG_CONS(actuals); DBG("\n");
-                               DBG("stack pop: formals"); DBG_CONS(formals); DBG("\n");
-                       } else {
-                               DBG("done func\n");
-                               break;
-                       }
+static int
+ao_lisp_eval_exec(void)
+{
+       ao_poly v;
+       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;
+               v = ao_lisp_func(ao_lisp_poly_builtin(ao_lisp_v)) (
+                       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");
+                       });
+               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(ao_lisp_poly_lambda(ao_lisp_v),
+                                               ao_lisp_poly_cons(ao_lisp_stack->values));
+               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;
+}
+
+static int
+ao_lisp_eval_lambda_done(void)
+{
+       DBGI("lambda_done: "); DBG_POLY(ao_lisp_v); DBG("\n");
+       DBG_STACK();
+       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;
+               }
+               ao_lisp_v = ao_lisp_poly_cons(ao_lisp_v)->car;
+               ao_lisp_stack->state = eval_cond_test;
+               if (!ao_lisp_stack_push())
+                       return 0;
+               ao_lisp_stack->state = eval_sexpr;
+       }
+       return 1;
+}
+
+/*
+ * Finish a cond clause.
+ *
+ * Check the value from the test expression, if
+ * non-nil, then set up to evaluate the value expression.
+ *
+ * Otherwise, step to the next clause and go back to the 'cond'
+ * state
+ */
+static int
+ao_lisp_eval_cond_test(void)
+{
+       DBGI("cond_test: "); DBG_POLY(ao_lisp_v); DBG(" sexprs "); 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_v) {
+               struct ao_lisp_cons *car = ao_lisp_poly_cons(ao_lisp_poly_cons(ao_lisp_stack->sexprs)->car);
+               struct ao_lisp_cons *c = ao_lisp_poly_cons(car->cdr);
+
+               ao_lisp_stack->state = eval_val;
+               if (c) {
+                       ao_lisp_v = c->car;
+                       if (!ao_lisp_stack_push())
+                               return 0;
+               }
+       } else {
+               ao_lisp_stack->sexprs = ao_lisp_poly_cons(ao_lisp_stack->sexprs)->cdr;
+               DBGI("next cond: "); DBG_POLY(ao_lisp_stack->sexprs); DBG("\n");
+               ao_lisp_stack->state = eval_cond;
+       }
+       return 1;
+}
+
+static int (*const evals[])(void) = {
+       [eval_sexpr] = ao_lisp_eval_sexpr,
+       [eval_val] = ao_lisp_eval_val,
+       [eval_formal] = ao_lisp_eval_formal,
+       [eval_exec] = ao_lisp_eval_exec,
+       [eval_cond] = ao_lisp_eval_cond,
+       [eval_cond_test] = ao_lisp_eval_cond_test,
+};
+
+ao_poly
+ao_lisp_eval(ao_poly _v)
+{
+       static uint8_t been_here;
+
+       ao_lisp_v = _v;
+       if (!been_here) {
+               been_here = 1;
+               ao_lisp_root_add(&ao_lisp_stack_type, &ao_lisp_stack);
+               ao_lisp_root_poly_add(&ao_lisp_v);
+       }
+
+       if (!ao_lisp_stack_push())
+               return AO_LISP_NIL;
+
+       while (ao_lisp_stack) {
+//             DBG_STACK();
+               if (!(*evals[ao_lisp_stack->state])() || ao_lisp_exception) {
+                       ao_lisp_stack_clear();
+                       return AO_LISP_NIL;
                }
-               if (!cons)
-                       break;
        }
-       return v;
+       DBG_DO(if (ao_lisp_frame_current) {DBGI("frame left as "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");});
+       ao_lisp_frame_current = NULL;
+       return ao_lisp_v;
 }