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)
162 ao_scheme_ref(ao_poly poly);
165 ao_scheme_poly(const void *addr, ao_poly type);
167 struct ao_scheme_type {
168 int (*size)(void *addr);
169 void (*mark)(void *addr);
170 void (*move)(void *addr);
174 struct ao_scheme_cons {
179 struct ao_scheme_atom {
186 struct ao_scheme_string {
191 struct ao_scheme_val {
196 struct ao_scheme_frame_vals {
199 struct ao_scheme_val vals[];
202 struct ao_scheme_frame {
209 struct ao_scheme_bool {
216 #ifdef AO_SCHEME_FEATURE_FLOAT
217 struct ao_scheme_float {
225 #ifdef AO_SCHEME_FEATURE_VECTOR
226 struct ao_scheme_vector {
234 #define AO_SCHEME_MIN_INT (-(1 << (15 - AO_SCHEME_TYPE_SHIFT)))
235 #define AO_SCHEME_MAX_INT ((1 << (15 - AO_SCHEME_TYPE_SHIFT)) - 1)
237 #ifdef AO_SCHEME_FEATURE_BIGINT
239 struct ao_scheme_bigint {
243 #define AO_SCHEME_MIN_BIGINT INT32_MIN
244 #define AO_SCHEME_MAX_BIGINT INT32_MAX
246 #endif /* AO_SCHEME_FEATURE_BIGINT */
248 /* Set on type when the frame escapes the lambda */
249 #define AO_SCHEME_FRAME_MARK 0x80
251 static inline int ao_scheme_frame_marked(struct ao_scheme_frame *f) {
252 return f->type & AO_SCHEME_FRAME_MARK;
255 static inline struct ao_scheme_frame *
256 ao_scheme_poly_frame(ao_poly poly) {
257 return ao_scheme_ref(poly);
260 static inline ao_poly
261 ao_scheme_frame_poly(struct ao_scheme_frame *frame) {
262 return ao_scheme_poly(frame, AO_SCHEME_OTHER);
265 static inline struct ao_scheme_frame_vals *
266 ao_scheme_poly_frame_vals(ao_poly poly) {
267 return ao_scheme_ref(poly);
270 static inline ao_poly
271 ao_scheme_frame_vals_poly(struct ao_scheme_frame_vals *vals) {
272 return ao_scheme_poly(vals, AO_SCHEME_OTHER);
276 eval_sexpr, /* Evaluate an sexpr */
277 eval_val, /* Value computed */
278 eval_formal, /* Formal computed */
279 eval_exec, /* Start a lambda evaluation */
280 eval_apply, /* Execute apply */
281 eval_cond, /* Start next cond clause */
282 eval_cond_test, /* Check cond condition */
283 eval_begin, /* Start next begin entry */
284 eval_while, /* Start while condition */
285 eval_while_test, /* Check while condition */
286 eval_macro, /* Finished with macro generation */
289 struct ao_scheme_stack {
290 uint8_t type; /* AO_SCHEME_STACK */
291 uint8_t state; /* enum eval_state */
292 ao_poly prev; /* previous stack frame */
293 ao_poly sexprs; /* expressions to evaluate */
294 ao_poly values; /* values computed */
295 ao_poly values_tail; /* end of the values list for easy appending */
296 ao_poly frame; /* current lookup frame */
297 ao_poly list; /* most recent function call */
300 #define AO_SCHEME_STACK_MARK 0x80 /* set on type when a reference has been taken */
302 static inline int ao_scheme_stack_marked(struct ao_scheme_stack *s) {
303 return s->type & AO_SCHEME_STACK_MARK;
306 static inline void ao_scheme_stack_mark(struct ao_scheme_stack *s) {
307 s->type |= AO_SCHEME_STACK_MARK;
310 static inline struct ao_scheme_stack *
311 ao_scheme_poly_stack(ao_poly p)
313 return ao_scheme_ref(p);
316 static inline ao_poly
317 ao_scheme_stack_poly(struct ao_scheme_stack *stack)
319 return ao_scheme_poly(stack, AO_SCHEME_OTHER);
322 extern ao_poly ao_scheme_v;
324 #define AO_SCHEME_FUNC_LAMBDA 0
325 #define AO_SCHEME_FUNC_NLAMBDA 1
326 #define AO_SCHEME_FUNC_MACRO 2
328 #define AO_SCHEME_FUNC_FREE_ARGS 0x80
329 #define AO_SCHEME_FUNC_MASK 0x7f
331 #define AO_SCHEME_FUNC_F_LAMBDA (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_LAMBDA)
332 #define AO_SCHEME_FUNC_F_NLAMBDA (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_NLAMBDA)
333 #define AO_SCHEME_FUNC_F_MACRO (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_MACRO)
335 struct ao_scheme_builtin {
341 #define AO_SCHEME_BUILTIN_ID
342 #include "ao_scheme_builtin.h"
344 typedef ao_poly (*ao_scheme_func_t)(struct ao_scheme_cons *cons);
346 extern const ao_scheme_func_t ao_scheme_builtins[];
348 static inline ao_scheme_func_t
349 ao_scheme_func(struct ao_scheme_builtin *b)
351 return ao_scheme_builtins[b->func];
354 struct ao_scheme_lambda {
361 static inline struct ao_scheme_lambda *
362 ao_scheme_poly_lambda(ao_poly poly)
364 return ao_scheme_ref(poly);
367 static inline ao_poly
368 ao_scheme_lambda_poly(struct ao_scheme_lambda *lambda)
370 return ao_scheme_poly(lambda, AO_SCHEME_OTHER);
374 ao_scheme_poly_other(ao_poly poly) {
375 return ao_scheme_ref(poly);
378 static inline uint8_t
379 ao_scheme_other_type(void *other) {
381 if ((*((uint8_t *) other) & AO_SCHEME_OTHER_TYPE_MASK) >= AO_SCHEME_NUM_TYPE)
384 return *((uint8_t *) other) & AO_SCHEME_OTHER_TYPE_MASK;
387 static inline ao_poly
388 ao_scheme_other_poly(const void *other)
390 return ao_scheme_poly(other, AO_SCHEME_OTHER);
394 ao_scheme_size_round(int size)
396 return (size + 3) & ~3;
400 ao_scheme_size(const struct ao_scheme_type *type, void *addr)
402 return ao_scheme_size_round(type->size(addr));
405 #define AO_SCHEME_OTHER_POLY(other) ((ao_poly)(other) + AO_SCHEME_OTHER)
407 static inline int ao_scheme_poly_base_type(ao_poly poly) {
408 return poly & AO_SCHEME_TYPE_MASK;
411 static inline int ao_scheme_poly_type(ao_poly poly) {
412 int type = poly & AO_SCHEME_TYPE_MASK;
413 if (type == AO_SCHEME_OTHER)
414 return ao_scheme_other_type(ao_scheme_poly_other(poly));
419 ao_scheme_is_cons(ao_poly poly) {
420 return (ao_scheme_poly_base_type(poly) == AO_SCHEME_CONS);
424 ao_scheme_is_pair(ao_poly poly) {
425 return poly != AO_SCHEME_NIL && (ao_scheme_poly_base_type(poly) == AO_SCHEME_CONS);
428 static inline struct ao_scheme_cons *
429 ao_scheme_poly_cons(ao_poly poly)
431 return ao_scheme_ref(poly);
434 static inline ao_poly
435 ao_scheme_cons_poly(struct ao_scheme_cons *cons)
437 return ao_scheme_poly(cons, AO_SCHEME_CONS);
440 static inline int32_t
441 ao_scheme_poly_int(ao_poly poly)
443 return (int32_t) ((ao_signed_poly) poly >> AO_SCHEME_TYPE_SHIFT);
446 static inline ao_poly
447 ao_scheme_int_poly(int32_t i)
449 return ((ao_poly) i << 2) | AO_SCHEME_INT;
452 #ifdef AO_SCHEME_FEATURE_BIGINT
453 static inline struct ao_scheme_bigint *
454 ao_scheme_poly_bigint(ao_poly poly)
456 return ao_scheme_ref(poly);
459 static inline ao_poly
460 ao_scheme_bigint_poly(struct ao_scheme_bigint *bi)
462 return ao_scheme_poly(bi, AO_SCHEME_BIGINT);
464 #endif /* AO_SCHEME_FEATURE_BIGINT */
466 static inline struct ao_scheme_string *
467 ao_scheme_poly_string(ao_poly poly)
469 return ao_scheme_ref(poly);
472 static inline ao_poly
473 ao_scheme_string_poly(struct ao_scheme_string *s)
475 return ao_scheme_poly(s, AO_SCHEME_OTHER);
478 static inline struct ao_scheme_atom *
479 ao_scheme_poly_atom(ao_poly poly)
481 return ao_scheme_ref(poly);
484 static inline ao_poly
485 ao_scheme_atom_poly(struct ao_scheme_atom *a)
487 return ao_scheme_poly(a, AO_SCHEME_OTHER);
490 static inline struct ao_scheme_builtin *
491 ao_scheme_poly_builtin(ao_poly poly)
493 return ao_scheme_ref(poly);
496 static inline ao_poly
497 ao_scheme_builtin_poly(struct ao_scheme_builtin *b)
499 return ao_scheme_poly(b, AO_SCHEME_OTHER);
502 static inline ao_poly
503 ao_scheme_bool_poly(struct ao_scheme_bool *b)
505 return ao_scheme_poly(b, AO_SCHEME_OTHER);
508 static inline struct ao_scheme_bool *
509 ao_scheme_poly_bool(ao_poly poly)
511 return ao_scheme_ref(poly);
514 #ifdef AO_SCHEME_FEATURE_FLOAT
515 static inline ao_poly
516 ao_scheme_float_poly(struct ao_scheme_float *f)
518 return ao_scheme_poly(f, AO_SCHEME_OTHER);
521 static inline struct ao_scheme_float *
522 ao_scheme_poly_float(ao_poly poly)
524 return ao_scheme_ref(poly);
528 ao_scheme_poly_number(ao_poly p);
531 #ifdef AO_SCHEME_FEATURE_VECTOR
532 static inline ao_poly
533 ao_scheme_vector_poly(struct ao_scheme_vector *v)
535 return ao_scheme_poly(v, AO_SCHEME_OTHER);
538 static inline struct ao_scheme_vector *
539 ao_scheme_poly_vector(ao_poly poly)
541 return ao_scheme_ref(poly);
545 /* memory functions */
547 extern uint64_t ao_scheme_collects[2];
548 extern uint64_t ao_scheme_freed[2];
549 extern uint64_t ao_scheme_loops[2];
551 /* returns 1 if the object was already marked */
553 ao_scheme_mark_memory(const struct ao_scheme_type *type, void *addr);
555 /* returns 1 if the object was already moved */
557 ao_scheme_move_memory(const struct ao_scheme_type *type, void **ref);
560 ao_scheme_alloc(int size);
562 /* Marks an object as being printed, returns 1 if it was already marked */
564 ao_scheme_print_mark_addr(void *addr);
567 ao_scheme_print_clear_addr(void *addr);
569 /* Notes that printing has started */
571 ao_scheme_print_start(void);
573 /* Notes that printing has ended, returns 1 if printing is still happening */
575 ao_scheme_print_stop(void);
577 #define AO_SCHEME_COLLECT_FULL 1
578 #define AO_SCHEME_COLLECT_INCREMENTAL 0
581 ao_scheme_collect(uint8_t style);
585 ao_scheme_cons_check(struct ao_scheme_cons *cons);
589 ao_scheme_cons_stash(int id, struct ao_scheme_cons *cons);
591 struct ao_scheme_cons *
592 ao_scheme_cons_fetch(int id);
595 ao_scheme_poly_stash(int id, ao_poly poly);
598 ao_scheme_poly_fetch(int id);
601 ao_scheme_string_stash(int id, struct ao_scheme_string *string);
603 struct ao_scheme_string *
604 ao_scheme_string_fetch(int id);
607 ao_scheme_stack_stash(int id, struct ao_scheme_stack *stack) {
608 ao_scheme_poly_stash(id, ao_scheme_stack_poly(stack));
611 static inline struct ao_scheme_stack *
612 ao_scheme_stack_fetch(int id) {
613 return ao_scheme_poly_stack(ao_scheme_poly_fetch(id));
617 ao_scheme_frame_stash(int id, struct ao_scheme_frame *frame);
619 struct ao_scheme_frame *
620 ao_scheme_frame_fetch(int id);
624 extern const struct ao_scheme_type ao_scheme_bool_type;
627 ao_scheme_bool_write(ao_poly v, bool write);
629 #ifdef AO_SCHEME_MAKE_CONST
630 extern struct ao_scheme_bool *ao_scheme_true, *ao_scheme_false;
632 struct ao_scheme_bool *
633 ao_scheme_bool_get(uint8_t value);
637 extern const struct ao_scheme_type ao_scheme_cons_type;
639 struct ao_scheme_cons *
640 ao_scheme_cons_cons(ao_poly car, ao_poly cdr);
642 /* Return a cons or NULL for a proper list, else error */
643 struct ao_scheme_cons *
644 ao_scheme_cons_cdr(struct ao_scheme_cons *cons);
647 ao_scheme_cons(ao_poly car, ao_poly cdr);
649 extern struct ao_scheme_cons *ao_scheme_cons_free_list;
652 ao_scheme_cons_free(struct ao_scheme_cons *cons);
655 ao_scheme_cons_write(ao_poly, bool write);
658 ao_scheme_cons_length(struct ao_scheme_cons *cons);
660 struct ao_scheme_cons *
661 ao_scheme_cons_copy(struct ao_scheme_cons *cons);
664 extern const struct ao_scheme_type ao_scheme_string_type;
666 struct ao_scheme_string *
667 ao_scheme_string_copy(struct ao_scheme_string *a);
669 struct ao_scheme_string *
670 ao_scheme_string_make(char *a);
672 struct ao_scheme_string *
673 ao_scheme_atom_to_string(struct ao_scheme_atom *a);
675 struct ao_scheme_string *
676 ao_scheme_string_cat(struct ao_scheme_string *a, struct ao_scheme_string *b);
679 ao_scheme_string_pack(struct ao_scheme_cons *cons);
682 ao_scheme_string_unpack(struct ao_scheme_string *a);
685 ao_scheme_string_write(ao_poly s, bool write);
688 extern const struct ao_scheme_type ao_scheme_atom_type;
690 extern struct ao_scheme_atom *ao_scheme_atoms;
691 extern struct ao_scheme_frame *ao_scheme_frame_global;
692 extern struct ao_scheme_frame *ao_scheme_frame_current;
695 ao_scheme_atom_write(ao_poly a, bool write);
697 struct ao_scheme_atom *
698 ao_scheme_string_to_atom(struct ao_scheme_string *string);
700 struct ao_scheme_atom *
701 ao_scheme_atom_intern(char *name);
704 ao_scheme_atom_ref(ao_poly atom, struct ao_scheme_frame **frame_ref);
707 ao_scheme_atom_get(ao_poly atom);
710 ao_scheme_atom_set(ao_poly atom, ao_poly val);
713 ao_scheme_atom_def(ao_poly atom, ao_poly val);
717 ao_scheme_int_write(ao_poly i, bool write);
719 #ifdef AO_SCHEME_FEATURE_BIGINT
721 ao_scheme_poly_integer(ao_poly p, bool *fail);
724 ao_scheme_integer_poly(int32_t i);
727 ao_scheme_integer_typep(uint8_t t)
729 return (t == AO_SCHEME_INT) || (t == AO_SCHEME_BIGINT);
733 ao_scheme_bigint_write(ao_poly i, bool write);
735 extern const struct ao_scheme_type ao_scheme_bigint_type;
739 #define ao_scheme_poly_integer(a,b) ao_scheme_poly_int(a)
740 #define ao_scheme_integer_poly ao_scheme_int_poly
743 ao_scheme_integer_typep(uint8_t t)
745 return (t == AO_SCHEME_INT);
748 #endif /* AO_SCHEME_FEATURE_BIGINT */
753 ao_scheme_vector_write(ao_poly v, bool write);
755 struct ao_scheme_vector *
756 ao_scheme_vector_alloc(uint16_t length, ao_poly fill);
759 ao_scheme_vector_get(ao_poly v, ao_poly i);
762 ao_scheme_vector_set(ao_poly v, ao_poly i, ao_poly p);
764 struct ao_scheme_vector *
765 ao_scheme_list_to_vector(struct ao_scheme_cons *cons);
767 struct ao_scheme_cons *
768 ao_scheme_vector_to_list(struct ao_scheme_vector *vector);
770 extern const struct ao_scheme_type ao_scheme_vector_type;
773 void (*ao_scheme_poly_write_func(ao_poly p))(ao_poly p, bool write);
776 ao_scheme_poly_write(ao_poly p, bool write) { (*ao_scheme_poly_write_func(p))(p, write); }
779 ao_scheme_poly_mark(ao_poly p, uint8_t note_cons);
781 /* returns 1 if the object has already been moved */
783 ao_scheme_poly_move(ao_poly *p, uint8_t note_cons);
788 ao_scheme_eval_clear_globals(void);
791 ao_scheme_eval_restart(void);
794 ao_scheme_eval(ao_poly p);
797 ao_scheme_set_cond(struct ao_scheme_cons *cons);
800 #ifdef AO_SCHEME_FEATURE_FLOAT
801 extern const struct ao_scheme_type ao_scheme_float_type;
804 ao_scheme_float_write(ao_poly p, bool write);
807 ao_scheme_float_get(float value);
810 #ifdef AO_SCHEME_FEATURE_FLOAT
811 static inline uint8_t
812 ao_scheme_number_typep(uint8_t t)
814 return ao_scheme_integer_typep(t) || (t == AO_SCHEME_FLOAT);
817 #define ao_scheme_number_typep ao_scheme_integer_typep
822 ao_scheme_builtin_write(ao_poly b, bool write);
824 extern const struct ao_scheme_type ao_scheme_builtin_type;
826 /* Check argument count */
828 ao_scheme_check_argc(ao_poly name, struct ao_scheme_cons *cons, int min, int max);
830 /* Check argument type */
832 ao_scheme_check_argt(ao_poly name, struct ao_scheme_cons *cons, int argc, int type, int nil_ok);
834 /* Fetch an arg (nil if off the end) */
836 ao_scheme_arg(struct ao_scheme_cons *cons, int argc);
839 ao_scheme_args_name(uint8_t args);
842 extern int ao_scheme_read_list;
843 extern struct ao_scheme_cons *ao_scheme_read_cons;
844 extern struct ao_scheme_cons *ao_scheme_read_cons_tail;
845 extern struct ao_scheme_cons *ao_scheme_read_stack;
848 ao_scheme_read(void);
852 ao_scheme_read_eval_print(void);
855 extern const struct ao_scheme_type ao_scheme_frame_type;
856 extern const struct ao_scheme_type ao_scheme_frame_vals_type;
858 #define AO_SCHEME_FRAME_FREE 6
860 extern struct ao_scheme_frame *ao_scheme_frame_free_list[AO_SCHEME_FRAME_FREE];
863 ao_scheme_frame_mark(struct ao_scheme_frame *frame);
866 ao_scheme_frame_ref(struct ao_scheme_frame *frame, ao_poly atom);
868 struct ao_scheme_frame *
869 ao_scheme_frame_new(int num);
872 ao_scheme_frame_free(struct ao_scheme_frame *frame);
875 ao_scheme_frame_bind(struct ao_scheme_frame *frame, int num, ao_poly atom, ao_poly val);
878 ao_scheme_frame_add(struct ao_scheme_frame *frame, ao_poly atom, ao_poly val);
881 ao_scheme_frame_write(ao_poly p, bool write);
884 ao_scheme_frame_init(void);
887 extern const struct ao_scheme_type ao_scheme_lambda_type;
889 extern const char * const ao_scheme_state_names[];
891 struct ao_scheme_lambda *
892 ao_scheme_lambda_new(ao_poly cons);
895 ao_scheme_lambda_write(ao_poly lambda, bool write);
898 ao_scheme_lambda_eval(void);
902 extern const struct ao_scheme_type ao_scheme_stack_type;
903 extern struct ao_scheme_stack *ao_scheme_stack;
904 extern struct ao_scheme_stack *ao_scheme_stack_free_list;
906 extern int ao_scheme_frame_print_indent;
909 ao_scheme_stack_reset(struct ao_scheme_stack *stack);
912 ao_scheme_stack_push(void);
915 ao_scheme_stack_pop(void);
918 ao_scheme_stack_clear(void);
921 ao_scheme_stack_write(ao_poly stack, bool write);
924 ao_scheme_stack_eval(void);
929 ao_scheme_vprintf(const char *format, va_list args);
932 ao_scheme_printf(const char *format, ...);
935 ao_scheme_error(int error, const char *format, ...);
939 #define AO_SCHEME_BUILTIN_DECLS
940 #include "ao_scheme_builtin.h"
942 /* debugging macros */
944 #if DBG_EVAL || DBG_READ
945 int ao_scheme_stack_depth;
950 #define DBG_INDENT() do { int _s; for(_s = 0; _s < ao_scheme_stack_depth; _s++) printf(" "); } while(0)
951 #define DBG_IN() (++ao_scheme_stack_depth)
952 #define DBG_OUT() (--ao_scheme_stack_depth)
953 #define DBG_RESET() (ao_scheme_stack_depth = 0)
954 #define DBG(...) ao_scheme_printf(__VA_ARGS__)
955 #define DBGI(...) do { printf("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
956 #define DBG_CONS(a) ao_scheme_cons_write(ao_scheme_cons_poly(a), true)
957 #define DBG_POLY(a) ao_scheme_poly_write(a, true)
958 #define OFFSET(a) ((a) ? (int) ((uint8_t *) a - ao_scheme_pool) : -1)
959 #define DBG_STACK() ao_scheme_stack_write(ao_scheme_stack_poly(ao_scheme_stack), true)
961 ao_scheme_frames_dump(void)
963 struct ao_scheme_stack *s;
964 DBGI(".. current frame: "); DBG_POLY(ao_scheme_frame_poly(ao_scheme_frame_current)); DBG("\n");
965 for (s = ao_scheme_stack; s; s = ao_scheme_poly_stack(s->prev)) {
966 DBGI(".. stack frame: "); DBG_POLY(s->frame); DBG("\n");
969 #define DBG_FRAMES() ao_scheme_frames_dump()
985 #define RDBGI(...) do { printf("%4d: ", __LINE__); DBG_INDENT(); ao_scheme_printf(__VA_ARGS__); } while (0)
986 #define RDBG_IN() (++ao_scheme_stack_depth)
987 #define RDBG_OUT() (--ao_scheme_stack_depth)
995 ao_scheme_mdbg_offset(void *a)
1002 if (ao_scheme_pool <= u && u < ao_scheme_pool + AO_SCHEME_POOL)
1003 return u - ao_scheme_pool;
1005 #ifndef AO_SCHEME_MAKE_CONST
1006 if (ao_scheme_const <= u && u < ao_scheme_const + AO_SCHEME_POOL_CONST)
1007 return - (int) (u - ao_scheme_const);
1012 #define MDBG_OFFSET(a) ao_scheme_mdbg_offset(a)
1016 #define DBG_MEM_START 1
1019 extern int dbg_move_depth;
1024 #define MDBG_DO(a) a
1025 #define MDBG_MOVE(...) do { if (dbg_mem) { int d; for (d = 0; d < dbg_move_depth; d++) printf (" "); printf(__VA_ARGS__); } } while (0)
1026 #define MDBG_MORE(...) do { if (dbg_mem) printf(__VA_ARGS__); } while (0)
1027 #define MDBG_MOVE_IN() (dbg_move_depth++)
1028 #define MDBG_MOVE_OUT() (assert(--dbg_move_depth >= 0))
1033 #define MDBG_MOVE(...)
1034 #define MDBG_MORE(...)
1035 #define MDBG_MOVE_IN()
1036 #define MDBG_MOVE_OUT()
1040 #endif /* _AO_SCHEME_H_ */