+struct ao_lisp_val {
+ ao_poly atom;
+ ao_poly val;
+};
+
+struct ao_lisp_frame_vals {
+ uint8_t type;
+ uint8_t size;
+ struct ao_lisp_val vals[];
+};
+
+struct ao_lisp_frame {
+ uint8_t type;
+ uint8_t num;
+ ao_poly prev;
+ ao_poly vals;
+};
+
+struct ao_lisp_bool {
+ uint8_t type;
+ uint8_t value;
+ uint16_t pad;
+};
+
+struct ao_lisp_bigint {
+ uint32_t value;
+};
+
+struct ao_lisp_float {
+ uint8_t type;
+ uint8_t pad1;
+ uint16_t pad2;
+ float value;
+};
+
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+static inline uint32_t
+ao_lisp_int_bigint(int32_t i) {
+ return AO_LISP_BIGINT | (i << 8);
+}
+static inline int32_t
+ao_lisp_bigint_int(uint32_t bi) {
+ return (int32_t) bi >> 8;
+}
+#else
+static inline uint32_t
+ao_lisp_int_bigint(int32_t i) {
+ return (uint32_t) (i & 0xffffff) | (AO_LISP_BIGINT << 24);
+}
+static inlint int32_t
+ao_lisp_bigint_int(uint32_t bi) {
+ return (int32_t) (bi << 8) >> 8;
+}
+#endif
+
+#define AO_LISP_MIN_INT (-(1 << (15 - AO_LISP_TYPE_SHIFT)))
+#define AO_LISP_MAX_INT ((1 << (15 - AO_LISP_TYPE_SHIFT)) - 1)
+#define AO_LISP_MIN_BIGINT (-(1 << 24))
+#define AO_LISP_MAX_BIGINT ((1 << 24) - 1)
+
+#define AO_LISP_NOT_INTEGER 0x7fffffff
+
+/* Set on type when the frame escapes the lambda */
+#define AO_LISP_FRAME_MARK 0x80
+#define AO_LISP_FRAME_PRINT 0x40
+
+static inline int ao_lisp_frame_marked(struct ao_lisp_frame *f) {
+ return f->type & AO_LISP_FRAME_MARK;
+}
+
+static inline struct ao_lisp_frame *
+ao_lisp_poly_frame(ao_poly poly) {
+ 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);
+}
+
+static inline struct ao_lisp_frame_vals *
+ao_lisp_poly_frame_vals(ao_poly poly) {
+ return ao_lisp_ref(poly);
+}
+
+static inline ao_poly
+ao_lisp_frame_vals_poly(struct ao_lisp_frame_vals *vals) {
+ return ao_lisp_poly(vals, 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_apply, /* Execute apply */
+ eval_cond, /* Start next cond clause */
+ eval_cond_test, /* Check cond condition */
+ eval_begin, /* Start next begin entry */
+ eval_while, /* Start while condition */
+ eval_while_test, /* Check while condition */
+ eval_macro, /* Finished with macro generation */
+};
+
+struct ao_lisp_stack {
+ uint8_t type; /* 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 */
+};
+
+#define AO_LISP_STACK_MARK 0x80 /* set on type when a reference has been taken */
+#define AO_LISP_STACK_PRINT 0x40 /* stack is being printed */
+
+static inline int ao_lisp_stack_marked(struct ao_lisp_stack *s) {
+ return s->type & AO_LISP_STACK_MARK;
+}
+
+static inline void ao_lisp_stack_mark(struct ao_lisp_stack *s) {
+ s->type |= AO_LISP_STACK_MARK;
+}
+
+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 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)