+struct ao_lisp_val {
+ ao_poly atom;
+ ao_poly val;
+};
+
+struct ao_lisp_frame {
+ uint8_t type;
+ uint8_t _num;
+ ao_poly prev;
+ struct ao_lisp_val vals[];
+};
+
+#define AO_LISP_FRAME_NUM_MASK 0x7f
+
+/* Set when the frame escapes the lambda */
+#define AO_LISP_FRAME_MARK 0x80
+
+static inline int ao_lisp_frame_num(struct ao_lisp_frame *f) {
+ if (f->_num == 0xff)
+ ao_lisp_abort();
+ return f->_num & AO_LISP_FRAME_NUM_MASK;
+}
+
+static inline int ao_lisp_frame_marked(struct ao_lisp_frame *f) {
+ if (f->_num == 0xff)
+ ao_lisp_abort();
+ return f->_num & AO_LISP_FRAME_MARK;
+}
+
+static inline struct ao_lisp_frame *
+ao_lisp_poly_frame(ao_poly poly) {
+ struct ao_lisp_frame *frame = ao_lisp_ref(poly);
+ if (frame && frame->_num == 0xff)
+ ao_lisp_abort();
+ return ao_lisp_ref(poly);
+}
+
+static inline ao_poly
+ao_lisp_frame_poly(struct ao_lisp_frame *frame) {
+ return ao_lisp_poly(frame, AO_LISP_OTHER);
+}
+
+enum eval_state {
+ eval_sexpr, /* Evaluate an sexpr */
+ eval_val, /* Value computed */
+ eval_formal, /* Formal computed */
+ eval_exec, /* Start a lambda evaluation */
+ eval_cond, /* Start next cond clause */
+ eval_cond_test, /* Check cond condition */
+ eval_progn, /* Start next progn entry */
+ eval_while, /* Start while condition */
+ eval_while_test, /* Check while condition */
+ eval_macro, /* Finished with macro generation */
+};
+
+struct ao_lisp_stack {
+ uint8_t state; /* enum eval_state */
+ ao_poly prev; /* previous stack frame */
+ ao_poly sexprs; /* expressions to evaluate */
+ ao_poly values; /* values computed */
+ ao_poly values_tail; /* end of the values list for easy appending */
+ ao_poly frame; /* current lookup frame */
+ ao_poly list; /* most recent function call */
+};
+
+static inline struct ao_lisp_stack *
+ao_lisp_poly_stack(ao_poly p)
+{
+ return ao_lisp_ref(p);
+}
+
+static inline ao_poly
+ao_lisp_stack_poly(struct ao_lisp_stack *stack)
+{
+ return ao_lisp_poly(stack, AO_LISP_OTHER);
+}
+
+extern struct ao_lisp_stack *ao_lisp_stack;
+extern struct ao_lisp_stack *ao_lisp_stack_free_list;
+extern ao_poly ao_lisp_v;
+
+#define AO_LISP_FUNC_LAMBDA 0
+#define AO_LISP_FUNC_NLAMBDA 1
+#define AO_LISP_FUNC_MACRO 2
+#define AO_LISP_FUNC_LEXPR 3
+
+#define AO_LISP_FUNC_FREE_ARGS 0x80
+#define AO_LISP_FUNC_MASK 0x7f
+
+#define AO_LISP_FUNC_F_LAMBDA (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_LAMBDA)
+#define AO_LISP_FUNC_F_NLAMBDA (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_NLAMBDA)
+#define AO_LISP_FUNC_F_MACRO (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_MACRO)
+#define AO_LISP_FUNC_F_LEXPR (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_LEXPR)