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
253 static inline int ao_scheme_frame_marked(struct ao_scheme_frame *f) {
254 return f->type & AO_SCHEME_FRAME_MARK;
257 static inline struct ao_scheme_frame *
258 ao_scheme_poly_frame(ao_poly poly) {
259 return ao_scheme_ref(poly);
262 static inline ao_poly
263 ao_scheme_frame_poly(struct ao_scheme_frame *frame) {
264 return ao_scheme_poly(frame, AO_SCHEME_OTHER);
267 static inline struct ao_scheme_frame_vals *
268 ao_scheme_poly_frame_vals(ao_poly poly) {
269 return ao_scheme_ref(poly);
272 static inline ao_poly
273 ao_scheme_frame_vals_poly(struct ao_scheme_frame_vals *vals) {
274 return ao_scheme_poly(vals, AO_SCHEME_OTHER);
278 eval_sexpr, /* Evaluate an sexpr */
279 eval_val, /* Value computed */
280 eval_formal, /* Formal computed */
281 eval_exec, /* Start a lambda evaluation */
282 eval_apply, /* Execute apply */
283 eval_cond, /* Start next cond clause */
284 eval_cond_test, /* Check cond condition */
285 eval_begin, /* Start next begin entry */
286 eval_while, /* Start while condition */
287 eval_while_test, /* Check while condition */
288 eval_macro, /* Finished with macro generation */
291 struct ao_scheme_stack {
292 uint8_t type; /* AO_SCHEME_STACK */
293 uint8_t state; /* enum eval_state */
294 ao_poly prev; /* previous stack frame */
295 ao_poly sexprs; /* expressions to evaluate */
296 ao_poly values; /* values computed */
297 ao_poly values_tail; /* end of the values list for easy appending */
298 ao_poly frame; /* current lookup frame */
299 ao_poly list; /* most recent function call */
302 #define AO_SCHEME_STACK_MARK 0x80 /* set on type when a reference has been taken */
304 static inline int ao_scheme_stack_marked(struct ao_scheme_stack *s) {
305 return s->type & AO_SCHEME_STACK_MARK;
308 static inline void ao_scheme_stack_mark(struct ao_scheme_stack *s) {
309 s->type |= AO_SCHEME_STACK_MARK;
312 static inline struct ao_scheme_stack *
313 ao_scheme_poly_stack(ao_poly p)
315 return ao_scheme_ref(p);
318 static inline ao_poly
319 ao_scheme_stack_poly(struct ao_scheme_stack *stack)
321 return ao_scheme_poly(stack, AO_SCHEME_OTHER);
324 extern ao_poly ao_scheme_v;
326 #define AO_SCHEME_FUNC_LAMBDA 0
327 #define AO_SCHEME_FUNC_NLAMBDA 1
328 #define AO_SCHEME_FUNC_MACRO 2
330 #define AO_SCHEME_FUNC_FREE_ARGS 0x80
331 #define AO_SCHEME_FUNC_MASK 0x7f
333 #define AO_SCHEME_FUNC_F_LAMBDA (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_LAMBDA)
334 #define AO_SCHEME_FUNC_F_NLAMBDA (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_NLAMBDA)
335 #define AO_SCHEME_FUNC_F_MACRO (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_MACRO)
337 struct ao_scheme_builtin {
343 #define AO_SCHEME_BUILTIN_ID
344 #include "ao_scheme_builtin.h"
346 typedef ao_poly (*ao_scheme_func_t)(struct ao_scheme_cons *cons);
348 extern const ao_scheme_func_t ao_scheme_builtins[];
350 static inline ao_scheme_func_t
351 ao_scheme_func(struct ao_scheme_builtin *b)
353 return ao_scheme_builtins[b->func];
356 struct ao_scheme_lambda {
363 static inline struct ao_scheme_lambda *
364 ao_scheme_poly_lambda(ao_poly poly)
366 return ao_scheme_ref(poly);
369 static inline ao_poly
370 ao_scheme_lambda_poly(struct ao_scheme_lambda *lambda)
372 return ao_scheme_poly(lambda, AO_SCHEME_OTHER);
376 ao_scheme_poly_other(ao_poly poly) {
377 return ao_scheme_ref(poly);
380 static inline uint8_t
381 ao_scheme_other_type(void *other) {
383 if ((*((uint8_t *) other) & AO_SCHEME_OTHER_TYPE_MASK) >= AO_SCHEME_NUM_TYPE)
386 return *((uint8_t *) other) & AO_SCHEME_OTHER_TYPE_MASK;
389 static inline ao_poly
390 ao_scheme_other_poly(const void *other)
392 return ao_scheme_poly(other, AO_SCHEME_OTHER);
396 ao_scheme_size_round(int size)
398 return (size + 3) & ~3;
402 ao_scheme_size(const struct ao_scheme_type *type, void *addr)
404 return ao_scheme_size_round(type->size(addr));
407 #define AO_SCHEME_OTHER_POLY(other) ((ao_poly)(other) + AO_SCHEME_OTHER)
409 static inline int ao_scheme_poly_base_type(ao_poly poly) {
410 return poly & AO_SCHEME_TYPE_MASK;
413 static inline int ao_scheme_poly_type(ao_poly poly) {
414 int type = poly & AO_SCHEME_TYPE_MASK;
415 if (type == AO_SCHEME_OTHER)
416 return ao_scheme_other_type(ao_scheme_poly_other(poly));
421 ao_scheme_is_cons(ao_poly poly) {
422 return (ao_scheme_poly_base_type(poly) == AO_SCHEME_CONS);
426 ao_scheme_is_pair(ao_poly poly) {
427 return poly != AO_SCHEME_NIL && (ao_scheme_poly_base_type(poly) == AO_SCHEME_CONS);
430 static inline struct ao_scheme_cons *
431 ao_scheme_poly_cons(ao_poly poly)
433 return ao_scheme_ref(poly);
436 static inline ao_poly
437 ao_scheme_cons_poly(struct ao_scheme_cons *cons)
439 return ao_scheme_poly(cons, AO_SCHEME_CONS);
442 static inline int32_t
443 ao_scheme_poly_int(ao_poly poly)
445 return (int32_t) ((ao_signed_poly) poly >> AO_SCHEME_TYPE_SHIFT);
448 static inline ao_poly
449 ao_scheme_int_poly(int32_t i)
451 return ((ao_poly) i << 2) | AO_SCHEME_INT;
454 #ifdef AO_SCHEME_FEATURE_BIGINT
455 static inline struct ao_scheme_bigint *
456 ao_scheme_poly_bigint(ao_poly poly)
458 return ao_scheme_ref(poly);
461 static inline ao_poly
462 ao_scheme_bigint_poly(struct ao_scheme_bigint *bi)
464 return ao_scheme_poly(bi, AO_SCHEME_BIGINT);
466 #endif /* AO_SCHEME_FEATURE_BIGINT */
468 static inline struct ao_scheme_string *
469 ao_scheme_poly_string(ao_poly poly)
471 return ao_scheme_ref(poly);
474 static inline ao_poly
475 ao_scheme_string_poly(struct ao_scheme_string *s)
477 return ao_scheme_poly(s, AO_SCHEME_OTHER);
480 static inline struct ao_scheme_atom *
481 ao_scheme_poly_atom(ao_poly poly)
483 return ao_scheme_ref(poly);
486 static inline ao_poly
487 ao_scheme_atom_poly(struct ao_scheme_atom *a)
489 return ao_scheme_poly(a, AO_SCHEME_OTHER);
492 static inline struct ao_scheme_builtin *
493 ao_scheme_poly_builtin(ao_poly poly)
495 return ao_scheme_ref(poly);
498 static inline ao_poly
499 ao_scheme_builtin_poly(struct ao_scheme_builtin *b)
501 return ao_scheme_poly(b, AO_SCHEME_OTHER);
504 static inline ao_poly
505 ao_scheme_bool_poly(struct ao_scheme_bool *b)
507 return ao_scheme_poly(b, AO_SCHEME_OTHER);
510 static inline struct ao_scheme_bool *
511 ao_scheme_poly_bool(ao_poly poly)
513 return ao_scheme_ref(poly);
516 #ifdef AO_SCHEME_FEATURE_FLOAT
517 static inline ao_poly
518 ao_scheme_float_poly(struct ao_scheme_float *f)
520 return ao_scheme_poly(f, AO_SCHEME_OTHER);
523 static inline struct ao_scheme_float *
524 ao_scheme_poly_float(ao_poly poly)
526 return ao_scheme_ref(poly);
530 ao_scheme_poly_number(ao_poly p);
533 #ifdef AO_SCHEME_FEATURE_VECTOR
534 static inline ao_poly
535 ao_scheme_vector_poly(struct ao_scheme_vector *v)
537 return ao_scheme_poly(v, AO_SCHEME_OTHER);
540 static inline struct ao_scheme_vector *
541 ao_scheme_poly_vector(ao_poly poly)
543 return ao_scheme_ref(poly);
547 /* memory functions */
549 extern uint64_t ao_scheme_collects[2];
550 extern uint64_t ao_scheme_freed[2];
551 extern uint64_t ao_scheme_loops[2];
553 /* returns 1 if the object was already marked */
555 ao_scheme_mark_memory(const struct ao_scheme_type *type, void *addr);
557 /* returns 1 if the object was already moved */
559 ao_scheme_move_memory(const struct ao_scheme_type *type, void **ref);
562 ao_scheme_alloc(int size);
564 /* Marks an object as being printed, returns 1 if it was already marked */
566 ao_scheme_print_mark_addr(void *addr);
569 ao_scheme_print_clear_addr(void *addr);
571 /* Notes that printing has started */
573 ao_scheme_print_start(void);
575 /* Notes that printing has ended, returns 1 if printing is still happening */
577 ao_scheme_print_stop(void);
579 #define AO_SCHEME_COLLECT_FULL 1
580 #define AO_SCHEME_COLLECT_INCREMENTAL 0
583 ao_scheme_collect(uint8_t style);
587 ao_scheme_cons_check(struct ao_scheme_cons *cons);
591 ao_scheme_cons_stash(int id, struct ao_scheme_cons *cons);
593 struct ao_scheme_cons *
594 ao_scheme_cons_fetch(int id);
597 ao_scheme_poly_stash(int id, ao_poly poly);
600 ao_scheme_poly_fetch(int id);
603 ao_scheme_string_stash(int id, struct ao_scheme_string *string);
605 struct ao_scheme_string *
606 ao_scheme_string_fetch(int id);
609 ao_scheme_stack_stash(int id, struct ao_scheme_stack *stack) {
610 ao_scheme_poly_stash(id, ao_scheme_stack_poly(stack));
613 static inline struct ao_scheme_stack *
614 ao_scheme_stack_fetch(int id) {
615 return ao_scheme_poly_stack(ao_scheme_poly_fetch(id));
619 ao_scheme_frame_stash(int id, struct ao_scheme_frame *frame);
621 struct ao_scheme_frame *
622 ao_scheme_frame_fetch(int id);
626 extern const struct ao_scheme_type ao_scheme_bool_type;
629 ao_scheme_bool_write(ao_poly v, bool write);
631 #ifdef AO_SCHEME_MAKE_CONST
632 extern struct ao_scheme_bool *ao_scheme_true, *ao_scheme_false;
634 struct ao_scheme_bool *
635 ao_scheme_bool_get(uint8_t value);
639 extern const struct ao_scheme_type ao_scheme_cons_type;
641 struct ao_scheme_cons *
642 ao_scheme_cons_cons(ao_poly car, ao_poly cdr);
644 /* Return a cons or NULL for a proper list, else error */
645 struct ao_scheme_cons *
646 ao_scheme_cons_cdr(struct ao_scheme_cons *cons);
649 ao_scheme__cons(ao_poly car, ao_poly cdr);
651 extern struct ao_scheme_cons *ao_scheme_cons_free_list;
654 ao_scheme_cons_free(struct ao_scheme_cons *cons);
657 ao_scheme_cons_write(ao_poly, bool write);
660 ao_scheme_cons_length(struct ao_scheme_cons *cons);
662 struct ao_scheme_cons *
663 ao_scheme_cons_copy(struct ao_scheme_cons *cons);
666 extern const struct ao_scheme_type ao_scheme_string_type;
668 struct ao_scheme_string *
669 ao_scheme_string_copy(struct ao_scheme_string *a);
671 struct ao_scheme_string *
672 ao_scheme_string_make(char *a);
674 struct ao_scheme_string *
675 ao_scheme_atom_to_string(struct ao_scheme_atom *a);
677 struct ao_scheme_string *
678 ao_scheme_string_cat(struct ao_scheme_string *a, struct ao_scheme_string *b);
681 ao_scheme_string_pack(struct ao_scheme_cons *cons);
684 ao_scheme_string_unpack(struct ao_scheme_string *a);
687 ao_scheme_string_write(ao_poly s, bool write);
690 extern const struct ao_scheme_type ao_scheme_atom_type;
692 extern struct ao_scheme_atom *ao_scheme_atoms;
693 extern struct ao_scheme_frame *ao_scheme_frame_global;
694 extern struct ao_scheme_frame *ao_scheme_frame_current;
697 ao_scheme_atom_write(ao_poly a, bool write);
699 struct ao_scheme_atom *
700 ao_scheme_string_to_atom(struct ao_scheme_string *string);
702 struct ao_scheme_atom *
703 ao_scheme_atom_intern(char *name);
706 ao_scheme_atom_ref(ao_poly atom, struct ao_scheme_frame **frame_ref);
709 ao_scheme_atom_get(ao_poly atom);
712 ao_scheme_atom_set(ao_poly atom, ao_poly val);
715 ao_scheme_atom_def(ao_poly atom, ao_poly val);
719 ao_scheme_int_write(ao_poly i, bool write);
721 #ifdef AO_SCHEME_FEATURE_BIGINT
723 ao_scheme_poly_integer(ao_poly p, bool *fail);
726 ao_scheme_integer_poly(int32_t i);
729 ao_scheme_integer_typep(uint8_t t)
731 return (t == AO_SCHEME_INT) || (t == AO_SCHEME_BIGINT);
735 ao_scheme_bigint_write(ao_poly i, bool write);
737 extern const struct ao_scheme_type ao_scheme_bigint_type;
741 #define ao_scheme_poly_integer(a,b) ao_scheme_poly_int(a)
742 #define ao_scheme_integer_poly ao_scheme_int_poly
745 ao_scheme_integer_typep(uint8_t t)
747 return (t == AO_SCHEME_INT);
750 #endif /* AO_SCHEME_FEATURE_BIGINT */
755 ao_scheme_vector_write(ao_poly v, bool write);
757 struct ao_scheme_vector *
758 ao_scheme_vector_alloc(uint16_t length, ao_poly fill);
761 ao_scheme_vector_get(ao_poly v, ao_poly i);
764 ao_scheme_vector_set(ao_poly v, ao_poly i, ao_poly p);
766 struct ao_scheme_vector *
767 ao_scheme_list_to_vector(struct ao_scheme_cons *cons);
769 struct ao_scheme_cons *
770 ao_scheme_vector_to_list(struct ao_scheme_vector *vector);
772 extern const struct ao_scheme_type ao_scheme_vector_type;
775 void (*ao_scheme_poly_write_func(ao_poly p))(ao_poly p, bool write);
778 ao_scheme_poly_write(ao_poly p, bool write) { (*ao_scheme_poly_write_func(p))(p, write); }
781 ao_scheme_poly_mark(ao_poly p, uint8_t note_cons);
783 /* returns 1 if the object has already been moved */
785 ao_scheme_poly_move(ao_poly *p, uint8_t note_cons);
790 ao_scheme_eval_clear_globals(void);
793 ao_scheme_eval_restart(void);
796 ao_scheme_eval(ao_poly p);
799 ao_scheme_set_cond(struct ao_scheme_cons *cons);
802 #ifdef AO_SCHEME_FEATURE_FLOAT
803 extern const struct ao_scheme_type ao_scheme_float_type;
806 ao_scheme_float_write(ao_poly p, bool write);
809 ao_scheme_float_get(float value);
812 #ifdef AO_SCHEME_FEATURE_FLOAT
813 static inline uint8_t
814 ao_scheme_number_typep(uint8_t t)
816 return ao_scheme_integer_typep(t) || (t == AO_SCHEME_FLOAT);
819 #define ao_scheme_number_typep ao_scheme_integer_typep
824 ao_scheme_builtin_write(ao_poly b, bool write);
826 extern const struct ao_scheme_type ao_scheme_builtin_type;
828 /* Check argument count */
830 ao_scheme_check_argc(ao_poly name, struct ao_scheme_cons *cons, int min, int max);
832 /* Check argument type */
834 ao_scheme_check_argt(ao_poly name, struct ao_scheme_cons *cons, int argc, int type, int nil_ok);
836 /* Fetch an arg (nil if off the end) */
838 ao_scheme_arg(struct ao_scheme_cons *cons, int argc);
841 ao_scheme_args_name(uint8_t args);
844 extern int ao_scheme_read_list;
845 extern struct ao_scheme_cons *ao_scheme_read_cons;
846 extern struct ao_scheme_cons *ao_scheme_read_cons_tail;
847 extern struct ao_scheme_cons *ao_scheme_read_stack;
850 ao_scheme_read(void);
854 ao_scheme_read_eval_print(void);
857 extern const struct ao_scheme_type ao_scheme_frame_type;
858 extern const struct ao_scheme_type ao_scheme_frame_vals_type;
860 #define AO_SCHEME_FRAME_FREE 6
862 extern struct ao_scheme_frame *ao_scheme_frame_free_list[AO_SCHEME_FRAME_FREE];
865 ao_scheme_frame_mark(struct ao_scheme_frame *frame);
868 ao_scheme_frame_ref(struct ao_scheme_frame *frame, ao_poly atom);
870 struct ao_scheme_frame *
871 ao_scheme_frame_new(int num);
874 ao_scheme_frame_free(struct ao_scheme_frame *frame);
877 ao_scheme_frame_bind(struct ao_scheme_frame *frame, int num, ao_poly atom, ao_poly val);
880 ao_scheme_frame_add(struct ao_scheme_frame *frame, ao_poly atom, ao_poly val);
883 ao_scheme_frame_write(ao_poly p, bool write);
886 ao_scheme_frame_init(void);
889 extern const struct ao_scheme_type ao_scheme_lambda_type;
891 extern const char * const ao_scheme_state_names[];
893 struct ao_scheme_lambda *
894 ao_scheme_lambda_new(ao_poly cons);
897 ao_scheme_lambda_write(ao_poly lambda, bool write);
900 ao_scheme_lambda_eval(void);
904 extern const struct ao_scheme_type ao_scheme_stack_type;
905 extern struct ao_scheme_stack *ao_scheme_stack;
906 extern struct ao_scheme_stack *ao_scheme_stack_free_list;
908 extern int ao_scheme_frame_print_indent;
911 ao_scheme_stack_reset(struct ao_scheme_stack *stack);
914 ao_scheme_stack_push(void);
917 ao_scheme_stack_pop(void);
920 ao_scheme_stack_clear(void);
923 ao_scheme_stack_write(ao_poly stack, bool write);
926 ao_scheme_stack_eval(void);
931 ao_scheme_vprintf(const char *format, va_list args);
934 ao_scheme_printf(const char *format, ...);
937 ao_scheme_error(int error, const char *format, ...);
941 #define AO_SCHEME_BUILTIN_DECLS
942 #include "ao_scheme_builtin.h"
944 /* debugging macros */
946 #if DBG_EVAL || DBG_READ
947 int ao_scheme_stack_depth;
952 #define DBG_INDENT() do { int _s; for(_s = 0; _s < ao_scheme_stack_depth; _s++) printf(" "); } while(0)
953 #define DBG_IN() (++ao_scheme_stack_depth)
954 #define DBG_OUT() (--ao_scheme_stack_depth)
955 #define DBG_RESET() (ao_scheme_stack_depth = 0)
956 #define DBG(...) ao_scheme_printf(__VA_ARGS__)
957 #define DBGI(...) do { printf("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
958 #define DBG_CONS(a) ao_scheme_cons_write(ao_scheme_cons_poly(a), true)
959 #define DBG_POLY(a) ao_scheme_poly_write(a, true)
960 #define OFFSET(a) ((a) ? (int) ((uint8_t *) a - ao_scheme_pool) : -1)
961 #define DBG_STACK() ao_scheme_stack_write(ao_scheme_stack_poly(ao_scheme_stack), true)
963 ao_scheme_frames_dump(void)
965 struct ao_scheme_stack *s;
966 DBGI(".. current frame: "); DBG_POLY(ao_scheme_frame_poly(ao_scheme_frame_current)); DBG("\n");
967 for (s = ao_scheme_stack; s; s = ao_scheme_poly_stack(s->prev)) {
968 DBGI(".. stack frame: "); DBG_POLY(s->frame); DBG("\n");
971 #define DBG_FRAMES() ao_scheme_frames_dump()
987 #define RDBGI(...) do { printf("%4d: ", __LINE__); DBG_INDENT(); ao_scheme_printf(__VA_ARGS__); } while (0)
988 #define RDBG_IN() (++ao_scheme_stack_depth)
989 #define RDBG_OUT() (--ao_scheme_stack_depth)
997 ao_scheme_mdbg_offset(void *a)
1004 if (ao_scheme_pool <= u && u < ao_scheme_pool + AO_SCHEME_POOL)
1005 return u - ao_scheme_pool;
1007 #ifndef AO_SCHEME_MAKE_CONST
1008 if (ao_scheme_const <= u && u < ao_scheme_const + AO_SCHEME_POOL_CONST)
1009 return - (int) (u - ao_scheme_const);
1014 #define MDBG_OFFSET(a) ao_scheme_mdbg_offset(a)
1018 #define DBG_MEM_START 1
1021 extern int dbg_move_depth;
1026 #define MDBG_DO(a) a
1027 #define MDBG_MOVE(...) do { if (dbg_mem) { int d; for (d = 0; d < dbg_move_depth; d++) printf (" "); printf(__VA_ARGS__); } } while (0)
1028 #define MDBG_MORE(...) do { if (dbg_mem) printf(__VA_ARGS__); } while (0)
1029 #define MDBG_MOVE_IN() (dbg_move_depth++)
1030 #define MDBG_MOVE_OUT() (assert(--dbg_move_depth >= 0))
1035 #define MDBG_MOVE(...)
1036 #define MDBG_MORE(...)
1037 #define MDBG_MOVE_IN()
1038 #define MDBG_MOVE_OUT()
1042 #endif /* _AO_SCHEME_H_ */