2 * Copyright © 2016 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
21 #define DBG_FREE_CONS 0
26 #define AO_SCHEME_BUILTIN_FEATURES
27 #include "ao_scheme_builtin.h"
28 #undef AO_SCHEME_BUILTIN_FEATURES
29 #include <ao_scheme_os.h>
34 typedef uint16_t ao_poly;
35 typedef int16_t ao_signed_poly;
39 struct ao_scheme_os_save {
42 uint16_t const_checksum;
43 uint16_t const_checksum_inv;
46 #ifndef AO_SCHEME_POOL_TOTAL
47 #error Must define AO_SCHEME_POOL_TOTAL for AO_SCHEME_SAVE
50 #define AO_SCHEME_POOL_EXTRA (sizeof(struct ao_scheme_os_save))
51 #define AO_SCHEME_POOL ((int) (AO_SCHEME_POOL_TOTAL - AO_SCHEME_POOL_EXTRA))
54 ao_scheme_os_save(void);
57 ao_scheme_os_restore_save(struct ao_scheme_os_save *save, int offset);
60 ao_scheme_os_restore(void);
64 #ifdef AO_SCHEME_MAKE_CONST
65 #define AO_SCHEME_POOL_CONST 16384
66 extern uint8_t ao_scheme_const[AO_SCHEME_POOL_CONST] __attribute__((aligned(4)));
67 #define ao_scheme_pool ao_scheme_const
68 #define AO_SCHEME_POOL AO_SCHEME_POOL_CONST
70 #define _atom(n) ao_scheme_atom_poly(ao_scheme_atom_intern((char *) n))
71 #define _bool(v) ao_scheme_bool_poly(ao_scheme_bool_get(v))
73 #define _ao_scheme_bool_true _bool(1)
74 #define _ao_scheme_bool_false _bool(0)
76 #define _ao_scheme_atom_eof _atom("eof")
77 #define _ao_scheme_atom_else _atom("else")
79 #define AO_SCHEME_BUILTIN_ATOMS
80 #include "ao_scheme_builtin.h"
83 #include "ao_scheme_const.h"
84 #ifndef AO_SCHEME_POOL
85 #error Must define AO_SCHEME_POOL
87 #ifndef AO_SCHEME_POOL_EXTRA
88 #define AO_SCHEME_POOL_EXTRA 0
90 extern uint8_t ao_scheme_pool[AO_SCHEME_POOL + AO_SCHEME_POOL_EXTRA] __attribute__((aligned(4)));
94 #define AO_SCHEME_CONS 0
95 #define AO_SCHEME_INT 1
96 #define AO_SCHEME_STRING 2
97 #define AO_SCHEME_OTHER 3
99 #define AO_SCHEME_TYPE_MASK 0x0003
100 #define AO_SCHEME_TYPE_SHIFT 2
101 #define AO_SCHEME_REF_MASK 0x7ffc
102 #define AO_SCHEME_CONST 0x8000
104 /* These have a type value at the start of the struct */
105 #define AO_SCHEME_ATOM 4
106 #define AO_SCHEME_BUILTIN 5
107 #define AO_SCHEME_FRAME 6
108 #define AO_SCHEME_FRAME_VALS 7
109 #define AO_SCHEME_LAMBDA 8
110 #define AO_SCHEME_STACK 9
111 #define AO_SCHEME_BOOL 10
112 #ifdef AO_SCHEME_FEATURE_BIGINT
113 #define AO_SCHEME_BIGINT 11
114 #define _AO_SCHEME_BIGINT AO_SCHEME_BIGINT
116 #define _AO_SCHEME_BIGINT AO_SCHEME_BOOL
118 #ifdef AO_SCHEME_FEATURE_FLOAT
119 #define AO_SCHEME_FLOAT (_AO_SCHEME_BIGINT + 1)
120 #define _AO_SCHEME_FLOAT AO_SCHEME_FLOAT
122 #define _AO_SCHEME_FLOAT _AO_SCHEME_BIGINT
124 #ifdef AO_SCHEME_FEATURE_VECTOR
125 #define AO_SCHEME_VECTOR 13
126 #define _AO_SCHEME_VECTOR AO_SCHEME_VECTOR
128 #define _AO_SCHEME_VECTOR _AO_SCHEME_FLOAT
130 #define AO_SCHEME_NUM_TYPE (_AO_SCHEME_VECTOR+1)
132 /* Leave two bits for types to use as they please */
133 #define AO_SCHEME_OTHER_TYPE_MASK 0x3f
135 #define AO_SCHEME_NIL 0
137 extern uint16_t ao_scheme_top;
139 #define AO_SCHEME_OOM 0x01
140 #define AO_SCHEME_DIVIDE_BY_ZERO 0x02
141 #define AO_SCHEME_INVALID 0x04
142 #define AO_SCHEME_UNDEFINED 0x08
143 #define AO_SCHEME_REDEFINED 0x10
144 #define AO_SCHEME_EOF 0x20
145 #define AO_SCHEME_EXIT 0x40
147 extern uint8_t ao_scheme_exception;
150 ao_scheme_is_const(ao_poly poly) {
151 return poly & AO_SCHEME_CONST;
154 #define AO_SCHEME_IS_CONST(a) (ao_scheme_const <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_scheme_const + AO_SCHEME_POOL_CONST)
155 #define AO_SCHEME_IS_POOL(a) (ao_scheme_pool <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_scheme_pool + AO_SCHEME_POOL)
156 #define AO_SCHEME_IS_INT(p) (ao_scheme_poly_base_type(p) == AO_SCHEME_INT)
159 ao_scheme_ref(ao_poly poly);
162 ao_scheme_poly(const void *addr, ao_poly type);
164 struct ao_scheme_type {
165 int (*size)(void *addr);
166 void (*mark)(void *addr);
167 void (*move)(void *addr);
171 struct ao_scheme_cons {
176 struct ao_scheme_atom {
183 struct ao_scheme_val {
188 struct ao_scheme_frame_vals {
191 struct ao_scheme_val vals[];
194 struct ao_scheme_frame {
201 struct ao_scheme_bool {
208 #ifdef AO_SCHEME_FEATURE_FLOAT
209 struct ao_scheme_float {
217 #ifdef AO_SCHEME_FEATURE_VECTOR
218 struct ao_scheme_vector {
226 #define AO_SCHEME_MIN_INT (-(1 << (15 - AO_SCHEME_TYPE_SHIFT)))
227 #define AO_SCHEME_MAX_INT ((1 << (15 - AO_SCHEME_TYPE_SHIFT)) - 1)
229 #ifdef AO_SCHEME_FEATURE_BIGINT
230 struct ao_scheme_bigint {
234 #define AO_SCHEME_MIN_BIGINT (-(1 << 24))
235 #define AO_SCHEME_MAX_BIGINT ((1 << 24) - 1)
237 #if __BYTE_ORDER == __LITTLE_ENDIAN
239 static inline uint32_t
240 ao_scheme_int_bigint(int32_t i) {
241 return AO_SCHEME_BIGINT | (i << 8);
243 static inline int32_t
244 ao_scheme_bigint_int(uint32_t bi) {
245 return (int32_t) bi >> 8;
248 static inline uint32_t
249 ao_scheme_int_bigint(int32_t i) {
250 return (uint32_t) (i & 0xffffff) | (AO_SCHEME_BIGINT << 24);
252 static inlint int32_t
253 ao_scheme_bigint_int(uint32_t bi) {
254 return (int32_t) (bi << 8) >> 8;
257 #endif /* __BYTE_ORDER */
258 #endif /* AO_SCHEME_FEATURE_BIGINT */
260 #define AO_SCHEME_NOT_INTEGER 0x7fffffff
262 /* Set on type when the frame escapes the lambda */
263 #define AO_SCHEME_FRAME_MARK 0x80
264 #define AO_SCHEME_FRAME_PRINT 0x40
266 static inline int ao_scheme_frame_marked(struct ao_scheme_frame *f) {
267 return f->type & AO_SCHEME_FRAME_MARK;
270 static inline struct ao_scheme_frame *
271 ao_scheme_poly_frame(ao_poly poly) {
272 return ao_scheme_ref(poly);
275 static inline ao_poly
276 ao_scheme_frame_poly(struct ao_scheme_frame *frame) {
277 return ao_scheme_poly(frame, AO_SCHEME_OTHER);
280 static inline struct ao_scheme_frame_vals *
281 ao_scheme_poly_frame_vals(ao_poly poly) {
282 return ao_scheme_ref(poly);
285 static inline ao_poly
286 ao_scheme_frame_vals_poly(struct ao_scheme_frame_vals *vals) {
287 return ao_scheme_poly(vals, AO_SCHEME_OTHER);
291 eval_sexpr, /* Evaluate an sexpr */
292 eval_val, /* Value computed */
293 eval_formal, /* Formal computed */
294 eval_exec, /* Start a lambda evaluation */
295 eval_apply, /* Execute apply */
296 eval_cond, /* Start next cond clause */
297 eval_cond_test, /* Check cond condition */
298 eval_begin, /* Start next begin entry */
299 eval_while, /* Start while condition */
300 eval_while_test, /* Check while condition */
301 eval_macro, /* Finished with macro generation */
304 struct ao_scheme_stack {
305 uint8_t type; /* AO_SCHEME_STACK */
306 uint8_t state; /* enum eval_state */
307 ao_poly prev; /* previous stack frame */
308 ao_poly sexprs; /* expressions to evaluate */
309 ao_poly values; /* values computed */
310 ao_poly values_tail; /* end of the values list for easy appending */
311 ao_poly frame; /* current lookup frame */
312 ao_poly list; /* most recent function call */
315 #define AO_SCHEME_STACK_MARK 0x80 /* set on type when a reference has been taken */
316 #define AO_SCHEME_STACK_PRINT 0x40 /* stack is being printed */
318 static inline int ao_scheme_stack_marked(struct ao_scheme_stack *s) {
319 return s->type & AO_SCHEME_STACK_MARK;
322 static inline void ao_scheme_stack_mark(struct ao_scheme_stack *s) {
323 s->type |= AO_SCHEME_STACK_MARK;
326 static inline struct ao_scheme_stack *
327 ao_scheme_poly_stack(ao_poly p)
329 return ao_scheme_ref(p);
332 static inline ao_poly
333 ao_scheme_stack_poly(struct ao_scheme_stack *stack)
335 return ao_scheme_poly(stack, AO_SCHEME_OTHER);
338 extern ao_poly ao_scheme_v;
340 #define AO_SCHEME_FUNC_LAMBDA 0
341 #define AO_SCHEME_FUNC_NLAMBDA 1
342 #define AO_SCHEME_FUNC_MACRO 2
344 #define AO_SCHEME_FUNC_FREE_ARGS 0x80
345 #define AO_SCHEME_FUNC_MASK 0x7f
347 #define AO_SCHEME_FUNC_F_LAMBDA (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_LAMBDA)
348 #define AO_SCHEME_FUNC_F_NLAMBDA (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_NLAMBDA)
349 #define AO_SCHEME_FUNC_F_MACRO (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_MACRO)
351 struct ao_scheme_builtin {
357 #define AO_SCHEME_BUILTIN_ID
358 #include "ao_scheme_builtin.h"
360 typedef ao_poly (*ao_scheme_func_t)(struct ao_scheme_cons *cons);
362 extern const ao_scheme_func_t ao_scheme_builtins[];
364 static inline ao_scheme_func_t
365 ao_scheme_func(struct ao_scheme_builtin *b)
367 return ao_scheme_builtins[b->func];
370 struct ao_scheme_lambda {
377 static inline struct ao_scheme_lambda *
378 ao_scheme_poly_lambda(ao_poly poly)
380 return ao_scheme_ref(poly);
383 static inline ao_poly
384 ao_scheme_lambda_poly(struct ao_scheme_lambda *lambda)
386 return ao_scheme_poly(lambda, AO_SCHEME_OTHER);
390 ao_scheme_poly_other(ao_poly poly) {
391 return ao_scheme_ref(poly);
394 static inline uint8_t
395 ao_scheme_other_type(void *other) {
397 if ((*((uint8_t *) other) & AO_SCHEME_OTHER_TYPE_MASK) >= AO_SCHEME_NUM_TYPE)
400 return *((uint8_t *) other) & AO_SCHEME_OTHER_TYPE_MASK;
403 static inline ao_poly
404 ao_scheme_other_poly(const void *other)
406 return ao_scheme_poly(other, AO_SCHEME_OTHER);
410 ao_scheme_size_round(int size)
412 return (size + 3) & ~3;
416 ao_scheme_size(const struct ao_scheme_type *type, void *addr)
418 return ao_scheme_size_round(type->size(addr));
421 #define AO_SCHEME_OTHER_POLY(other) ((ao_poly)(other) + AO_SCHEME_OTHER)
423 static inline int ao_scheme_poly_base_type(ao_poly poly) {
424 return poly & AO_SCHEME_TYPE_MASK;
427 static inline int ao_scheme_poly_type(ao_poly poly) {
428 int type = poly & AO_SCHEME_TYPE_MASK;
429 if (type == AO_SCHEME_OTHER)
430 return ao_scheme_other_type(ao_scheme_poly_other(poly));
435 ao_scheme_is_cons(ao_poly poly) {
436 return (ao_scheme_poly_base_type(poly) == AO_SCHEME_CONS);
440 ao_scheme_is_pair(ao_poly poly) {
441 return poly != AO_SCHEME_NIL && (ao_scheme_poly_base_type(poly) == AO_SCHEME_CONS);
444 static inline struct ao_scheme_cons *
445 ao_scheme_poly_cons(ao_poly poly)
447 return ao_scheme_ref(poly);
450 static inline ao_poly
451 ao_scheme_cons_poly(struct ao_scheme_cons *cons)
453 return ao_scheme_poly(cons, AO_SCHEME_CONS);
456 static inline int32_t
457 ao_scheme_poly_int(ao_poly poly)
459 return (int32_t) ((ao_signed_poly) poly >> AO_SCHEME_TYPE_SHIFT);
462 static inline ao_poly
463 ao_scheme_int_poly(int32_t i)
465 return ((ao_poly) i << 2) | AO_SCHEME_INT;
468 #ifdef AO_SCHEME_FEATURE_BIGINT
469 static inline struct ao_scheme_bigint *
470 ao_scheme_poly_bigint(ao_poly poly)
472 return ao_scheme_ref(poly);
475 static inline ao_poly
476 ao_scheme_bigint_poly(struct ao_scheme_bigint *bi)
478 return ao_scheme_poly(bi, AO_SCHEME_OTHER);
480 #endif /* AO_SCHEME_FEATURE_BIGINT */
483 ao_scheme_poly_string(ao_poly poly)
485 return ao_scheme_ref(poly);
488 static inline ao_poly
489 ao_scheme_string_poly(char *s)
491 return ao_scheme_poly(s, AO_SCHEME_STRING);
494 static inline struct ao_scheme_atom *
495 ao_scheme_poly_atom(ao_poly poly)
497 return ao_scheme_ref(poly);
500 static inline ao_poly
501 ao_scheme_atom_poly(struct ao_scheme_atom *a)
503 return ao_scheme_poly(a, AO_SCHEME_OTHER);
506 static inline struct ao_scheme_builtin *
507 ao_scheme_poly_builtin(ao_poly poly)
509 return ao_scheme_ref(poly);
512 static inline ao_poly
513 ao_scheme_builtin_poly(struct ao_scheme_builtin *b)
515 return ao_scheme_poly(b, AO_SCHEME_OTHER);
518 static inline ao_poly
519 ao_scheme_bool_poly(struct ao_scheme_bool *b)
521 return ao_scheme_poly(b, AO_SCHEME_OTHER);
524 static inline struct ao_scheme_bool *
525 ao_scheme_poly_bool(ao_poly poly)
527 return ao_scheme_ref(poly);
530 #ifdef AO_SCHEME_FEATURE_FLOAT
531 static inline ao_poly
532 ao_scheme_float_poly(struct ao_scheme_float *f)
534 return ao_scheme_poly(f, AO_SCHEME_OTHER);
537 static inline struct ao_scheme_float *
538 ao_scheme_poly_float(ao_poly poly)
540 return ao_scheme_ref(poly);
544 ao_scheme_poly_number(ao_poly p);
547 #ifdef AO_SCHEME_FEATURE_VECTOR
548 static inline ao_poly
549 ao_scheme_vector_poly(struct ao_scheme_vector *v)
551 return ao_scheme_poly(v, AO_SCHEME_OTHER);
554 static inline struct ao_scheme_vector *
555 ao_scheme_poly_vector(ao_poly poly)
557 return ao_scheme_ref(poly);
561 /* memory functions */
563 extern uint64_t ao_scheme_collects[2];
564 extern uint64_t ao_scheme_freed[2];
565 extern uint64_t ao_scheme_loops[2];
567 /* returns 1 if the object was already marked */
569 ao_scheme_mark_memory(const struct ao_scheme_type *type, void *addr);
571 /* returns 1 if the object was already moved */
573 ao_scheme_move_memory(const struct ao_scheme_type *type, void **ref);
576 ao_scheme_alloc(int size);
578 #define AO_SCHEME_COLLECT_FULL 1
579 #define AO_SCHEME_COLLECT_INCREMENTAL 0
582 ao_scheme_collect(uint8_t style);
586 ao_scheme_cons_check(struct ao_scheme_cons *cons);
590 ao_scheme_cons_stash(int id, struct ao_scheme_cons *cons);
592 struct ao_scheme_cons *
593 ao_scheme_cons_fetch(int id);
596 ao_scheme_poly_stash(int id, ao_poly poly);
599 ao_scheme_poly_fetch(int id);
602 ao_scheme_string_stash(int id, char *string);
605 ao_scheme_string_fetch(int id);
608 ao_scheme_stack_stash(int id, struct ao_scheme_stack *stack) {
609 ao_scheme_poly_stash(id, ao_scheme_stack_poly(stack));
612 static inline struct ao_scheme_stack *
613 ao_scheme_stack_fetch(int id) {
614 return ao_scheme_poly_stack(ao_scheme_poly_fetch(id));
618 ao_scheme_frame_stash(int id, struct ao_scheme_frame *frame);
620 struct ao_scheme_frame *
621 ao_scheme_frame_fetch(int id);
625 extern const struct ao_scheme_type ao_scheme_bool_type;
628 ao_scheme_bool_write(ao_poly v);
630 #ifdef AO_SCHEME_MAKE_CONST
631 extern struct ao_scheme_bool *ao_scheme_true, *ao_scheme_false;
633 struct ao_scheme_bool *
634 ao_scheme_bool_get(uint8_t value);
638 extern const struct ao_scheme_type ao_scheme_cons_type;
640 struct ao_scheme_cons *
641 ao_scheme_cons_cons(ao_poly car, ao_poly cdr);
643 /* Return a cons or NULL for a proper list, else error */
644 struct ao_scheme_cons *
645 ao_scheme_cons_cdr(struct ao_scheme_cons *cons);
648 ao_scheme__cons(ao_poly car, ao_poly cdr);
650 extern struct ao_scheme_cons *ao_scheme_cons_free_list;
653 ao_scheme_cons_free(struct ao_scheme_cons *cons);
656 ao_scheme_cons_write(ao_poly);
659 ao_scheme_cons_display(ao_poly);
662 ao_scheme_cons_length(struct ao_scheme_cons *cons);
664 struct ao_scheme_cons *
665 ao_scheme_cons_copy(struct ao_scheme_cons *cons);
668 extern const struct ao_scheme_type ao_scheme_string_type;
671 ao_scheme_string_copy(char *a);
674 ao_scheme_string_cat(char *a, char *b);
677 ao_scheme_string_pack(struct ao_scheme_cons *cons);
680 ao_scheme_string_unpack(char *a);
683 ao_scheme_string_write(ao_poly s);
686 ao_scheme_string_display(ao_poly s);
689 extern const struct ao_scheme_type ao_scheme_atom_type;
691 extern struct ao_scheme_atom *ao_scheme_atoms;
692 extern struct ao_scheme_frame *ao_scheme_frame_global;
693 extern struct ao_scheme_frame *ao_scheme_frame_current;
696 ao_scheme_atom_write(ao_poly a);
698 struct ao_scheme_atom *
699 ao_scheme_atom_intern(char *name);
702 ao_scheme_atom_ref(ao_poly atom, struct ao_scheme_frame **frame_ref);
705 ao_scheme_atom_get(ao_poly atom);
708 ao_scheme_atom_set(ao_poly atom, ao_poly val);
711 ao_scheme_atom_def(ao_poly atom, ao_poly val);
715 ao_scheme_int_write(ao_poly i);
717 #ifdef AO_SCHEME_FEATURE_BIGINT
719 ao_scheme_poly_integer(ao_poly p);
722 ao_scheme_integer_poly(int32_t i);
725 ao_scheme_integer_typep(uint8_t t)
727 return (t == AO_SCHEME_INT) || (t == AO_SCHEME_BIGINT);
731 ao_scheme_bigint_write(ao_poly i);
733 extern const struct ao_scheme_type ao_scheme_bigint_type;
737 #define ao_scheme_poly_integer ao_scheme_poly_int
738 #define ao_scheme_integer_poly ao_scheme_int_poly
741 ao_scheme_integer_typep(uint8_t t)
743 return (t == AO_SCHEME_INT);
746 #endif /* AO_SCHEME_FEATURE_BIGINT */
751 ao_scheme_vector_write(ao_poly v);
754 ao_scheme_vector_display(ao_poly v);
756 struct ao_scheme_vector *
757 ao_scheme_vector_alloc(uint16_t length, ao_poly fill);
760 ao_scheme_vector_get(ao_poly v, ao_poly i);
763 ao_scheme_vector_set(ao_poly v, ao_poly i, ao_poly p);
765 struct ao_scheme_vector *
766 ao_scheme_list_to_vector(struct ao_scheme_cons *cons);
768 struct ao_scheme_cons *
769 ao_scheme_vector_to_list(struct ao_scheme_vector *vector);
771 extern const struct ao_scheme_type ao_scheme_vector_type;
774 void (*ao_scheme_poly_write_func(ao_poly p))(ao_poly p);
775 void (*ao_scheme_poly_display_func(ao_poly p))(ao_poly p);
778 ao_scheme_poly_write(ao_poly p) { (*ao_scheme_poly_write_func(p))(p); }
781 ao_scheme_poly_display(ao_poly p) { (*ao_scheme_poly_display_func(p))(p); }
784 ao_scheme_poly_mark(ao_poly p, uint8_t note_cons);
786 /* returns 1 if the object has already been moved */
788 ao_scheme_poly_move(ao_poly *p, uint8_t note_cons);
793 ao_scheme_eval_clear_globals(void);
796 ao_scheme_eval_restart(void);
799 ao_scheme_eval(ao_poly p);
802 ao_scheme_set_cond(struct ao_scheme_cons *cons);
805 #ifdef AO_SCHEME_FEATURE_FLOAT
806 extern const struct ao_scheme_type ao_scheme_float_type;
809 ao_scheme_float_write(ao_poly p);
812 ao_scheme_float_get(float value);
815 #ifdef AO_SCHEME_FEATURE_FLOAT
816 static inline uint8_t
817 ao_scheme_number_typep(uint8_t t)
819 return ao_scheme_integer_typep(t) || (t == AO_SCHEME_FLOAT);
822 #define ao_scheme_number_typep ao_scheme_integer_typep
827 ao_scheme_builtin_write(ao_poly b);
829 extern const struct ao_scheme_type ao_scheme_builtin_type;
831 /* Check argument count */
833 ao_scheme_check_argc(ao_poly name, struct ao_scheme_cons *cons, int min, int max);
835 /* Check argument type */
837 ao_scheme_check_argt(ao_poly name, struct ao_scheme_cons *cons, int argc, int type, int nil_ok);
839 /* Fetch an arg (nil if off the end) */
841 ao_scheme_arg(struct ao_scheme_cons *cons, int argc);
844 ao_scheme_args_name(uint8_t args);
847 extern int ao_scheme_read_list;
848 extern struct ao_scheme_cons *ao_scheme_read_cons;
849 extern struct ao_scheme_cons *ao_scheme_read_cons_tail;
850 extern struct ao_scheme_cons *ao_scheme_read_stack;
853 ao_scheme_read(void);
857 ao_scheme_read_eval_print(void);
860 extern const struct ao_scheme_type ao_scheme_frame_type;
861 extern const struct ao_scheme_type ao_scheme_frame_vals_type;
863 #define AO_SCHEME_FRAME_FREE 6
865 extern struct ao_scheme_frame *ao_scheme_frame_free_list[AO_SCHEME_FRAME_FREE];
868 ao_scheme_frame_mark(struct ao_scheme_frame *frame);
871 ao_scheme_frame_ref(struct ao_scheme_frame *frame, ao_poly atom);
873 struct ao_scheme_frame *
874 ao_scheme_frame_new(int num);
877 ao_scheme_frame_free(struct ao_scheme_frame *frame);
880 ao_scheme_frame_bind(struct ao_scheme_frame *frame, int num, ao_poly atom, ao_poly val);
883 ao_scheme_frame_add(struct ao_scheme_frame *frame, ao_poly atom, ao_poly val);
886 ao_scheme_frame_write(ao_poly p);
889 ao_scheme_frame_init(void);
892 extern const struct ao_scheme_type ao_scheme_lambda_type;
894 extern const char * const ao_scheme_state_names[];
896 struct ao_scheme_lambda *
897 ao_scheme_lambda_new(ao_poly cons);
900 ao_scheme_lambda_write(ao_poly lambda);
903 ao_scheme_lambda_eval(void);
907 extern const struct ao_scheme_type ao_scheme_stack_type;
908 extern struct ao_scheme_stack *ao_scheme_stack;
909 extern struct ao_scheme_stack *ao_scheme_stack_free_list;
912 ao_scheme_stack_reset(struct ao_scheme_stack *stack);
915 ao_scheme_stack_push(void);
918 ao_scheme_stack_pop(void);
921 ao_scheme_stack_clear(void);
924 ao_scheme_stack_write(ao_poly stack);
927 ao_scheme_stack_eval(void);
932 ao_scheme_vprintf(const char *format, va_list args);
935 ao_scheme_printf(const char *format, ...);
938 ao_scheme_error_poly(const char *name, ao_poly poly, ao_poly last);
941 ao_scheme_error_frame(int indent, const char *name, struct ao_scheme_frame *frame);
944 ao_scheme_error(int error, const char *format, ...);
948 #define AO_SCHEME_BUILTIN_DECLS
949 #include "ao_scheme_builtin.h"
951 /* debugging macros */
953 #if DBG_EVAL || DBG_READ || DBG_MEM
955 int ao_scheme_stack_depth;
957 #define DBG_INDENT() do { int _s; for(_s = 0; _s < ao_scheme_stack_depth; _s++) printf(" "); } while(0)
958 #define DBG_IN() (++ao_scheme_stack_depth)
959 #define DBG_OUT() (--ao_scheme_stack_depth)
960 #define DBG_RESET() (ao_scheme_stack_depth = 0)
961 #define DBG(...) ao_scheme_printf(__VA_ARGS__)
962 #define DBGI(...) do { printf("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
963 #define DBG_CONS(a) ao_scheme_cons_write(ao_scheme_cons_poly(a))
964 #define DBG_POLY(a) ao_scheme_poly_write(a)
965 #define OFFSET(a) ((a) ? (int) ((uint8_t *) a - ao_scheme_pool) : -1)
966 #define DBG_STACK() ao_scheme_stack_write(ao_scheme_stack_poly(ao_scheme_stack))
968 ao_scheme_frames_dump(void)
970 struct ao_scheme_stack *s;
971 DBGI(".. current frame: "); DBG_POLY(ao_scheme_frame_poly(ao_scheme_frame_current)); DBG("\n");
972 for (s = ao_scheme_stack; s; s = ao_scheme_poly_stack(s->prev)) {
973 DBGI(".. stack frame: "); DBG_POLY(s->frame); DBG("\n");
976 #define DBG_FRAMES() ao_scheme_frames_dump()
992 #define RDBGI(...) DBGI(__VA_ARGS__)
993 #define RDBG_IN() DBG_IN()
994 #define RDBG_OUT() DBG_OUT()
1001 #define DBG_MEM_START 1
1006 extern int dbg_move_depth;
1008 #define MDBG_OFFSET(a) ((a) ? (int) ((uint8_t *) (a) - ao_scheme_pool) : -1)
1012 #define MDBG_DO(a) DBG_DO(a)
1013 #define MDBG_MOVE(...) do { if (dbg_mem) { int d; for (d = 0; d < dbg_move_depth; d++) printf (" "); printf(__VA_ARGS__); } } while (0)
1014 #define MDBG_MORE(...) do { if (dbg_mem) printf(__VA_ARGS__); } while (0)
1015 #define MDBG_MOVE_IN() (dbg_move_depth++)
1016 #define MDBG_MOVE_OUT() (assert(--dbg_move_depth >= 0))
1021 #define MDBG_MOVE(...)
1022 #define MDBG_MORE(...)
1023 #define MDBG_MOVE_IN()
1024 #define MDBG_MOVE_OUT()
1028 #endif /* _AO_SCHEME_H_ */