#ifndef _AO_LISP_H_
#define _AO_LISP_H_
-#include <stdlib.h>
-
-#if !defined(AO_LISP_TEST) && !defined(AO_LISP_MAKE_CONST)
-#include <ao.h>
-#define AO_LISP_ALTOS 1
-#define abort() ao_panic(1)
-#endif
-
#include <stdint.h>
#include <string.h>
-#include <stdio.h>
+//#include <stdio.h>
+#include <ao_lisp_os.h>
#ifdef AO_LISP_MAKE_CONST
#define AO_LISP_POOL_CONST 16384
#define _ao_lisp_atom_cdr _atom("cdr")
#define _ao_lisp_atom_cons _atom("cons")
#define _ao_lisp_atom_last _atom("last")
+#define _ao_lisp_atom_length _atom("length")
#define _ao_lisp_atom_cond _atom("cond")
#define _ao_lisp_atom_lambda _atom("lambda")
+#define _ao_lisp_atom_led _atom("led")
+#define _ao_lisp_atom_delay _atom("delay")
+#define _ao_lisp_atom_pack _atom("pack")
+#define _ao_lisp_atom_unpack _atom("unpack")
+#define _ao_lisp_atom_flush _atom("flush")
+#define _ao_lisp_atom_eval _atom("eval")
+#define _ao_lisp_atom_read _atom("read")
+#define _ao_lisp_atom_eof _atom("eof")
#else
#include "ao_lisp_const.h"
#ifndef AO_LISP_POOL
#define AO_LISP_OOM 0x01
#define AO_LISP_DIVIDE_BY_ZERO 0x02
#define AO_LISP_INVALID 0x04
+#define AO_LISP_UNDEFINED 0x08
+#define AO_LISP_EOF 0x10
extern uint8_t ao_lisp_exception;
#define AO_LISP_IS_CONST(a) (ao_lisp_const <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_const + AO_LISP_POOL_CONST)
#define AO_LISP_IS_POOL(a) (ao_lisp_pool <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_pool + AO_LISP_POOL)
-static inline void *
-ao_lisp_ref(ao_poly poly) {
- if (poly == 0xBEEF)
- abort();
- if (poly == AO_LISP_NIL)
- return NULL;
- if (poly & AO_LISP_CONST)
- return (void *) (AO_LISP_CONST_BASE + (poly & AO_LISP_REF_MASK));
- return (void *) (AO_LISP_POOL_BASE + (poly & AO_LISP_REF_MASK));
-}
+void *
+ao_lisp_ref(ao_poly poly);
-static inline ao_poly
-ao_lisp_poly(const void *addr, ao_poly type) {
- const uint8_t *a = addr;
- if (a == NULL)
- return AO_LISP_NIL;
- if (AO_LISP_IS_CONST(a))
- return AO_LISP_CONST | (a - AO_LISP_CONST_BASE) | type;
- return (a - AO_LISP_POOL_BASE) | type;
-}
+ao_poly
+ao_lisp_poly(const void *addr, ao_poly type);
struct ao_lisp_type {
int (*size)(void *addr);
return ao_lisp_poly(frame, AO_LISP_OTHER);
}
-struct ao_lisp_stack {
- ao_poly prev;
- uint8_t state;
- uint8_t macro;
- ao_poly sexprs;
- ao_poly values;
- ao_poly values_tail;
- ao_poly frame;
- ao_poly macro_frame;
- ao_poly list;
-};
-
enum eval_state {
- eval_sexpr,
+ eval_sexpr, /* Evaluate an sexpr */
eval_val,
eval_formal,
eval_exec,
- eval_lambda_done,
eval_cond,
- eval_cond_test
+ eval_cond_test,
+ eval_progn,
+ eval_while,
+ eval_while_test,
+};
+
+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 *
};
enum ao_lisp_builtin_id {
+ builtin_eval,
+ builtin_read,
builtin_lambda,
builtin_lexpr,
builtin_nlambda,
builtin_cdr,
builtin_cons,
builtin_last,
+ builtin_length,
builtin_quote,
builtin_set,
builtin_setq,
builtin_cond,
+ builtin_progn,
+ builtin_while,
builtin_print,
builtin_patom,
builtin_plus,
builtin_greater,
builtin_less_equal,
builtin_greater_equal,
+ builtin_pack,
+ builtin_unpack,
+ builtin_flush,
+ builtin_delay,
+ builtin_led,
_builtin_last
};
typedef ao_poly (*ao_lisp_func_t)(struct ao_lisp_cons *cons);
-extern ao_lisp_func_t ao_lisp_builtins[];
+extern const ao_lisp_func_t ao_lisp_builtins[];
static inline ao_lisp_func_t
ao_lisp_func(struct ao_lisp_builtin *b)
void
ao_lisp_cons_patom(ao_poly);
+int
+ao_lisp_cons_length(struct ao_lisp_cons *cons);
+
/* string */
extern const struct ao_lisp_type ao_lisp_string_type;
char *
ao_lisp_string_cat(char *a, char *b);
+ao_poly
+ao_lisp_string_pack(struct ao_lisp_cons *cons);
+
+ao_poly
+ao_lisp_string_unpack(char *a);
+
void
ao_lisp_string_print(ao_poly s);
struct ao_lisp_frame *
ao_lisp_frame_new(int num);
-struct ao_lisp_frame *
-ao_lisp_frame_add(struct ao_lisp_frame *frame, ao_poly atom, ao_poly val);
+int
+ao_lisp_frame_add(struct ao_lisp_frame **frame, ao_poly atom, ao_poly val);
void
ao_lisp_frame_print(ao_poly p);
ao_lisp_macro(struct ao_lisp_cons *cons);
ao_poly
-ao_lisp_lambda_eval(struct ao_lisp_lambda *lambda,
- struct ao_lisp_cons *cons);
+ao_lisp_lambda_eval(void);
/* error */
#define DBG_POLY(a) ao_lisp_poly_print(a)
#define OFFSET(a) ((a) ? (int) ((uint8_t *) a - ao_lisp_pool) : -1)
#define DBG_STACK() ao_lisp_stack_print()
+static inline 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");
+ }
+}
+#define DBG_FRAMES() ao_lisp_frames_dump()
#else
#define DBG_DO(a)
#define DBG_INDENT()
#define DBG_POLY(a)
#define DBG_RESET()
#define DBG_STACK()
+#define DBG_FRAMES()
#endif
#endif /* _AO_LISP_H_ */