cleaning things up a bit
[fw/altos] / src / scheme / ao_scheme.h
index 0881721bab58a7e6aa48c91de64dedd496cfb800..d4c9bc05051d15c3222c8ed9564ba71ccb177e68 100644 (file)
@@ -155,10 +155,17 @@ ao_scheme_is_const(ao_poly poly) {
        return poly & AO_SCHEME_CONST;
 }
 
-#define AO_SCHEME_IS_CONST(a)  (ao_scheme_const <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_scheme_const + AO_SCHEME_POOL_CONST)
-#define AO_SCHEME_IS_POOL(a)   (ao_scheme_pool <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_scheme_pool + AO_SCHEME_POOL)
-#define AO_SCHEME_IS_INT(p)    (ao_scheme_poly_base_type(p) == AO_SCHEME_INT)
-#define AO_SCHEME_IS_CONS(p)   (ao_scheme_poly_base_type(p) == AO_SCHEME_CONS)
+static inline int
+ao_scheme_is_const_addr(const void *addr) {
+       const uint8_t *a = addr;
+       return (ao_scheme_const <= a) && (a < ao_scheme_const + AO_SCHEME_POOL_CONST);
+}
+
+static inline int
+ao_scheme_is_pool_addr(const void *addr) {
+       const uint8_t *a = addr;
+       return (ao_scheme_pool <= a) && (a < ao_scheme_pool + AO_SCHEME_POOL);
+}
 
 void *
 ao_scheme_ref(ao_poly poly);
