altos/lisp: Split out read debug, add memory validation
[fw/altos] / src / lisp / ao_lisp.h
index 96a7a05f22c760e2255a9cdf79113f16a4c451da..d32e7dcd8da263f8d96377bd8190ded9469a5b22 100644 (file)
@@ -17,6 +17,9 @@
 
 #define DBG_MEM                0
 #define DBG_EVAL       0
+#define DBG_READ       0
+#define DBG_FREE_CONS  0
+#define NDEBUG         1
 
 #include <stdint.h>
 #include <string.h>
@@ -111,8 +114,9 @@ extern uint16_t             ao_lisp_top;
 #define AO_LISP_DIVIDE_BY_ZERO 0x02
 #define AO_LISP_INVALID                0x04
 #define AO_LISP_UNDEFINED      0x08
-#define AO_LISP_EOF            0x10
-#define AO_LISP_EXIT           0x20
+#define AO_LISP_REDEFINED      0x10
+#define AO_LISP_EOF            0x20
+#define AO_LISP_EXIT           0x40
 
 extern uint8_t         ao_lisp_exception;
 
@@ -207,6 +211,8 @@ ao_lisp_bigint_int(uint32_t bi) {
 
 #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
 
@@ -384,6 +390,16 @@ static inline int ao_lisp_poly_type(ao_poly poly) {
        return type;
 }
 
+static inline int
+ao_lisp_is_cons(ao_poly poly) {
+       return (ao_lisp_poly_base_type(poly) == AO_LISP_CONS);
+}
+
+static inline int
+ao_lisp_is_pair(ao_poly poly) {
+       return poly != AO_LISP_NIL && (ao_lisp_poly_base_type(poly) == AO_LISP_CONS);
+}
+
 static inline struct ao_lisp_cons *
 ao_lisp_poly_cons(ao_poly poly)
 {
@@ -517,6 +533,11 @@ ao_lisp_alloc(int size);
 int
 ao_lisp_collect(uint8_t style);
 
+#if DBG_FREE_CONS
+void
+ao_lisp_cons_check(struct ao_lisp_cons *cons);
+#endif
+
 void
 ao_lisp_cons_stash(int id, struct ao_lisp_cons *cons);
 
@@ -627,7 +648,7 @@ struct ao_lisp_atom *
 ao_lisp_atom_intern(char *name);
 
 ao_poly *
-ao_lisp_atom_ref(struct ao_lisp_frame *frame, ao_poly atom);
+ao_lisp_atom_ref(ao_poly atom);
 
 ao_poly
 ao_lisp_atom_get(ao_poly atom);
@@ -635,6 +656,9 @@ ao_lisp_atom_get(ao_poly atom);
 ao_poly
 ao_lisp_atom_set(ao_poly atom, ao_poly val);
 
+ao_poly
+ao_lisp_atom_def(ao_poly atom, ao_poly val);
+
 /* int */
 void
 ao_lisp_int_write(ao_poly i);
@@ -757,12 +781,15 @@ ao_lisp_frame_free(struct ao_lisp_frame *frame);
 void
 ao_lisp_frame_bind(struct ao_lisp_frame *frame, int num, ao_poly atom, ao_poly val);
 
-int
-ao_lisp_frame_add(struct ao_lisp_frame **frame, ao_poly atom, ao_poly val);
+ao_poly
+ao_lisp_frame_add(struct ao_lisp_frame *frame, ao_poly atom, ao_poly val);
 
 void
 ao_lisp_frame_write(ao_poly p);
 
+void
+ao_lisp_frame_init(void);
+
 /* lambda */
 extern const struct ao_lisp_type ao_lisp_lambda_type;
 
@@ -803,6 +830,12 @@ ao_lisp_stack_eval(void);
 
 /* error */
 
+void
+ao_lisp_vprintf(char *format, va_list args);
+
+void
+ao_lisp_printf(char *format, ...);
+
 void
 ao_lisp_error_poly(char *name, ao_poly poly, ao_poly last);
 
@@ -819,7 +852,7 @@ ao_lisp_error(int error, char *format, ...);
 
 /* debugging macros */
 
-#if DBG_EVAL
+#if DBG_EVAL || DBG_READ || DBG_MEM
 #define DBG_CODE       1
 int ao_lisp_stack_depth;
 #define DBG_DO(a)      a
@@ -827,8 +860,8 @@ int ao_lisp_stack_depth;
 #define DBG_IN()       (++ao_lisp_stack_depth)
 #define DBG_OUT()      (--ao_lisp_stack_depth)
 #define DBG_RESET()    (ao_lisp_stack_depth = 0)
-#define DBG(...)       printf(__VA_ARGS__)
-#define DBGI(...)      do { DBG("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
+#define DBG(...)       ao_lisp_printf(__VA_ARGS__)
+#define DBGI(...)      do { printf("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
 #define DBG_CONS(a)    ao_lisp_cons_write(ao_lisp_cons_poly(a))
 #define DBG_POLY(a)    ao_lisp_poly_write(a)
 #define OFFSET(a)      ((a) ? (int) ((uint8_t *) a - ao_lisp_pool) : -1)
@@ -857,6 +890,16 @@ ao_lisp_frames_dump(void)
 #define DBG_FRAMES()
 #endif
 
+#if DBG_READ
+#define RDBGI(...)     DBGI(__VA_ARGS__)
+#define RDBG_IN()      DBG_IN()
+#define RDBG_OUT()     DBG_OUT()
+#else
+#define RDBGI(...)
+#define RDBG_IN()
+#define RDBG_OUT()
+#endif
+
 #define DBG_MEM_START  1
 
 #if DBG_MEM
@@ -864,11 +907,11 @@ ao_lisp_frames_dump(void)
 #include <assert.h>
 extern int dbg_move_depth;
 #define MDBG_DUMP 1
-#define MDBG_OFFSET(a) ((int) ((uint8_t *) (a) - ao_lisp_pool))
+#define MDBG_OFFSET(a) ((a) ? (int) ((uint8_t *) (a) - ao_lisp_pool) : -1)
 
 extern int dbg_mem;
 
-#define MDBG_DO(a)     a
+#define MDBG_DO(a)     DBG_DO(a)
 #define MDBG_MOVE(...) do { if (dbg_mem) { int d; for (d = 0; d < dbg_move_depth; d++) printf ("  "); printf(__VA_ARGS__); } } while (0)
 #define MDBG_MORE(...) do { if (dbg_mem) printf(__VA_ARGS__); } while (0)
 #define MDBG_MOVE_IN() (dbg_move_depth++)