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
27 #define AO_SCHEME_BUILTIN_FEATURES
28 #include "ao_scheme_builtin.h"
29 #undef AO_SCHEME_BUILTIN_FEATURES
30 #include <ao_scheme_os.h>
35 typedef uint16_t ao_poly;
36 typedef int16_t ao_signed_poly;
40 struct ao_scheme_os_save {
43 uint16_t const_checksum;
44 uint16_t const_checksum_inv;
47 #ifndef AO_SCHEME_POOL_TOTAL
48 #error Must define AO_SCHEME_POOL_TOTAL for AO_SCHEME_SAVE
51 #define AO_SCHEME_POOL_EXTRA (sizeof(struct ao_scheme_os_save))
52 #define AO_SCHEME_POOL ((int) (AO_SCHEME_POOL_TOTAL - AO_SCHEME_POOL_EXTRA))
55 ao_scheme_os_save(void);
58 ao_scheme_os_restore_save(struct ao_scheme_os_save *save, int offset);
61 ao_scheme_os_restore(void);
65 #ifdef AO_SCHEME_MAKE_CONST
66 #define AO_SCHEME_POOL_CONST 16384
67 extern uint8_t ao_scheme_const[AO_SCHEME_POOL_CONST] __attribute__((aligned(4)));
68 #define ao_scheme_pool ao_scheme_const
69 #define AO_SCHEME_POOL AO_SCHEME_POOL_CONST
71 #define _atom(n) ao_scheme_atom_poly(ao_scheme_atom_intern((char *) n))
72 #define _bool(v) ao_scheme_bool_poly(ao_scheme_bool_get(v))
74 #define _ao_scheme_bool_true _bool(1)
75 #define _ao_scheme_bool_false _bool(0)
77 #define _ao_scheme_atom_eof _atom("eof")
78 #define _ao_scheme_atom_else _atom("else")
80 #define AO_SCHEME_BUILTIN_ATOMS
81 #include "ao_scheme_builtin.h"
84 #include "ao_scheme_const.h"
85 #ifndef AO_SCHEME_POOL
86 #error Must define AO_SCHEME_POOL
88 #ifndef AO_SCHEME_POOL_EXTRA
89 #define AO_SCHEME_POOL_EXTRA 0
91 extern uint8_t ao_scheme_pool[AO_SCHEME_POOL + AO_SCHEME_POOL_EXTRA] __attribute__((aligned(4)));
95 #define AO_SCHEME_CONS 0
96 #define AO_SCHEME_INT 1
97 #define AO_SCHEME_BIGINT 2
98 #define AO_SCHEME_OTHER 3
100 #define AO_SCHEME_TYPE_MASK 0x0003
101 #define AO_SCHEME_TYPE_SHIFT 2
102 #define AO_SCHEME_REF_MASK 0x7ffc
103 #define AO_SCHEME_CONST 0x8000
105 /* These have a type value at the start of the struct */
106 #define AO_SCHEME_ATOM 4
107 #define AO_SCHEME_BUILTIN 5
108 #define AO_SCHEME_FRAME 6
109 #define AO_SCHEME_FRAME_VALS 7
110 #define AO_SCHEME_LAMBDA 8
111 #define AO_SCHEME_STACK 9
112 #define AO_SCHEME_BOOL 10
113 #define AO_SCHEME_STRING 11
114 #ifdef AO_SCHEME_FEATURE_FLOAT
115 #define AO_SCHEME_FLOAT 12
116 #define _AO_SCHEME_FLOAT AO_SCHEME_FLOAT
118 #define _AO_SCHEME_FLOAT 12
120 #ifdef AO_SCHEME_FEATURE_VECTOR
121 #define AO_SCHEME_VECTOR 13
122 #define _AO_SCHEME_VECTOR AO_SCHEME_VECTOR
124 #define _AO_SCHEME_VECTOR _AO_SCHEME_FLOAT
126 #define AO_SCHEME_NUM_TYPE (_AO_SCHEME_VECTOR+1)
128 /* Leave two bits for types to use as they please */
129 #define AO_SCHEME_OTHER_TYPE_MASK 0x3f
131 #define AO_SCHEME_NIL 0
133 extern uint16_t ao_scheme_top;
135 #define AO_SCHEME_OOM 0x01
136 #define AO_SCHEME_DIVIDE_BY_ZERO 0x02
137 #define AO_SCHEME_INVALID 0x04
138 #define AO_SCHEME_UNDEFINED 0x08
139 #define AO_SCHEME_REDEFINED 0x10
140 #define AO_SCHEME_EOF 0x20
141 #define AO_SCHEME_EXIT 0x40
143 extern uint8_t ao_scheme_exception;
146 ao_scheme_is_const(ao_poly poly) {
147 return poly & AO_SCHEME_CONST;
150 #define AO_SCHEME_IS_CONST(a) (ao_scheme_const <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_scheme_const + AO_SCHEME_POOL_CONST)
151 #define AO_SCHEME_IS_POOL(a) (ao_scheme_pool <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_scheme_pool + AO_SCHEME_POOL)
152 #define AO_SCHEME_IS_INT(p) (ao_scheme_poly_base_type(p) == AO_SCHEME_INT)
155 ao_scheme_ref(ao_poly poly);
158 ao_scheme_poly(const void *addr, ao_poly type);
160 struct ao_scheme_type {
161 int (*size)(void *addr);
162 void (*mark)(void *addr);
163 void (*move)(void *addr);
167 struct ao_scheme_cons {
172 struct ao_scheme_atom {
179 struct ao_scheme_string {
184 struct ao_scheme_val {
189 struct ao_scheme_frame_vals {
192 struct ao_scheme_val vals[];
195 struct ao_scheme_frame {
202 struct ao_scheme_bool {
209 #ifdef AO_SCHEME_FEATURE_FLOAT
210 struct ao_scheme_float {
218 #ifdef AO_SCHEME_FEATURE_VECTOR
219 struct ao_scheme_vector {
227 #define AO_SCHEME_MIN_INT (-(1 << (15 - AO_SCHEME_TYPE_SHIFT)))
228 #define AO_SCHEME_MAX_INT ((1 << (15 - AO_SCHEME_TYPE_SHIFT)) - 1)
230 #ifdef AO_SCHEME_FEATURE_BIGINT
232 struct ao_scheme_bigint {
236 #define AO_SCHEME_MIN_BIGINT INT32_MIN
237 #define AO_SCHEME_MAX_BIGINT INT32_MAX
239 #endif /* AO_SCHEME_FEATURE_BIGINT */
241 /* Set on type when the frame escapes the lambda */
242 #define AO_SCHEME_FRAME_MARK 0x80
243 #define AO_SCHEME_FRAME_PRINT 0x40
245 static inline int ao_scheme_frame_marked(struct ao_scheme_frame *f) {
246 return f->type & AO_SCHEME_FRAME_MARK;
249 static inline struct ao_scheme_frame *
250 ao_scheme_poly_frame(ao_poly poly) {
251 return ao_scheme_ref(poly);
254 static inline ao_poly
255 ao_scheme_frame_poly(struct ao_scheme_frame *frame) {
256 return ao_scheme_poly(frame, AO_SCHEME_OTHER);
259 static inline struct ao_scheme_frame_vals *
260 ao_scheme_poly_frame_vals(ao_poly poly) {
261 return ao_scheme_ref(poly);
264 static inline ao_poly
265 ao_scheme_frame_vals_poly(struct ao_scheme_frame_vals *vals) {
266 return ao_scheme_poly(vals, AO_SCHEME_OTHER);
270 eval_sexpr, /* Evaluate an sexpr */
271 eval_val, /* Value computed */
272 eval_formal, /* Formal computed */
273 eval_exec, /* Start a lambda evaluation */
274 eval_apply, /* Execute apply */
275 eval_cond, /* Start next cond clause */
276 eval_cond_test, /* Check cond condition */
277 eval_begin, /* Start next begin entry */
278 eval_while, /* Start while condition */
279 eval_while_test, /* Check while condition */
280 eval_macro, /* Finished with macro generation */
283 struct ao_scheme_stack {
284 uint8_t type; /* AO_SCHEME_STACK */
285 uint8_t state; /* enum eval_state */
286 ao_poly prev; /* previous stack frame */
287 ao_poly sexprs; /* expressions to evaluate */
288 ao_poly values; /* values computed */
289 ao_poly values_tail; /* end of the values list for easy appending */
290 ao_poly frame; /* current lookup frame */
291 ao_poly list; /* most recent function call */
294 #define AO_SCHEME_STACK_MARK 0x80 /* set on type when a reference has been taken */
295 #define AO_SCHEME_STACK_PRINT 0x40 /* stack is being printed */
297 static inline int ao_scheme_stack_marked(struct ao_scheme_stack *s) {
298 return s->type & AO_SCHEME_STACK_MARK;
301 static inline void ao_scheme_stack_mark(struct ao_scheme_stack *s) {
302 s->type |= AO_SCHEME_STACK_MARK;
305 static inline struct ao_scheme_stack *
306 ao_scheme_poly_stack(ao_poly p)
308 return ao_scheme_ref(p);
311 static inline ao_poly
312 ao_scheme_stack_poly(struct ao_scheme_stack *stack)
314 return ao_scheme_poly(stack, AO_SCHEME_OTHER);
317 extern ao_poly ao_scheme_v;
319 #define AO_SCHEME_FUNC_LAMBDA 0
320 #define AO_SCHEME_FUNC_NLAMBDA 1
321 #define AO_SCHEME_FUNC_MACRO 2
323 #define AO_SCHEME_FUNC_FREE_ARGS 0x80
324 #define AO_SCHEME_FUNC_MASK 0x7f
326 #define AO_SCHEME_FUNC_F_LAMBDA (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_LAMBDA)
327 #define AO_SCHEME_FUNC_F_NLAMBDA (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_NLAMBDA)
328 #define AO_SCHEME_FUNC_F_MACRO (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_MACRO)
330 struct ao_scheme_builtin {
336 #define AO_SCHEME_BUILTIN_ID
337 #include "ao_scheme_builtin.h"
339 typedef ao_poly (*ao_scheme_func_t)(struct ao_scheme_cons *cons);
341 extern const ao_scheme_func_t ao_scheme_builtins[];
343 static inline ao_scheme_func_t
344 ao_scheme_func(struct ao_scheme_builtin *b)
346 return ao_scheme_builtins[b->func];
349 struct ao_scheme_lambda {
356 static inline struct ao_scheme_lambda *
357 ao_scheme_poly_lambda(ao_poly poly)
359 return ao_scheme_ref(poly);
362 static inline ao_poly
363 ao_scheme_lambda_poly(struct ao_scheme_lambda *lambda)
365 return ao_scheme_poly(lambda, AO_SCHEME_OTHER);
369 ao_scheme_poly_other(ao_poly poly) {
370 return ao_scheme_ref(poly);
373 static inline uint8_t
374 ao_scheme_other_type(void *other) {
376 if ((*((uint8_t *) other) & AO_SCHEME_OTHER_TYPE_MASK) >= AO_SCHEME_NUM_TYPE)
379 return *((uint8_t *) other) & AO_SCHEME_OTHER_TYPE_MASK;
382 static inline ao_poly
383 ao_scheme_other_poly(const void *other)
385 return ao_scheme_poly(other, AO_SCHEME_OTHER);
389 ao_scheme_size_round(int size)
391 return (size + 3) & ~3;
395 ao_scheme_size(const struct ao_scheme_type *type, void *addr)
397 return ao_scheme_size_round(type->size(addr));
400 #define AO_SCHEME_OTHER_POLY(other) ((ao_poly)(other) + AO_SCHEME_OTHER)
402 static inline int ao_scheme_poly_base_type(ao_poly poly) {
403 return poly & AO_SCHEME_TYPE_MASK;
406 static inline int ao_scheme_poly_type(ao_poly poly) {
407 int type = poly & AO_SCHEME_TYPE_MASK;
408 if (type == AO_SCHEME_OTHER)
409 return ao_scheme_other_type(ao_scheme_poly_other(poly));
414 ao_scheme_is_cons(ao_poly poly) {
415 return (ao_scheme_poly_base_type(poly) == AO_SCHEME_CONS);
419 ao_scheme_is_pair(ao_poly poly) {
420 return poly != AO_SCHEME_NIL && (ao_scheme_poly_base_type(poly) == AO_SCHEME_CONS);
423 static inline struct ao_scheme_cons *
424 ao_scheme_poly_cons(ao_poly poly)
426 return ao_scheme_ref(poly);
429 static inline ao_poly
430 ao_scheme_cons_poly(struct ao_scheme_cons *cons)
432 return ao_scheme_poly(cons, AO_SCHEME_CONS);
435 static inline int32_t
436 ao_scheme_poly_int(ao_poly poly)
438 return (int32_t) ((ao_signed_poly) poly >> AO_SCHEME_TYPE_SHIFT);
441 static inline ao_poly
442 ao_scheme_int_poly(int32_t i)
444 return ((ao_poly) i << 2) | AO_SCHEME_INT;
447 #ifdef AO_SCHEME_FEATURE_BIGINT
448 static inline struct ao_scheme_bigint *
449 ao_scheme_poly_bigint(ao_poly poly)
451 return ao_scheme_ref(poly);
454 static inline ao_poly
455 ao_scheme_bigint_poly(struct ao_scheme_bigint *bi)
457 return ao_scheme_poly(bi, AO_SCHEME_BIGINT);
459 #endif /* AO_SCHEME_FEATURE_BIGINT */
461 static inline struct ao_scheme_string *
462 ao_scheme_poly_string(ao_poly poly)
464 return ao_scheme_ref(poly);
467 static inline ao_poly
468 ao_scheme_string_poly(struct ao_scheme_string *s)
470 return ao_scheme_poly(s, AO_SCHEME_OTHER);
473 static inline struct ao_scheme_atom *
474 ao_scheme_poly_atom(ao_poly poly)
476 return ao_scheme_ref(poly);
479 static inline ao_poly
480 ao_scheme_atom_poly(struct ao_scheme_atom *a)
482 return ao_scheme_poly(a, AO_SCHEME_OTHER);
485 static inline struct ao_scheme_builtin *
486 ao_scheme_poly_builtin(ao_poly poly)
488 return ao_scheme_ref(poly);
491 static inline ao_poly
492 ao_scheme_builtin_poly(struct ao_scheme_builtin *b)
494 return ao_scheme_poly(b, AO_SCHEME_OTHER);
497 static inline ao_poly
498 ao_scheme_bool_poly(struct ao_scheme_bool *b)
500 return ao_scheme_poly(b, AO_SCHEME_OTHER);
503 static inline struct ao_scheme_bool *
504 ao_scheme_poly_bool(ao_poly poly)
506 return ao_scheme_ref(poly);
509 #ifdef AO_SCHEME_FEATURE_FLOAT
510 static inline ao_poly
511 ao_scheme_float_poly(struct ao_scheme_float *f)
513 return ao_scheme_poly(f, AO_SCHEME_OTHER);
516 static inline struct ao_scheme_float *
517 ao_scheme_poly_float(ao_poly poly)
519 return ao_scheme_ref(poly);
523 ao_scheme_poly_number(ao_poly p);
526 #ifdef AO_SCHEME_FEATURE_VECTOR
527 static inline ao_poly
528 ao_scheme_vector_poly(struct ao_scheme_vector *v)
530 return ao_scheme_poly(v, AO_SCHEME_OTHER);
533 static inline struct ao_scheme_vector *
534 ao_scheme_poly_vector(ao_poly poly)
536 return ao_scheme_ref(poly);
540 /* memory functions */
542 extern uint64_t ao_scheme_collects[2];
543 extern uint64_t ao_scheme_freed[2];
544 extern uint64_t ao_scheme_loops[2];
546 /* returns 1 if the object was already marked */
548 ao_scheme_mark_memory(const struct ao_scheme_type *type, void *addr);
550 /* returns 1 if the object was already moved */
552 ao_scheme_move_memory(const struct ao_scheme_type *type, void **ref);
555 ao_scheme_alloc(int size);
557 #define AO_SCHEME_COLLECT_FULL 1
558 #define AO_SCHEME_COLLECT_INCREMENTAL 0
561 ao_scheme_collect(uint8_t style);
565 ao_scheme_cons_check(struct ao_scheme_cons *cons);
569 ao_scheme_cons_stash(int id, struct ao_scheme_cons *cons);
571 struct ao_scheme_cons *
572 ao_scheme_cons_fetch(int id);
575 ao_scheme_poly_stash(int id, ao_poly poly);
578 ao_scheme_poly_fetch(int id);
581 ao_scheme_string_stash(int id, struct ao_scheme_string *string);
583 struct ao_scheme_string *
584 ao_scheme_string_fetch(int id);
587 ao_scheme_stack_stash(int id, struct ao_scheme_stack *stack) {
588 ao_scheme_poly_stash(id, ao_scheme_stack_poly(stack));
591 static inline struct ao_scheme_stack *
592 ao_scheme_stack_fetch(int id) {
593 return ao_scheme_poly_stack(ao_scheme_poly_fetch(id));
597 ao_scheme_frame_stash(int id, struct ao_scheme_frame *frame);
599 struct ao_scheme_frame *
600 ao_scheme_frame_fetch(int id);
604 extern const struct ao_scheme_type ao_scheme_bool_type;
607 ao_scheme_bool_write(ao_poly v);
609 #ifdef AO_SCHEME_MAKE_CONST
610 extern struct ao_scheme_bool *ao_scheme_true, *ao_scheme_false;
612 struct ao_scheme_bool *
613 ao_scheme_bool_get(uint8_t value);
617 extern const struct ao_scheme_type ao_scheme_cons_type;
619 struct ao_scheme_cons *
620 ao_scheme_cons_cons(ao_poly car, ao_poly cdr);
622 /* Return a cons or NULL for a proper list, else error */
623 struct ao_scheme_cons *
624 ao_scheme_cons_cdr(struct ao_scheme_cons *cons);
627 ao_scheme__cons(ao_poly car, ao_poly cdr);
629 extern struct ao_scheme_cons *ao_scheme_cons_free_list;
632 ao_scheme_cons_free(struct ao_scheme_cons *cons);
635 ao_scheme_cons_write(ao_poly);
638 ao_scheme_cons_display(ao_poly);
641 ao_scheme_cons_length(struct ao_scheme_cons *cons);
643 struct ao_scheme_cons *
644 ao_scheme_cons_copy(struct ao_scheme_cons *cons);
647 extern const struct ao_scheme_type ao_scheme_string_type;
649 struct ao_scheme_string *
650 ao_scheme_string_copy(struct ao_scheme_string *a);
652 struct ao_scheme_string *
653 ao_scheme_string_make(char *a);
655 struct ao_scheme_string *
656 ao_scheme_atom_to_string(struct ao_scheme_atom *a);
658 struct ao_scheme_string *
659 ao_scheme_string_cat(struct ao_scheme_string *a, struct ao_scheme_string *b);
662 ao_scheme_string_pack(struct ao_scheme_cons *cons);
665 ao_scheme_string_unpack(struct ao_scheme_string *a);
668 ao_scheme_string_write(ao_poly s);
671 ao_scheme_string_display(ao_poly s);
674 extern const struct ao_scheme_type ao_scheme_atom_type;
676 extern struct ao_scheme_atom *ao_scheme_atoms;
677 extern struct ao_scheme_frame *ao_scheme_frame_global;
678 extern struct ao_scheme_frame *ao_scheme_frame_current;
681 ao_scheme_atom_write(ao_poly a);
683 struct ao_scheme_atom *
684 ao_scheme_string_to_atom(struct ao_scheme_string *string);
686 struct ao_scheme_atom *
687 ao_scheme_atom_intern(char *name);
690 ao_scheme_atom_ref(ao_poly atom, struct ao_scheme_frame **frame_ref);
693 ao_scheme_atom_get(ao_poly atom);
696 ao_scheme_atom_set(ao_poly atom, ao_poly val);
699 ao_scheme_atom_def(ao_poly atom, ao_poly val);
703 ao_scheme_int_write(ao_poly i);
705 #ifdef AO_SCHEME_FEATURE_BIGINT
707 ao_scheme_poly_integer(ao_poly p, bool *fail);
710 ao_scheme_integer_poly(int32_t i);
713 ao_scheme_integer_typep(uint8_t t)
715 return (t == AO_SCHEME_INT) || (t == AO_SCHEME_BIGINT);
719 ao_scheme_bigint_write(ao_poly i);
721 extern const struct ao_scheme_type ao_scheme_bigint_type;
725 #define ao_scheme_poly_integer(a,b) ao_scheme_poly_int(a)
726 #define ao_scheme_integer_poly ao_scheme_int_poly
729 ao_scheme_integer_typep(uint8_t t)
731 return (t == AO_SCHEME_INT);
734 #endif /* AO_SCHEME_FEATURE_BIGINT */
739 ao_scheme_vector_write(ao_poly v);
742 ao_scheme_vector_display(ao_poly v);
744 struct ao_scheme_vector *
745 ao_scheme_vector_alloc(uint16_t length, ao_poly fill);
748 ao_scheme_vector_get(ao_poly v, ao_poly i);
751 ao_scheme_vector_set(ao_poly v, ao_poly i, ao_poly p);
753 struct ao_scheme_vector *
754 ao_scheme_list_to_vector(struct ao_scheme_cons *cons);
756 struct ao_scheme_cons *
757 ao_scheme_vector_to_list(struct ao_scheme_vector *vector);
759 extern const struct ao_scheme_type ao_scheme_vector_type;
762 void (*ao_scheme_poly_write_func(ao_poly p))(ao_poly p);
763 void (*ao_scheme_poly_display_func(ao_poly p))(ao_poly p);
766 ao_scheme_poly_write(ao_poly p) { (*ao_scheme_poly_write_func(p))(p); }
769 ao_scheme_poly_display(ao_poly p) { (*ao_scheme_poly_display_func(p))(p); }
772 ao_scheme_poly_mark(ao_poly p, uint8_t note_cons);
774 /* returns 1 if the object has already been moved */
776 ao_scheme_poly_move(ao_poly *p, uint8_t note_cons);
781 ao_scheme_eval_clear_globals(void);
784 ao_scheme_eval_restart(void);
787 ao_scheme_eval(ao_poly p);
790 ao_scheme_set_cond(struct ao_scheme_cons *cons);
793 #ifdef AO_SCHEME_FEATURE_FLOAT
794 extern const struct ao_scheme_type ao_scheme_float_type;
797 ao_scheme_float_write(ao_poly p);
800 ao_scheme_float_get(float value);
803 #ifdef AO_SCHEME_FEATURE_FLOAT
804 static inline uint8_t
805 ao_scheme_number_typep(uint8_t t)
807 return ao_scheme_integer_typep(t) || (t == AO_SCHEME_FLOAT);
810 #define ao_scheme_number_typep ao_scheme_integer_typep
815 ao_scheme_builtin_write(ao_poly b);
817 extern const struct ao_scheme_type ao_scheme_builtin_type;
819 /* Check argument count */
821 ao_scheme_check_argc(ao_poly name, struct ao_scheme_cons *cons, int min, int max);
823 /* Check argument type */
825 ao_scheme_check_argt(ao_poly name, struct ao_scheme_cons *cons, int argc, int type, int nil_ok);
827 /* Fetch an arg (nil if off the end) */
829 ao_scheme_arg(struct ao_scheme_cons *cons, int argc);
832 ao_scheme_args_name(uint8_t args);
835 extern int ao_scheme_read_list;
836 extern struct ao_scheme_cons *ao_scheme_read_cons;
837 extern struct ao_scheme_cons *ao_scheme_read_cons_tail;
838 extern struct ao_scheme_cons *ao_scheme_read_stack;
841 ao_scheme_read(void);
845 ao_scheme_read_eval_print(void);
848 extern const struct ao_scheme_type ao_scheme_frame_type;
849 extern const struct ao_scheme_type ao_scheme_frame_vals_type;
851 #define AO_SCHEME_FRAME_FREE 6
853 extern struct ao_scheme_frame *ao_scheme_frame_free_list[AO_SCHEME_FRAME_FREE];
856 ao_scheme_frame_mark(struct ao_scheme_frame *frame);
859 ao_scheme_frame_ref(struct ao_scheme_frame *frame, ao_poly atom);
861 struct ao_scheme_frame *
862 ao_scheme_frame_new(int num);
865 ao_scheme_frame_free(struct ao_scheme_frame *frame);
868 ao_scheme_frame_bind(struct ao_scheme_frame *frame, int num, ao_poly atom, ao_poly val);
871 ao_scheme_frame_add(struct ao_scheme_frame *frame, ao_poly atom, ao_poly val);
874 ao_scheme_frame_write(ao_poly p);
877 ao_scheme_frame_init(void);
880 extern const struct ao_scheme_type ao_scheme_lambda_type;
882 extern const char * const ao_scheme_state_names[];
884 struct ao_scheme_lambda *
885 ao_scheme_lambda_new(ao_poly cons);
888 ao_scheme_lambda_write(ao_poly lambda);
891 ao_scheme_lambda_eval(void);
895 extern const struct ao_scheme_type ao_scheme_stack_type;
896 extern struct ao_scheme_stack *ao_scheme_stack;
897 extern struct ao_scheme_stack *ao_scheme_stack_free_list;
900 ao_scheme_stack_reset(struct ao_scheme_stack *stack);
903 ao_scheme_stack_push(void);
906 ao_scheme_stack_pop(void);
909 ao_scheme_stack_clear(void);
912 ao_scheme_stack_write(ao_poly stack);
915 ao_scheme_stack_eval(void);
920 ao_scheme_vprintf(const char *format, va_list args);
923 ao_scheme_printf(const char *format, ...);
926 ao_scheme_error_poly(const char *name, ao_poly poly, ao_poly last);
929 ao_scheme_error_frame(int indent, const char *name, struct ao_scheme_frame *frame);
932 ao_scheme_error(int error, const char *format, ...);
936 #define AO_SCHEME_BUILTIN_DECLS
937 #include "ao_scheme_builtin.h"
939 /* debugging macros */
941 #if DBG_EVAL || DBG_READ || DBG_MEM
943 int ao_scheme_stack_depth;
945 #define DBG_INDENT() do { int _s; for(_s = 0; _s < ao_scheme_stack_depth; _s++) printf(" "); } while(0)
946 #define DBG_IN() (++ao_scheme_stack_depth)
947 #define DBG_OUT() (--ao_scheme_stack_depth)
948 #define DBG_RESET() (ao_scheme_stack_depth = 0)
949 #define DBG(...) ao_scheme_printf(__VA_ARGS__)
950 #define DBGI(...) do { printf("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
951 #define DBG_CONS(a) ao_scheme_cons_write(ao_scheme_cons_poly(a))
952 #define DBG_POLY(a) ao_scheme_poly_write(a)
953 #define OFFSET(a) ((a) ? (int) ((uint8_t *) a - ao_scheme_pool) : -1)
954 #define DBG_STACK() ao_scheme_stack_write(ao_scheme_stack_poly(ao_scheme_stack))
956 ao_scheme_frames_dump(void)
958 struct ao_scheme_stack *s;
959 DBGI(".. current frame: "); DBG_POLY(ao_scheme_frame_poly(ao_scheme_frame_current)); DBG("\n");
960 for (s = ao_scheme_stack; s; s = ao_scheme_poly_stack(s->prev)) {
961 DBGI(".. stack frame: "); DBG_POLY(s->frame); DBG("\n");
964 #define DBG_FRAMES() ao_scheme_frames_dump()
980 #define RDBGI(...) DBGI(__VA_ARGS__)
981 #define RDBG_IN() DBG_IN()
982 #define RDBG_OUT() DBG_OUT()
989 #define DBG_MEM_START 1
994 extern int dbg_move_depth;
996 #define MDBG_OFFSET(a) ((a) ? (int) ((uint8_t *) (a) - ao_scheme_pool) : -1)
1000 #define MDBG_DO(a) DBG_DO(a)
1001 #define MDBG_MOVE(...) do { if (dbg_mem) { int d; for (d = 0; d < dbg_move_depth; d++) printf (" "); printf(__VA_ARGS__); } } while (0)
1002 #define MDBG_MORE(...) do { if (dbg_mem) printf(__VA_ARGS__); } while (0)
1003 #define MDBG_MOVE_IN() (dbg_move_depth++)
1004 #define MDBG_MOVE_OUT() (assert(--dbg_move_depth >= 0))
1009 #define MDBG_MOVE(...)
1010 #define MDBG_MORE(...)
1011 #define MDBG_MOVE_IN()
1012 #define MDBG_MOVE_OUT()
1016 #endif /* _AO_SCHEME_H_ */