@@ -249,7 +256,6 @@ struct ao_scheme_bigint {
 
 /* Set on type when the frame escapes the lambda */
 #define AO_SCHEME_FRAME_MARK   0x80
-#define AO_SCHEME_FRAME_PRINT  0x40
 
 static inline int ao_scheme_frame_marked(struct ao_scheme_frame *f) {
        return f->type & AO_SCHEME_FRAME_MARK;
@@ -301,7 +307,6 @@ struct ao_scheme_stack {
 };
 
 #define AO_SCHEME_STACK_MARK   0x80    /* set on type when a reference has been taken */
-#define AO_SCHEME_STACK_PRINT  0x40    /* stack is being printed */
 
 static inline int ao_scheme_stack_marked(struct ao_scheme_stack *s) {
        return s->type & AO_SCHEME_STACK_MARK;
@@ -567,15 +572,15 @@ ao_scheme_alloc(int size);
 int
 ao_scheme_print_mark_addr(void *addr);
 
-int
-ao_scheme_print_mark_poly(ao_poly poly);
+void
+ao_scheme_print_clear_addr(void *addr);
 
 /* Notes that printing has started */
 void
 ao_scheme_print_start(void);
 
-/* Notes that printing has ended */
-void
+/* Notes that printing has ended, returns 1 if printing is still happening */
+int
 ao_scheme_print_stop(void);
 
 #define AO_SCHEME_COLLECT_FULL         1
@@ -590,45 +595,79 @@ ao_scheme_cons_check(struct ao_scheme_cons *cons);
 #endif
 
 void
-ao_scheme_cons_stash(int id, struct ao_scheme_cons *cons);
+ao_scheme_poly_stash(ao_poly poly);
 
-struct ao_scheme_cons *
-ao_scheme_cons_fetch(int id);
+ao_poly
+ao_scheme_poly_fetch(void);
 
-void
-ao_scheme_poly_stash(int id, ao_poly poly);
+static inline void
+ao_scheme_cons_stash(struct ao_scheme_cons *cons) {
+       ao_scheme_poly_stash(ao_scheme_cons_poly(cons));
+}
 
-ao_poly
-ao_scheme_poly_fetch(int id);
+static inline struct ao_scheme_cons *
+ao_scheme_cons_fetch(void) {
+       return ao_scheme_poly_cons(ao_scheme_poly_fetch());
+}
 
-void
-ao_scheme_string_stash(int id, struct ao_scheme_string *string);
+static inline void
+ao_scheme_atom_stash(struct ao_scheme_atom *atom) {
+       ao_scheme_poly_stash(ao_scheme_atom_poly(atom));
+}
 
-struct ao_scheme_string *
-ao_scheme_string_fetch(int id);
+static inline struct ao_scheme_atom *
+ao_scheme_atom_fetch(void) {
+       return ao_scheme_poly_atom(ao_scheme_poly_fetch());
+}
+
+static inline void
+ao_scheme_string_stash(struct ao_scheme_string *string) {
+       ao_scheme_poly_stash(ao_scheme_string_poly(string));
+}
+
+static inline struct ao_scheme_string *
+ao_scheme_string_fetch(void) {
+       return ao_scheme_poly_string(ao_scheme_poly_fetch());
+}
+
+#ifdef AO_SCHEME_FEATURE_VECTOR
+static inline void
+ao_scheme_vector_stash(struct ao_scheme_vector *vector) {
+       ao_scheme_poly_stash(ao_scheme_vector_poly(vector));
+}
+
+static inline struct ao_scheme_vector *
+ao_scheme_vector_fetch(void) {
+       return ao_scheme_poly_vector(ao_scheme_poly_fetch());
+}
+#endif
 
 static inline void
-ao_scheme_stack_stash(int id, struct ao_scheme_stack *stack) {
-       ao_scheme_poly_stash(id, ao_scheme_stack_poly(stack));
+ao_scheme_stack_stash(struct ao_scheme_stack *stack) {
+       ao_scheme_poly_stash(ao_scheme_stack_poly(stack));
 }
 
 static inline struct ao_scheme_stack *
-ao_scheme_stack_fetch(int id) {
-       return ao_scheme_poly_stack(ao_scheme_poly_fetch(id));
+ao_scheme_stack_fetch(void) {
+       return ao_scheme_poly_stack(ao_scheme_poly_fetch());
 }
 
-void
-ao_scheme_frame_stash(int id, struct ao_scheme_frame *frame);
+static inline void
+ao_scheme_frame_stash(struct ao_scheme_frame *frame) {
+       ao_scheme_poly_stash(ao_scheme_frame_poly(frame));
+}
 
-struct ao_scheme_frame *
-ao_scheme_frame_fetch(int id);
+static inline struct ao_scheme_frame *
+ao_scheme_frame_fetch(void) {
+       return ao_scheme_poly_frame(ao_scheme_poly_fetch());
+}
 
 /* bool */
 
 extern const struct ao_scheme_type ao_scheme_bool_type;
 
 void
-ao_scheme_bool_write(ao_poly v);
+ao_scheme_bool_write(ao_poly v, bool write);
 
 #ifdef AO_SCHEME_MAKE_CONST
 extern struct ao_scheme_bool   *ao_scheme_true, *ao_scheme_false;
@@ -648,7 +687,7 @@ struct ao_scheme_cons *
 ao_scheme_cons_cdr(struct ao_scheme_cons *cons);
 
 ao_poly
-ao_scheme__cons(ao_poly car, ao_poly cdr);
+ao_scheme_cons(ao_poly car, ao_poly cdr);
 
 extern struct ao_scheme_cons *ao_scheme_cons_free_list;
 
@@ -656,10 +695,7 @@ void
 ao_scheme_cons_free(struct ao_scheme_cons *cons);
 
 void
-ao_scheme_cons_write(ao_poly);
-
-void
-ao_scheme_cons_display(ao_poly);
+ao_scheme_cons_write(ao_poly, bool write);
 
 int
 ao_scheme_cons_length(struct ao_scheme_cons *cons);
@@ -689,10 +725,7 @@ ao_poly
 ao_scheme_string_unpack(struct ao_scheme_string *a);
 
 void
-ao_scheme_string_write(ao_poly s);
-
-void
-ao_scheme_string_display(ao_poly s);
+ao_scheme_string_write(ao_poly s, bool write);
 
 /* atom */
 extern const struct ao_scheme_type ao_scheme_atom_type;
@@ -702,7 +735,7 @@ extern struct ao_scheme_frame       *ao_scheme_frame_global;
 extern struct ao_scheme_frame  *ao_scheme_frame_current;
 
 void
-ao_scheme_atom_write(ao_poly a);
+ao_scheme_atom_write(ao_poly a, bool write);
 
 struct ao_scheme_atom *
 ao_scheme_string_to_atom(struct ao_scheme_string *string);
@@ -724,7 +757,7 @@ ao_scheme_atom_def(ao_poly atom, ao_poly val);
 
 /* int */
 void
-ao_scheme_int_write(ao_poly i);
+ao_scheme_int_write(ao_poly i, bool write);
 
 #ifdef AO_SCHEME_FEATURE_BIGINT
 int32_t
@@ -740,7 +773,7 @@ ao_scheme_integer_typep(uint8_t t)
 }
 
 void
-ao_scheme_bigint_write(ao_poly i);
+ao_scheme_bigint_write(ao_poly i, bool write);
 
 extern const struct ao_scheme_type     ao_scheme_bigint_type;
 
@@ -760,10 +793,7 @@ ao_scheme_integer_typep(uint8_t t)
 /* vector */
 
 void
-ao_scheme_vector_write(ao_poly v);
-
-void
-ao_scheme_vector_display(ao_poly v);
+ao_scheme_vector_write(ao_poly v, bool write);
 
 struct ao_scheme_vector *
 ao_scheme_vector_alloc(uint16_t length, ao_poly fill);
@@ -783,14 +813,10 @@ ao_scheme_vector_to_list(struct ao_scheme_vector *vector);
 extern const struct ao_scheme_type     ao_scheme_vector_type;
 
 /* prim */
-void (*ao_scheme_poly_write_func(ao_poly p))(ao_poly p);
-void (*ao_scheme_poly_display_func(ao_poly p))(ao_poly p);
+void (*ao_scheme_poly_write_func(ao_poly p))(ao_poly p, bool write);
 
 static inline void
-ao_scheme_poly_write(ao_poly p) { (*ao_scheme_poly_write_func(p))(p); }
-
-static inline void
-ao_scheme_poly_display(ao_poly p) { (*ao_scheme_poly_display_func(p))(p); }
+ao_scheme_poly_write(ao_poly p, bool write) { (*ao_scheme_poly_write_func(p))(p, write); }
 
 int
 ao_scheme_poly_mark(ao_poly p, uint8_t note_cons);
@@ -818,7 +844,7 @@ ao_scheme_set_cond(struct ao_scheme_cons *cons);
 extern const struct ao_scheme_type ao_scheme_float_type;
 
 void
-ao_scheme_float_write(ao_poly p);
+ao_scheme_float_write(ao_poly p, bool write);
 
 ao_poly
 ao_scheme_float_get(float value);
@@ -836,7 +862,7 @@ ao_scheme_number_typep(uint8_t t)
 
 /* builtin */
 void
-ao_scheme_builtin_write(ao_poly b);
+ao_scheme_builtin_write(ao_poly b, bool write);
 
 extern const struct ao_scheme_type ao_scheme_builtin_type;
 
@@ -895,7 +921,7 @@ ao_poly
 ao_scheme_frame_add(struct ao_scheme_frame *frame, ao_poly atom, ao_poly val);
 
 void
-ao_scheme_frame_write(ao_poly p);
+ao_scheme_frame_write(ao_poly p, bool write);
 
 void
 ao_scheme_frame_init(void);
@@ -909,7 +935,7 @@ struct ao_scheme_lambda *
 ao_scheme_lambda_new(ao_poly cons);
 
 void
-ao_scheme_lambda_write(ao_poly lambda);
+ao_scheme_lambda_write(ao_poly lambda, bool write);
 
 ao_poly
 ao_scheme_lambda_eval(void);
@@ -920,6 +946,8 @@ extern const struct ao_scheme_type ao_scheme_stack_type;
 extern struct ao_scheme_stack  *ao_scheme_stack;
 extern struct ao_scheme_stack  *ao_scheme_stack_free_list;
 
+extern int                     ao_scheme_frame_print_indent;
+
 void
 ao_scheme_stack_reset(struct ao_scheme_stack *stack);
 
@@ -933,7 +961,7 @@ void
 ao_scheme_stack_clear(void);
 
 void
-ao_scheme_stack_write(ao_poly stack);
+ao_scheme_stack_write(ao_poly stack, bool write);
 
 ao_poly
 ao_scheme_stack_eval(void);
@@ -946,12 +974,6 @@ ao_scheme_vprintf(const char *format, va_list args);
 void
 ao_scheme_printf(const char *format, ...);
 
-void
-ao_scheme_error_poly(const char *name, ao_poly poly, ao_poly last);
-
-void
-ao_scheme_error_frame(int indent, const char *name, struct ao_scheme_frame *frame);
-
 ao_poly
 ao_scheme_error(int error, const char *format, ...);
 
@@ -974,10 +996,10 @@ int ao_scheme_stack_depth;
 #define DBG_RESET()    (ao_scheme_stack_depth = 0)
 #define DBG(...)       ao_scheme_printf(__VA_ARGS__)
 #define DBGI(...)      do { printf("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
-#define DBG_CONS(a)    ao_scheme_cons_write(ao_scheme_cons_poly(a))
-#define DBG_POLY(a)    ao_scheme_poly_write(a)
+#define DBG_CONS(a)    ao_scheme_cons_write(ao_scheme_cons_poly(a), true)
+#define DBG_POLY(a)    ao_scheme_poly_write(a, true)
 #define OFFSET(a)      ((a) ? (int) ((uint8_t *) a - ao_scheme_pool) : -1)
-#define DBG_STACK()    ao_scheme_stack_write(ao_scheme_stack_poly(ao_scheme_stack))
+#define DBG_STACK()    ao_scheme_stack_write(ao_scheme_stack_poly(ao_scheme_stack), true)
 static inline void
 ao_scheme_frames_dump(void)
 {