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.
28 #define DBG_FREE_CONS 0
35 #define AO_SCHEME_BUILTIN_FEATURES
36 #include "ao_scheme_builtin.h"
37 #undef AO_SCHEME_BUILTIN_FEATURES
38 #include <ao_scheme_os.h>
43 typedef uint16_t ao_poly;
44 typedef int16_t ao_signed_poly;
48 struct ao_scheme_os_save {
51 uint16_t const_checksum;
52 uint16_t const_checksum_inv;
55 #ifndef AO_SCHEME_POOL_TOTAL
56 #error Must define AO_SCHEME_POOL_TOTAL for AO_SCHEME_SAVE
59 #define AO_SCHEME_POOL_EXTRA (sizeof(struct ao_scheme_os_save))
60 #define AO_SCHEME_POOL ((int) (AO_SCHEME_POOL_TOTAL - AO_SCHEME_POOL_EXTRA))
63 ao_scheme_os_save(void);
66 ao_scheme_os_restore_save(struct ao_scheme_os_save *save, int offset);
69 ao_scheme_os_restore(void);
73 #ifdef AO_SCHEME_MAKE_CONST
74 #define AO_SCHEME_POOL_CONST 16384
75 extern uint8_t ao_scheme_const[AO_SCHEME_POOL_CONST] __attribute__((aligned(4)));
76 #define ao_scheme_pool ao_scheme_const
77 #define AO_SCHEME_POOL AO_SCHEME_POOL_CONST
79 #define _atom(n) ao_scheme_atom_poly(ao_scheme_atom_intern((char *) n))
80 #define _bool(v) ao_scheme_bool_poly(ao_scheme_bool_get(v))
82 #define _ao_scheme_bool_true _bool(1)
83 #define _ao_scheme_bool_false _bool(0)
85 #define _ao_scheme_atom_eof _atom("eof")
86 #define _ao_scheme_atom_else _atom("else")
88 #define AO_SCHEME_BUILTIN_ATOMS
89 #include "ao_scheme_builtin.h"
92 #include "ao_scheme_const.h"
93 #ifndef AO_SCHEME_POOL
94 #error Must define AO_SCHEME_POOL
96 #ifndef AO_SCHEME_POOL_EXTRA
97 #define AO_SCHEME_POOL_EXTRA 0
99 extern uint8_t ao_scheme_pool[AO_SCHEME_POOL + AO_SCHEME_POOL_EXTRA] __attribute__((aligned(4)));
102 /* Primitive types */
103 #define AO_SCHEME_CONS 0
104 #define AO_SCHEME_INT 1
105 #define AO_SCHEME_BIGINT 2
106 #define AO_SCHEME_OTHER 3
108 #define AO_SCHEME_TYPE_MASK 0x0003
109 #define AO_SCHEME_TYPE_SHIFT 2
110 #define AO_SCHEME_REF_MASK 0x7ffc
111 #define AO_SCHEME_CONST 0x8000
113 /* These have a type value at the start of the struct */
114 #define AO_SCHEME_ATOM 4
115 #define AO_SCHEME_BUILTIN 5
116 #define AO_SCHEME_FRAME 6
117 #define AO_SCHEME_FRAME_VALS 7
118 #define AO_SCHEME_LAMBDA 8
119 #define AO_SCHEME_STACK 9
120 #define AO_SCHEME_BOOL 10
121 #define AO_SCHEME_STRING 11
122 #ifdef AO_SCHEME_FEATURE_FLOAT
123 #define AO_SCHEME_FLOAT 12
124 #define _AO_SCHEME_FLOAT AO_SCHEME_FLOAT
126 #define _AO_SCHEME_FLOAT 12
128 #ifdef AO_SCHEME_FEATURE_VECTOR
129 #define AO_SCHEME_VECTOR 13
130 #define _AO_SCHEME_VECTOR AO_SCHEME_VECTOR
132 #define _AO_SCHEME_VECTOR _AO_SCHEME_FLOAT
134 #define AO_SCHEME_NUM_TYPE (_AO_SCHEME_VECTOR+1)
136 /* Leave two bits for types to use as they please */
137 #define AO_SCHEME_OTHER_TYPE_MASK 0x3f
139 #define AO_SCHEME_NIL 0
141 extern uint16_t ao_scheme_top;
143 #define AO_SCHEME_OOM 0x01
144 #define AO_SCHEME_DIVIDE_BY_ZERO 0x02
145 #define AO_SCHEME_INVALID 0x04
146 #define AO_SCHEME_UNDEFINED 0x08
147 #define AO_SCHEME_REDEFINED 0x10
148 #define AO_SCHEME_EOF 0x20
149 #define AO_SCHEME_EXIT 0x40
151 extern uint8_t ao_scheme_exception;
154 ao_scheme_is_const(ao_poly poly) {
155 return poly & AO_SCHEME_CONST;
158 #define AO_SCHEME_IS_CONST(a) (ao_scheme_const <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_scheme_const + AO_SCHEME_POOL_CONST)
159 #define AO_SCHEME_IS_POOL(a) (ao_scheme_pool <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_scheme_pool + AO_SCHEME_POOL)
160 #define AO_SCHEME_IS_INT(p) (ao_scheme_poly_base_type(p) == AO_SCHEME_INT)
161 #define AO_SCHEME_IS_CONS(p) (ao_scheme_poly_base_type(p) == AO_SCHEME_CONS)
164 ao_scheme_ref(ao_poly poly);
167 ao_scheme_poly(const void *addr, ao_poly type);
169 struct ao_scheme_type {
170 int (*size)(void *addr);
171 void (*mark)(void *addr);
172 void (*move)(void *addr);
176 struct ao_scheme_cons {
181 struct ao_scheme_atom {
188 struct ao_scheme_string {
193 struct ao_scheme_val {
198 struct ao_scheme_frame_vals {
201 struct ao_scheme_val vals[];
204 struct ao_scheme_frame {
211 struct ao_scheme_bool {
218 #ifdef AO_SCHEME_FEATURE_FLOAT
219 struct ao_scheme_float {
227 #ifdef AO_SCHEME_FEATURE_VECTOR
228 struct ao_scheme_vector {
236 #define AO_SCHEME_MIN_INT (-(1 << (15 - AO_SCHEME_TYPE_SHIFT)))
237 #define AO_SCHEME_MAX_INT ((1 << (15 - AO_SCHEME_TYPE_SHIFT)) - 1)
239 #ifdef AO_SCHEME_FEATURE_BIGINT
241 struct ao_scheme_bigint {
245 #define AO_SCHEME_MIN_BIGINT INT32_MIN
246 #define AO_SCHEME_MAX_BIGINT INT32_MAX
248 #endif /* AO_SCHEME_FEATURE_BIGINT */
250 /* Set on type when the frame escapes the lambda */
251 #define AO_SCHEME_FRAME_MARK 0x80
252 #define AO_SCHEME_FRAME_PRINT 0x40
254 static inline int ao_scheme_frame_marked(struct ao_scheme_frame *f) {
255 return f->type & AO_SCHEME_FRAME_MARK;
258 static inline struct ao_scheme_frame *
259 ao_scheme_poly_frame(ao_poly poly) {
260 return ao_scheme_ref(poly);
263 static inline ao_poly
264 ao_scheme_frame_poly(struct ao_scheme_frame *frame) {
265 return ao_scheme_poly(frame, AO_SCHEME_OTHER);
268 static inline struct ao_scheme_frame_vals *
269 ao_scheme_poly_frame_vals(ao_poly poly) {
270 return ao_scheme_ref(poly);
273 static inline ao_poly
274 ao_scheme_frame_vals_poly(struct ao_scheme_frame_vals *vals) {
275 return ao_scheme_poly(vals, AO_SCHEME_OTHER);
279 eval_sexpr, /* Evaluate an sexpr */
280 eval_val, /* Value computed */
281 eval_formal, /* Formal computed */
282 eval_exec, /* Start a lambda evaluation */
283 eval_apply, /* Execute apply */
284 eval_cond, /* Start next cond clause */
285 eval_cond_test, /* Check cond condition */
286 eval_begin, /* Start next begin entry */
287 eval_while, /* Start while condition */
288 eval_while_test, /* Check while condition */
289 eval_macro, /* Finished with macro generation */
292 struct ao_scheme_stack {
293 uint8_t type; /* AO_SCHEME_STACK */
294 uint8_t state; /* enum eval_state */
295 ao_poly prev; /* previous stack frame */
296 ao_poly sexprs; /* expressions to evaluate */
297 ao_poly values; /* values computed */
298 ao_poly values_tail; /* end of the values list for easy appending */
299 ao_poly frame; /* current lookup frame */
300 ao_poly list; /* most recent function call */
303 #define AO_SCHEME_STACK_MARK 0x80 /* set on type when a reference has been taken */
304 #define AO_SCHEME_STACK_PRINT 0x40 /* stack is being printed */
306 static inline int ao_scheme_stack_marked(struct ao_scheme_stack *s) {
307 return s->type & AO_SCHEME_STACK_MARK;
310 static inline void ao_scheme_stack_mark(struct ao_scheme_stack *s) {
311 s->type |= AO_SCHEME_STACK_MARK;
314 static inline struct ao_scheme_stack *
315 ao_scheme_poly_stack(ao_poly p)
317 return ao_scheme_ref(p);
320 static inline ao_poly
321 ao_scheme_stack_poly(struct ao_scheme_stack *stack)
323 return ao_scheme_poly(stack, AO_SCHEME_OTHER);
326 extern ao_poly ao_scheme_v;
328 #define AO_SCHEME_FUNC_LAMBDA 0
329 #define AO_SCHEME_FUNC_NLAMBDA 1
330 #define AO_SCHEME_FUNC_MACRO 2
332 #define AO_SCHEME_FUNC_FREE_ARGS 0x80
333 #define AO_SCHEME_FUNC_MASK 0x7f
335 #define AO_SCHEME_FUNC_F_LAMBDA (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_LAMBDA)
336 #define AO_SCHEME_FUNC_F_NLAMBDA (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_NLAMBDA)
337 #define AO_SCHEME_FUNC_F_MACRO (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_MACRO)
339 struct ao_scheme_builtin {
345 #define AO_SCHEME_BUILTIN_ID
346 #include "ao_scheme_builtin.h"
348 typedef ao_poly (*ao_scheme_func_t)(struct ao_scheme_cons *cons);
350 extern const ao_scheme_func_t ao_scheme_builtins[];
352 static inline ao_scheme_func_t
353 ao_scheme_func(struct ao_scheme_builtin *b)
355 return ao_scheme_builtins[b->func];
358 struct ao_scheme_lambda {
365 static inline struct ao_scheme_lambda *
366 ao_scheme_poly_lambda(ao_poly poly)
368 return ao_scheme_ref(poly);
371 static inline ao_poly
372 ao_scheme_lambda_poly(struct ao_scheme_lambda *lambda)
374 return ao_scheme_poly(lambda, AO_SCHEME_OTHER);
378 ao_scheme_poly_other(ao_poly poly) {
379 return ao_scheme_ref(poly);
382 static inline uint8_t
383 ao_scheme_other_type(void *other) {
385 if ((*((uint8_t *) other) & AO_SCHEME_OTHER_TYPE_MASK) >= AO_SCHEME_NUM_TYPE)
388 return *((uint8_t *) other) & AO_SCHEME_OTHER_TYPE_MASK;
391 static inline ao_poly
392 ao_scheme_other_poly(const void *other)
394 return ao_scheme_poly(other, AO_SCHEME_OTHER);
398 ao_scheme_size_round(int size)
400 return (size + 3) & ~3;
404 ao_scheme_size(const struct ao_scheme_type *type, void *addr)
406 return ao_scheme_size_round(type->size(addr));
409 #define AO_SCHEME_OTHER_POLY(other) ((ao_poly)(other) + AO_SCHEME_OTHER)
411 static inline int ao_scheme_poly_base_type(ao_poly poly) {
412 return poly & AO_SCHEME_TYPE_MASK;
415 static inline int ao_scheme_poly_type(ao_poly poly) {
416 int type = poly & AO_SCHEME_TYPE_MASK;
417 if (type == AO_SCHEME_OTHER)
418 return ao_scheme_other_type(ao_scheme_poly_other(poly));
423 ao_scheme_is_cons(ao_poly poly) {
424 return (ao_scheme_poly_base_type(poly) == AO_SCHEME_CONS);
428 ao_scheme_is_pair(ao_poly poly) {
429 return poly != AO_SCHEME_NIL && (ao_scheme_poly_base_type(poly) == AO_SCHEME_CONS);
432 static inline struct ao_scheme_cons *
433 ao_scheme_poly_cons(ao_poly poly)
435 return ao_scheme_ref(poly);
438 static inline ao_poly
439 ao_scheme_cons_poly(struct ao_scheme_cons *cons)
441 return ao_scheme_poly(cons, AO_SCHEME_CONS);
444 static inline int32_t
445 ao_scheme_poly_int(ao_poly poly)
447 return (int32_t) ((ao_signed_poly) poly >> AO_SCHEME_TYPE_SHIFT);
450 static inline ao_poly
451 ao_scheme_int_poly(int32_t i)
453 return ((ao_poly) i << 2) | AO_SCHEME_INT;
456 #ifdef AO_SCHEME_FEATURE_BIGINT
457 static inline struct ao_scheme_bigint *
458 ao_scheme_poly_bigint(ao_poly poly)
460 return ao_scheme_ref(poly);
463 static inline ao_poly
464 ao_scheme_bigint_poly(struct ao_scheme_bigint *bi)
466 return ao_scheme_poly(bi, AO_SCHEME_BIGINT);
468 #endif /* AO_SCHEME_FEATURE_BIGINT */
470 static inline struct ao_scheme_string *
471 ao_scheme_poly_string(ao_poly poly)
473 return ao_scheme_ref(poly);
476 static inline ao_poly
477 ao_scheme_string_poly(struct ao_scheme_string *s)
479 return ao_scheme_poly(s, AO_SCHEME_OTHER);
482 static inline struct ao_scheme_atom *
483 ao_scheme_poly_atom(ao_poly poly)
485 return ao_scheme_ref(poly);
488 static inline ao_poly
489 ao_scheme_atom_poly(struct ao_scheme_atom *a)
491 return ao_scheme_poly(a, AO_SCHEME_OTHER);
494 static inline struct ao_scheme_builtin *
495 ao_scheme_poly_builtin(ao_poly poly)
497 return ao_scheme_ref(poly);
500 static inline ao_poly
501 ao_scheme_builtin_poly(struct ao_scheme_builtin *b)
503 return ao_scheme_poly(b, AO_SCHEME_OTHER);
506 static inline ao_poly
507 ao_scheme_bool_poly(struct ao_scheme_bool *b)
509 return ao_scheme_poly(b, AO_SCHEME_OTHER);
512 static inline struct ao_scheme_bool *
513 ao_scheme_poly_bool(ao_poly poly)
515 return ao_scheme_ref(poly);
518 #ifdef AO_SCHEME_FEATURE_FLOAT
519 static inline ao_poly
520 ao_scheme_float_poly(struct ao_scheme_float *f)
522 return ao_scheme_poly(f, AO_SCHEME_OTHER);
525 static inline struct ao_scheme_float *
526 ao_scheme_poly_float(ao_poly poly)
528 return ao_scheme_ref(poly);
532 ao_scheme_poly_number(ao_poly p);
535 #ifdef AO_SCHEME_FEATURE_VECTOR
536 static inline ao_poly
537 ao_scheme_vector_poly(struct ao_scheme_vector *v)
539 return ao_scheme_poly(v, AO_SCHEME_OTHER);
542 static inline struct ao_scheme_vector *
543 ao_scheme_poly_vector(ao_poly poly)
545 return ao_scheme_ref(poly);
549 /* memory functions */
551 extern uint64_t ao_scheme_collects[2];
552 extern uint64_t ao_scheme_freed[2];
553 extern uint64_t ao_scheme_loops[2];
555 /* returns 1 if the object was already marked */
557 ao_scheme_mark_memory(const struct ao_scheme_type *type, void *addr);
559 /* returns 1 if the object was already moved */
561 ao_scheme_move_memory(const struct ao_scheme_type *type, void **ref);
564 ao_scheme_alloc(int size);
566 /* Marks an object as being printed, returns 1 if it was already marked */
568 ao_scheme_print_mark_addr(void *addr);
571 ao_scheme_print_mark_poly(ao_poly poly);
573 /* Notes that printing has started */
575 ao_scheme_print_start(void);
577 /* Notes that printing has ended */
579 ao_scheme_print_stop(void);
581 #define AO_SCHEME_COLLECT_FULL 1
582 #define AO_SCHEME_COLLECT_INCREMENTAL 0
585 ao_scheme_collect(uint8_t style);
589 ao_scheme_cons_check(struct ao_scheme_cons *cons);
593 ao_scheme_cons_stash(int id, struct ao_scheme_cons *cons);
595 struct ao_scheme_cons *
596 ao_scheme_cons_fetch(int id);
599 ao_scheme_poly_stash(int id, ao_poly poly);
602 ao_scheme_poly_fetch(int id);
605 ao_scheme_string_stash(int id, struct ao_scheme_string *string);
607 struct ao_scheme_string *
608 ao_scheme_string_fetch(int id);
611 ao_scheme_stack_stash(int id, struct ao_scheme_stack *stack) {
612 ao_scheme_poly_stash(id, ao_scheme_stack_poly(stack));
615 static inline struct ao_scheme_stack *
616 ao_scheme_stack_fetch(int id) {
617 return ao_scheme_poly_stack(ao_scheme_poly_fetch(id));
621 ao_scheme_frame_stash(int id, struct ao_scheme_frame *frame);
623 struct ao_scheme_frame *
624 ao_scheme_frame_fetch(int id);
628 extern const struct ao_scheme_type ao_scheme_bool_type;
631 ao_scheme_bool_write(ao_poly v);
633 #ifdef AO_SCHEME_MAKE_CONST
634 extern struct ao_scheme_bool *ao_scheme_true, *ao_scheme_false;
636 struct ao_scheme_bool *
637 ao_scheme_bool_get(uint8_t value);
641 extern const struct ao_scheme_type ao_scheme_cons_type;
643 struct ao_scheme_cons *
644 ao_scheme_cons_cons(ao_poly car, ao_poly cdr);
646 /* Return a cons or NULL for a proper list, else error */
647 struct ao_scheme_cons *
648 ao_scheme_cons_cdr(struct ao_scheme_cons *cons);
651 ao_scheme__cons(ao_poly car, ao_poly cdr);
653 extern struct ao_scheme_cons *ao_scheme_cons_free_list;
656 ao_scheme_cons_free(struct ao_scheme_cons *cons);
659 ao_scheme_cons_write(ao_poly);
662 ao_scheme_cons_display(ao_poly);
665 ao_scheme_cons_length(struct ao_scheme_cons *cons);
667 struct ao_scheme_cons *
668 ao_scheme_cons_copy(struct ao_scheme_cons *cons);
671 extern const struct ao_scheme_type ao_scheme_string_type;
673 struct ao_scheme_string *
674 ao_scheme_string_copy(struct ao_scheme_string *a);
676 struct ao_scheme_string *
677 ao_scheme_string_make(char *a);
679 struct ao_scheme_string *
680 ao_scheme_atom_to_string(struct ao_scheme_atom *a);
682 struct ao_scheme_string *
683 ao_scheme_string_cat(struct ao_scheme_string *a, struct ao_scheme_string *b);
686 ao_scheme_string_pack(struct ao_scheme_cons *cons);
689 ao_scheme_string_unpack(struct ao_scheme_string *a);
692 ao_scheme_string_write(ao_poly s);
695 ao_scheme_string_display(ao_poly s);
698 extern const struct ao_scheme_type ao_scheme_atom_type;
700 extern struct ao_scheme_atom *ao_scheme_atoms;
701 extern struct ao_scheme_frame *ao_scheme_frame_global;
702 extern struct ao_scheme_frame *ao_scheme_frame_current;
705 ao_scheme_atom_write(ao_poly a);
707 struct ao_scheme_atom *
708 ao_scheme_string_to_atom(struct ao_scheme_string *string);
710 struct ao_scheme_atom *
711 ao_scheme_atom_intern(char *name);
714 ao_scheme_atom_ref(ao_poly atom, struct ao_scheme_frame **frame_ref);
717 ao_scheme_atom_get(ao_poly atom);
720 ao_scheme_atom_set(ao_poly atom, ao_poly val);
723 ao_scheme_atom_def(ao_poly atom, ao_poly val);
727 ao_scheme_int_write(ao_poly i);
729 #ifdef AO_SCHEME_FEATURE_BIGINT
731 ao_scheme_poly_integer(ao_poly p, bool *fail);
734 ao_scheme_integer_poly(int32_t i);
737 ao_scheme_integer_typep(uint8_t t)
739 return (t == AO_SCHEME_INT) || (t == AO_SCHEME_BIGINT);
743 ao_scheme_bigint_write(ao_poly i);
745 extern const struct ao_scheme_type ao_scheme_bigint_type;
749 #define ao_scheme_poly_integer(a,b) ao_scheme_poly_int(a)
750 #define ao_scheme_integer_poly ao_scheme_int_poly
753 ao_scheme_integer_typep(uint8_t t)
755 return (t == AO_SCHEME_INT);
758 #endif /* AO_SCHEME_FEATURE_BIGINT */
763 ao_scheme_vector_write(ao_poly v);
766 ao_scheme_vector_display(ao_poly v);
768 struct ao_scheme_vector *
769 ao_scheme_vector_alloc(uint16_t length, ao_poly fill);
772 ao_scheme_vector_get(ao_poly v, ao_poly i);
775 ao_scheme_vector_set(ao_poly v, ao_poly i, ao_poly p);
777 struct ao_scheme_vector *
778 ao_scheme_list_to_vector(struct ao_scheme_cons *cons);
780 struct ao_scheme_cons *
781 ao_scheme_vector_to_list(struct ao_scheme_vector *vector);
783 extern const struct ao_scheme_type ao_scheme_vector_type;
786 void (*ao_scheme_poly_write_func(ao_poly p))(ao_poly p);
787 void (*ao_scheme_poly_display_func(ao_poly p))(ao_poly p);
790 ao_scheme_poly_write(ao_poly p) { (*ao_scheme_poly_write_func(p))(p); }
793 ao_scheme_poly_display(ao_poly p) { (*ao_scheme_poly_display_func(p))(p); }
796 ao_scheme_poly_mark(ao_poly p, uint8_t note_cons);
798 /* returns 1 if the object has already been moved */
800 ao_scheme_poly_move(ao_poly *p, uint8_t note_cons);
805 ao_scheme_eval_clear_globals(void);
808 ao_scheme_eval_restart(void);
811 ao_scheme_eval(ao_poly p);
814 ao_scheme_set_cond(struct ao_scheme_cons *cons);
817 #ifdef AO_SCHEME_FEATURE_FLOAT
818 extern const struct ao_scheme_type ao_scheme_float_type;
821 ao_scheme_float_write(ao_poly p);
824 ao_scheme_float_get(float value);
827 #ifdef AO_SCHEME_FEATURE_FLOAT
828 static inline uint8_t
829 ao_scheme_number_typep(uint8_t t)
831 return ao_scheme_integer_typep(t) || (t == AO_SCHEME_FLOAT);
834 #define ao_scheme_number_typep ao_scheme_integer_typep
839 ao_scheme_builtin_write(ao_poly b);
841 extern const struct ao_scheme_type ao_scheme_builtin_type;
843 /* Check argument count */
845 ao_scheme_check_argc(ao_poly name, struct ao_scheme_cons *cons, int min, int max);
847 /* Check argument type */
849 ao_scheme_check_argt(ao_poly name, struct ao_scheme_cons *cons, int argc, int type, int nil_ok);
851 /* Fetch an arg (nil if off the end) */
853 ao_scheme_arg(struct ao_scheme_cons *cons, int argc);
856 ao_scheme_args_name(uint8_t args);
859 extern int ao_scheme_read_list;
860 extern struct ao_scheme_cons *ao_scheme_read_cons;
861 extern struct ao_scheme_cons *ao_scheme_read_cons_tail;
862 extern struct ao_scheme_cons *ao_scheme_read_stack;
865 ao_scheme_read(void);
869 ao_scheme_read_eval_print(void);
872 extern const struct ao_scheme_type ao_scheme_frame_type;
873 extern const struct ao_scheme_type ao_scheme_frame_vals_type;
875 #define AO_SCHEME_FRAME_FREE 6
877 extern struct ao_scheme_frame *ao_scheme_frame_free_list[AO_SCHEME_FRAME_FREE];
880 ao_scheme_frame_mark(struct ao_scheme_frame *frame);
883 ao_scheme_frame_ref(struct ao_scheme_frame *frame, ao_poly atom);
885 struct ao_scheme_frame *
886 ao_scheme_frame_new(int num);
889 ao_scheme_frame_free(struct ao_scheme_frame *frame);
892 ao_scheme_frame_bind(struct ao_scheme_frame *frame, int num, ao_poly atom, ao_poly val);
895 ao_scheme_frame_add(struct ao_scheme_frame *frame, ao_poly atom, ao_poly val);
898 ao_scheme_frame_write(ao_poly p);
901 ao_scheme_frame_init(void);
904 extern const struct ao_scheme_type ao_scheme_lambda_type;
906 extern const char * const ao_scheme_state_names[];
908 struct ao_scheme_lambda *
909 ao_scheme_lambda_new(ao_poly cons);
912 ao_scheme_lambda_write(ao_poly lambda);
915 ao_scheme_lambda_eval(void);
919 extern const struct ao_scheme_type ao_scheme_stack_type;
920 extern struct ao_scheme_stack *ao_scheme_stack;
921 extern struct ao_scheme_stack *ao_scheme_stack_free_list;
924 ao_scheme_stack_reset(struct ao_scheme_stack *stack);
927 ao_scheme_stack_push(void);
930 ao_scheme_stack_pop(void);
933 ao_scheme_stack_clear(void);
936 ao_scheme_stack_write(ao_poly stack);
939 ao_scheme_stack_eval(void);
944 ao_scheme_vprintf(const char *format, va_list args);
947 ao_scheme_printf(const char *format, ...);
950 ao_scheme_error_poly(const char *name, ao_poly poly, ao_poly last);
953 ao_scheme_error_frame(int indent, const char *name, struct ao_scheme_frame *frame);
956 ao_scheme_error(int error, const char *format, ...);
960 #define AO_SCHEME_BUILTIN_DECLS
961 #include "ao_scheme_builtin.h"
963 /* debugging macros */
965 #if DBG_EVAL || DBG_READ
966 int ao_scheme_stack_depth;
971 #define DBG_INDENT() do { int _s; for(_s = 0; _s < ao_scheme_stack_depth; _s++) printf(" "); } while(0)
972 #define DBG_IN() (++ao_scheme_stack_depth)
973 #define DBG_OUT() (--ao_scheme_stack_depth)
974 #define DBG_RESET() (ao_scheme_stack_depth = 0)
975 #define DBG(...) ao_scheme_printf(__VA_ARGS__)
976 #define DBGI(...) do { printf("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
977 #define DBG_CONS(a) ao_scheme_cons_write(ao_scheme_cons_poly(a))
978 #define DBG_POLY(a) ao_scheme_poly_write(a)
979 #define OFFSET(a) ((a) ? (int) ((uint8_t *) a - ao_scheme_pool) : -1)
980 #define DBG_STACK() ao_scheme_stack_write(ao_scheme_stack_poly(ao_scheme_stack))
982 ao_scheme_frames_dump(void)
984 struct ao_scheme_stack *s;
985 DBGI(".. current frame: "); DBG_POLY(ao_scheme_frame_poly(ao_scheme_frame_current)); DBG("\n");
986 for (s = ao_scheme_stack; s; s = ao_scheme_poly_stack(s->prev)) {
987 DBGI(".. stack frame: "); DBG_POLY(s->frame); DBG("\n");
990 #define DBG_FRAMES() ao_scheme_frames_dump()
1002 #define DBG_FRAMES()
1006 #define RDBGI(...) do { printf("%4d: ", __LINE__); DBG_INDENT(); ao_scheme_printf(__VA_ARGS__); } while (0)
1007 #define RDBG_IN() (++ao_scheme_stack_depth)
1008 #define RDBG_OUT() (--ao_scheme_stack_depth)
1016 ao_scheme_mdbg_offset(void *a)
1023 if (ao_scheme_pool <= u && u < ao_scheme_pool + AO_SCHEME_POOL)
1024 return u - ao_scheme_pool;
1026 #ifndef AO_SCHEME_MAKE_CONST
1027 if (ao_scheme_const <= u && u < ao_scheme_const + AO_SCHEME_POOL_CONST)
1028 return - (int) (u - ao_scheme_const);
1033 #define MDBG_OFFSET(a) ao_scheme_mdbg_offset(a)
1037 #define DBG_MEM_START 1
1040 extern int dbg_move_depth;
1045 #define MDBG_DO(a) a
1046 #define MDBG_MOVE(...) do { if (dbg_mem) { int d; for (d = 0; d < dbg_move_depth; d++) printf (" "); printf(__VA_ARGS__); } } while (0)
1047 #define MDBG_MORE(...) do { if (dbg_mem) printf(__VA_ARGS__); } while (0)
1048 #define MDBG_MOVE_IN() (dbg_move_depth++)
1049 #define MDBG_MOVE_OUT() (assert(--dbg_move_depth >= 0))
1054 #define MDBG_MOVE(...)
1055 #define MDBG_MORE(...)
1056 #define MDBG_MOVE_IN()
1057 #define MDBG_MOVE_OUT()
1061 #endif /* _AO_SCHEME_H_ */