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)
153 #define AO_SCHEME_IS_CONS(p) (ao_scheme_poly_base_type(p) == AO_SCHEME_CONS)
156 ao_scheme_ref(ao_poly poly);
159 ao_scheme_poly(const void *addr, ao_poly type);
161 struct ao_scheme_type {
162 int (*size)(void *addr);
163 void (*mark)(void *addr);
164 void (*move)(void *addr);
168 struct ao_scheme_cons {
173 struct ao_scheme_atom {
180 struct ao_scheme_string {
185 struct ao_scheme_val {
190 struct ao_scheme_frame_vals {
193 struct ao_scheme_val vals[];
196 struct ao_scheme_frame {
203 struct ao_scheme_bool {
210 #ifdef AO_SCHEME_FEATURE_FLOAT
211 struct ao_scheme_float {
219 #ifdef AO_SCHEME_FEATURE_VECTOR
220 struct ao_scheme_vector {
228 #define AO_SCHEME_MIN_INT (-(1 << (15 - AO_SCHEME_TYPE_SHIFT)))
229 #define AO_SCHEME_MAX_INT ((1 << (15 - AO_SCHEME_TYPE_SHIFT)) - 1)
231 #ifdef AO_SCHEME_FEATURE_BIGINT
233 struct ao_scheme_bigint {
237 #define AO_SCHEME_MIN_BIGINT INT32_MIN
238 #define AO_SCHEME_MAX_BIGINT INT32_MAX
240 #endif /* AO_SCHEME_FEATURE_BIGINT */
242 /* Set on type when the frame escapes the lambda */
243 #define AO_SCHEME_FRAME_MARK 0x80
244 #define AO_SCHEME_FRAME_PRINT 0x40
246 static inline int ao_scheme_frame_marked(struct ao_scheme_frame *f) {
247 return f->type & AO_SCHEME_FRAME_MARK;
250 static inline struct ao_scheme_frame *
251 ao_scheme_poly_frame(ao_poly poly) {
252 return ao_scheme_ref(poly);
255 static inline ao_poly
256 ao_scheme_frame_poly(struct ao_scheme_frame *frame) {
257 return ao_scheme_poly(frame, AO_SCHEME_OTHER);
260 static inline struct ao_scheme_frame_vals *
261 ao_scheme_poly_frame_vals(ao_poly poly) {
262 return ao_scheme_ref(poly);
265 static inline ao_poly
266 ao_scheme_frame_vals_poly(struct ao_scheme_frame_vals *vals) {
267 return ao_scheme_poly(vals, AO_SCHEME_OTHER);
271 eval_sexpr, /* Evaluate an sexpr */
272 eval_val, /* Value computed */
273 eval_formal, /* Formal computed */
274 eval_exec, /* Start a lambda evaluation */
275 eval_apply, /* Execute apply */
276 eval_cond, /* Start next cond clause */
277 eval_cond_test, /* Check cond condition */
278 eval_begin, /* Start next begin entry */
279 eval_while, /* Start while condition */
280 eval_while_test, /* Check while condition */
281 eval_macro, /* Finished with macro generation */
284 struct ao_scheme_stack {
285 uint8_t type; /* AO_SCHEME_STACK */
286 uint8_t state; /* enum eval_state */
287 ao_poly prev; /* previous stack frame */
288 ao_poly sexprs; /* expressions to evaluate */
289 ao_poly values; /* values computed */
290 ao_poly values_tail; /* end of the values list for easy appending */
291 ao_poly frame; /* current lookup frame */
292 ao_poly list; /* most recent function call */
295 #define AO_SCHEME_STACK_MARK 0x80 /* set on type when a reference has been taken */
296 #define AO_SCHEME_STACK_PRINT 0x40 /* stack is being printed */
298 static inline int ao_scheme_stack_marked(struct ao_scheme_stack *s) {
299 return s->type & AO_SCHEME_STACK_MARK;
302 static inline void ao_scheme_stack_mark(struct ao_scheme_stack *s) {
303 s->type |= AO_SCHEME_STACK_MARK;
306 static inline struct ao_scheme_stack *
307 ao_scheme_poly_stack(ao_poly p)
309 return ao_scheme_ref(p);
312 static inline ao_poly
313 ao_scheme_stack_poly(struct ao_scheme_stack *stack)
315 return ao_scheme_poly(stack, AO_SCHEME_OTHER);
318 extern ao_poly ao_scheme_v;
320 #define AO_SCHEME_FUNC_LAMBDA 0
321 #define AO_SCHEME_FUNC_NLAMBDA 1
322 #define AO_SCHEME_FUNC_MACRO 2
324 #define AO_SCHEME_FUNC_FREE_ARGS 0x80
325 #define AO_SCHEME_FUNC_MASK 0x7f
327 #define AO_SCHEME_FUNC_F_LAMBDA (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_LAMBDA)
328 #define AO_SCHEME_FUNC_F_NLAMBDA (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_NLAMBDA)
329 #define AO_SCHEME_FUNC_F_MACRO (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_MACRO)
331 struct ao_scheme_builtin {
337 #define AO_SCHEME_BUILTIN_ID
338 #include "ao_scheme_builtin.h"
340 typedef ao_poly (*ao_scheme_func_t)(struct ao_scheme_cons *cons);
342 extern const ao_scheme_func_t ao_scheme_builtins[];
344 static inline ao_scheme_func_t
345 ao_scheme_func(struct ao_scheme_builtin *b)
347 return ao_scheme_builtins[b->func];
350 struct ao_scheme_lambda {
357 static inline struct ao_scheme_lambda *
358 ao_scheme_poly_lambda(ao_poly poly)
360 return ao_scheme_ref(poly);
363 static inline ao_poly
364 ao_scheme_lambda_poly(struct ao_scheme_lambda *lambda)
366 return ao_scheme_poly(lambda, AO_SCHEME_OTHER);
370 ao_scheme_poly_other(ao_poly poly) {
371 return ao_scheme_ref(poly);
374 static inline uint8_t
375 ao_scheme_other_type(void *other) {
377 if ((*((uint8_t *) other) & AO_SCHEME_OTHER_TYPE_MASK) >= AO_SCHEME_NUM_TYPE)
380 return *((uint8_t *) other) & AO_SCHEME_OTHER_TYPE_MASK;
383 static inline ao_poly
384 ao_scheme_other_poly(const void *other)
386 return ao_scheme_poly(other, AO_SCHEME_OTHER);
390 ao_scheme_size_round(int size)
392 return (size + 3) & ~3;
396 ao_scheme_size(const struct ao_scheme_type *type, void *addr)
398 return ao_scheme_size_round(type->size(addr));
401 #define AO_SCHEME_OTHER_POLY(other) ((ao_poly)(other) + AO_SCHEME_OTHER)
403 static inline int ao_scheme_poly_base_type(ao_poly poly) {
404 return poly & AO_SCHEME_TYPE_MASK;
407 static inline int ao_scheme_poly_type(ao_poly poly) {
408 int type = poly & AO_SCHEME_TYPE_MASK;
409 if (type == AO_SCHEME_OTHER)
410 return ao_scheme_other_type(ao_scheme_poly_other(poly));
415 ao_scheme_is_cons(ao_poly poly) {
416 return (ao_scheme_poly_base_type(poly) == AO_SCHEME_CONS);
420 ao_scheme_is_pair(ao_poly poly) {
421 return poly != AO_SCHEME_NIL && (ao_scheme_poly_base_type(poly) == AO_SCHEME_CONS);
424 static inline struct ao_scheme_cons *
425 ao_scheme_poly_cons(ao_poly poly)
427 return ao_scheme_ref(poly);
430 static inline ao_poly
431 ao_scheme_cons_poly(struct ao_scheme_cons *cons)
433 return ao_scheme_poly(cons, AO_SCHEME_CONS);
436 static inline int32_t
437 ao_scheme_poly_int(ao_poly poly)
439 return (int32_t) ((ao_signed_poly) poly >> AO_SCHEME_TYPE_SHIFT);
442 static inline ao_poly
443 ao_scheme_int_poly(int32_t i)
445 return ((ao_poly) i << 2) | AO_SCHEME_INT;
448 #ifdef AO_SCHEME_FEATURE_BIGINT
449 static inline struct ao_scheme_bigint *
450 ao_scheme_poly_bigint(ao_poly poly)
452 return ao_scheme_ref(poly);
455 static inline ao_poly
456 ao_scheme_bigint_poly(struct ao_scheme_bigint *bi)
458 return ao_scheme_poly(bi, AO_SCHEME_BIGINT);
460 #endif /* AO_SCHEME_FEATURE_BIGINT */
462 static inline struct ao_scheme_string *
463 ao_scheme_poly_string(ao_poly poly)
465 return ao_scheme_ref(poly);
468 static inline ao_poly
469 ao_scheme_string_poly(struct ao_scheme_string *s)
471 return ao_scheme_poly(s, AO_SCHEME_OTHER);
474 static inline struct ao_scheme_atom *
475 ao_scheme_poly_atom(ao_poly poly)
477 return ao_scheme_ref(poly);
480 static inline ao_poly
481 ao_scheme_atom_poly(struct ao_scheme_atom *a)
483 return ao_scheme_poly(a, AO_SCHEME_OTHER);
486 static inline struct ao_scheme_builtin *
487 ao_scheme_poly_builtin(ao_poly poly)
489 return ao_scheme_ref(poly);
492 static inline ao_poly
493 ao_scheme_builtin_poly(struct ao_scheme_builtin *b)
495 return ao_scheme_poly(b, AO_SCHEME_OTHER);
498 static inline ao_poly
499 ao_scheme_bool_poly(struct ao_scheme_bool *b)
501 return ao_scheme_poly(b, AO_SCHEME_OTHER);
504 static inline struct ao_scheme_bool *
505 ao_scheme_poly_bool(ao_poly poly)
507 return ao_scheme_ref(poly);
510 #ifdef AO_SCHEME_FEATURE_FLOAT
511 static inline ao_poly
512 ao_scheme_float_poly(struct ao_scheme_float *f)
514 return ao_scheme_poly(f, AO_SCHEME_OTHER);
517 static inline struct ao_scheme_float *
518 ao_scheme_poly_float(ao_poly poly)
520 return ao_scheme_ref(poly);
524 ao_scheme_poly_number(ao_poly p);
527 #ifdef AO_SCHEME_FEATURE_VECTOR
528 static inline ao_poly
529 ao_scheme_vector_poly(struct ao_scheme_vector *v)
531 return ao_scheme_poly(v, AO_SCHEME_OTHER);
534 static inline struct ao_scheme_vector *
535 ao_scheme_poly_vector(ao_poly poly)
537 return ao_scheme_ref(poly);
541 /* memory functions */
543 extern uint64_t ao_scheme_collects[2];
544 extern uint64_t ao_scheme_freed[2];
545 extern uint64_t ao_scheme_loops[2];
547 /* returns 1 if the object was already marked */
549 ao_scheme_mark_memory(const struct ao_scheme_type *type, void *addr);
551 /* returns 1 if the object was already moved */
553 ao_scheme_move_memory(const struct ao_scheme_type *type, void **ref);
556 ao_scheme_alloc(int size);
558 #define AO_SCHEME_COLLECT_FULL 1
559 #define AO_SCHEME_COLLECT_INCREMENTAL 0
562 ao_scheme_collect(uint8_t style);
566 ao_scheme_cons_check(struct ao_scheme_cons *cons);
570 ao_scheme_cons_stash(int id, struct ao_scheme_cons *cons);
572 struct ao_scheme_cons *
573 ao_scheme_cons_fetch(int id);
576 ao_scheme_poly_stash(int id, ao_poly poly);
579 ao_scheme_poly_fetch(int id);
582 ao_scheme_string_stash(int id, struct ao_scheme_string *string);
584 struct ao_scheme_string *
585 ao_scheme_string_fetch(int id);
588 ao_scheme_stack_stash(int id, struct ao_scheme_stack *stack) {
589 ao_scheme_poly_stash(id, ao_scheme_stack_poly(stack));
592 static inline struct ao_scheme_stack *
593 ao_scheme_stack_fetch(int id) {
594 return ao_scheme_poly_stack(ao_scheme_poly_fetch(id));
598 ao_scheme_frame_stash(int id, struct ao_scheme_frame *frame);
600 struct ao_scheme_frame *
601 ao_scheme_frame_fetch(int id);
605 extern const struct ao_scheme_type ao_scheme_bool_type;
608 ao_scheme_bool_write(ao_poly v);
610 #ifdef AO_SCHEME_MAKE_CONST
611 extern struct ao_scheme_bool *ao_scheme_true, *ao_scheme_false;
613 struct ao_scheme_bool *
614 ao_scheme_bool_get(uint8_t value);
618 extern const struct ao_scheme_type ao_scheme_cons_type;
620 struct ao_scheme_cons *
621 ao_scheme_cons_cons(ao_poly car, ao_poly cdr);
623 /* Return a cons or NULL for a proper list, else error */
624 struct ao_scheme_cons *
625 ao_scheme_cons_cdr(struct ao_scheme_cons *cons);
628 ao_scheme__cons(ao_poly car, ao_poly cdr);
630 extern struct ao_scheme_cons *ao_scheme_cons_free_list;
633 ao_scheme_cons_free(struct ao_scheme_cons *cons);
636 ao_scheme_cons_write(ao_poly);
639 ao_scheme_cons_display(ao_poly);
642 ao_scheme_cons_length(struct ao_scheme_cons *cons);
644 struct ao_scheme_cons *
645 ao_scheme_cons_copy(struct ao_scheme_cons *cons);
648 extern const struct ao_scheme_type ao_scheme_string_type;
650 struct ao_scheme_string *
651 ao_scheme_string_copy(struct ao_scheme_string *a);
653 struct ao_scheme_string *
654 ao_scheme_string_make(char *a);
656 struct ao_scheme_string *
657 ao_scheme_atom_to_string(struct ao_scheme_atom *a);
659 struct ao_scheme_string *
660 ao_scheme_string_cat(struct ao_scheme_string *a, struct ao_scheme_string *b);
663 ao_scheme_string_pack(struct ao_scheme_cons *cons);
666 ao_scheme_string_unpack(struct ao_scheme_string *a);
669 ao_scheme_string_write(ao_poly s);
672 ao_scheme_string_display(ao_poly s);
675 extern const struct ao_scheme_type ao_scheme_atom_type;
677 extern struct ao_scheme_atom *ao_scheme_atoms;
678 extern struct ao_scheme_frame *ao_scheme_frame_global;
679 extern struct ao_scheme_frame *ao_scheme_frame_current;
682 ao_scheme_atom_write(ao_poly a);
684 struct ao_scheme_atom *
685 ao_scheme_string_to_atom(struct ao_scheme_string *string);
687 struct ao_scheme_atom *
688 ao_scheme_atom_intern(char *name);
691 ao_scheme_atom_ref(ao_poly atom, struct ao_scheme_frame **frame_ref);
694 ao_scheme_atom_get(ao_poly atom);
697 ao_scheme_atom_set(ao_poly atom, ao_poly val);
700 ao_scheme_atom_def(ao_poly atom, ao_poly val);
704 ao_scheme_int_write(ao_poly i);
706 #ifdef AO_SCHEME_FEATURE_BIGINT
708 ao_scheme_poly_integer(ao_poly p, bool *fail);
711 ao_scheme_integer_poly(int32_t i);
714 ao_scheme_integer_typep(uint8_t t)
716 return (t == AO_SCHEME_INT) || (t == AO_SCHEME_BIGINT);
720 ao_scheme_bigint_write(ao_poly i);
722 extern const struct ao_scheme_type ao_scheme_bigint_type;
726 #define ao_scheme_poly_integer(a,b) ao_scheme_poly_int(a)
727 #define ao_scheme_integer_poly ao_scheme_int_poly
730 ao_scheme_integer_typep(uint8_t t)
732 return (t == AO_SCHEME_INT);
735 #endif /* AO_SCHEME_FEATURE_BIGINT */
740 ao_scheme_vector_write(ao_poly v);
743 ao_scheme_vector_display(ao_poly v);
745 struct ao_scheme_vector *
746 ao_scheme_vector_alloc(uint16_t length, ao_poly fill);
749 ao_scheme_vector_get(ao_poly v, ao_poly i);
752 ao_scheme_vector_set(ao_poly v, ao_poly i, ao_poly p);
754 struct ao_scheme_vector *
755 ao_scheme_list_to_vector(struct ao_scheme_cons *cons);
757 struct ao_scheme_cons *
758 ao_scheme_vector_to_list(struct ao_scheme_vector *vector);
760 extern const struct ao_scheme_type ao_scheme_vector_type;
763 void (*ao_scheme_poly_write_func(ao_poly p))(ao_poly p);
764 void (*ao_scheme_poly_display_func(ao_poly p))(ao_poly p);
767 ao_scheme_poly_write(ao_poly p) { (*ao_scheme_poly_write_func(p))(p); }
770 ao_scheme_poly_display(ao_poly p) { (*ao_scheme_poly_display_func(p))(p); }
773 ao_scheme_poly_mark(ao_poly p, uint8_t note_cons);
775 /* returns 1 if the object has already been moved */
777 ao_scheme_poly_move(ao_poly *p, uint8_t note_cons);
782 ao_scheme_eval_clear_globals(void);
785 ao_scheme_eval_restart(void);
788 ao_scheme_eval(ao_poly p);
791 ao_scheme_set_cond(struct ao_scheme_cons *cons);
794 #ifdef AO_SCHEME_FEATURE_FLOAT
795 extern const struct ao_scheme_type ao_scheme_float_type;
798 ao_scheme_float_write(ao_poly p);
801 ao_scheme_float_get(float value);
804 #ifdef AO_SCHEME_FEATURE_FLOAT
805 static inline uint8_t
806 ao_scheme_number_typep(uint8_t t)
808 return ao_scheme_integer_typep(t) || (t == AO_SCHEME_FLOAT);
811 #define ao_scheme_number_typep ao_scheme_integer_typep
816 ao_scheme_builtin_write(ao_poly b);
818 extern const struct ao_scheme_type ao_scheme_builtin_type;
820 /* Check argument count */
822 ao_scheme_check_argc(ao_poly name, struct ao_scheme_cons *cons, int min, int max);
824 /* Check argument type */
826 ao_scheme_check_argt(ao_poly name, struct ao_scheme_cons *cons, int argc, int type, int nil_ok);
828 /* Fetch an arg (nil if off the end) */
830 ao_scheme_arg(struct ao_scheme_cons *cons, int argc);
833 ao_scheme_args_name(uint8_t args);
836 extern int ao_scheme_read_list;
837 extern struct ao_scheme_cons *ao_scheme_read_cons;
838 extern struct ao_scheme_cons *ao_scheme_read_cons_tail;
839 extern struct ao_scheme_cons *ao_scheme_read_stack;
842 ao_scheme_read(void);
846 ao_scheme_read_eval_print(void);
849 extern const struct ao_scheme_type ao_scheme_frame_type;
850 extern const struct ao_scheme_type ao_scheme_frame_vals_type;
852 #define AO_SCHEME_FRAME_FREE 6
854 extern struct ao_scheme_frame *ao_scheme_frame_free_list[AO_SCHEME_FRAME_FREE];
857 ao_scheme_frame_mark(struct ao_scheme_frame *frame);
860 ao_scheme_frame_ref(struct ao_scheme_frame *frame, ao_poly atom);
862 struct ao_scheme_frame *
863 ao_scheme_frame_new(int num);
866 ao_scheme_frame_free(struct ao_scheme_frame *frame);
869 ao_scheme_frame_bind(struct ao_scheme_frame *frame, int num, ao_poly atom, ao_poly val);
872 ao_scheme_frame_add(struct ao_scheme_frame *frame, ao_poly atom, ao_poly val);
875 ao_scheme_frame_write(ao_poly p);
878 ao_scheme_frame_init(void);
881 extern const struct ao_scheme_type ao_scheme_lambda_type;
883 extern const char * const ao_scheme_state_names[];
885 struct ao_scheme_lambda *
886 ao_scheme_lambda_new(ao_poly cons);
889 ao_scheme_lambda_write(ao_poly lambda);
892 ao_scheme_lambda_eval(void);
896 extern const struct ao_scheme_type ao_scheme_stack_type;
897 extern struct ao_scheme_stack *ao_scheme_stack;
898 extern struct ao_scheme_stack *ao_scheme_stack_free_list;
901 ao_scheme_stack_reset(struct ao_scheme_stack *stack);
904 ao_scheme_stack_push(void);
907 ao_scheme_stack_pop(void);
910 ao_scheme_stack_clear(void);
913 ao_scheme_stack_write(ao_poly stack);
916 ao_scheme_stack_eval(void);
921 ao_scheme_vprintf(const char *format, va_list args);
924 ao_scheme_printf(const char *format, ...);
927 ao_scheme_error_poly(const char *name, ao_poly poly, ao_poly last);
930 ao_scheme_error_frame(int indent, const char *name, struct ao_scheme_frame *frame);
933 ao_scheme_error(int error, const char *format, ...);
937 #define AO_SCHEME_BUILTIN_DECLS
938 #include "ao_scheme_builtin.h"
940 /* debugging macros */
942 #if DBG_EVAL || DBG_READ || DBG_MEM
944 int ao_scheme_stack_depth;
946 #define DBG_INDENT() do { int _s; for(_s = 0; _s < ao_scheme_stack_depth; _s++) printf(" "); } while(0)
947 #define DBG_IN() (++ao_scheme_stack_depth)
948 #define DBG_OUT() (--ao_scheme_stack_depth)
949 #define DBG_RESET() (ao_scheme_stack_depth = 0)
950 #define DBG(...) ao_scheme_printf(__VA_ARGS__)
951 #define DBGI(...) do { printf("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
952 #define DBG_CONS(a) ao_scheme_cons_write(ao_scheme_cons_poly(a))
953 #define DBG_POLY(a) ao_scheme_poly_write(a)
954 #define OFFSET(a) ((a) ? (int) ((uint8_t *) a - ao_scheme_pool) : -1)
955 #define DBG_STACK() ao_scheme_stack_write(ao_scheme_stack_poly(ao_scheme_stack))
957 ao_scheme_frames_dump(void)
959 struct ao_scheme_stack *s;
960 DBGI(".. current frame: "); DBG_POLY(ao_scheme_frame_poly(ao_scheme_frame_current)); DBG("\n");
961 for (s = ao_scheme_stack; s; s = ao_scheme_poly_stack(s->prev)) {
962 DBGI(".. stack frame: "); DBG_POLY(s->frame); DBG("\n");
965 #define DBG_FRAMES() ao_scheme_frames_dump()
981 #define RDBGI(...) DBGI(__VA_ARGS__)
982 #define RDBG_IN() DBG_IN()
983 #define RDBG_OUT() DBG_OUT()
990 #define DBG_MEM_START 1
995 extern int dbg_move_depth;
997 #define MDBG_OFFSET(a) ((a) ? (int) ((uint8_t *) (a) - ao_scheme_pool) : -1)
1001 #define MDBG_DO(a) DBG_DO(a)
1002 #define MDBG_MOVE(...) do { if (dbg_mem) { int d; for (d = 0; d < dbg_move_depth; d++) printf (" "); printf(__VA_ARGS__); } } while (0)
1003 #define MDBG_MORE(...) do { if (dbg_mem) printf(__VA_ARGS__); } while (0)
1004 #define MDBG_MOVE_IN() (dbg_move_depth++)
1005 #define MDBG_MOVE_OUT() (assert(--dbg_move_depth >= 0))
1010 #define MDBG_MOVE(...)
1011 #define MDBG_MORE(...)
1012 #define MDBG_MOVE_IN()
1013 #define MDBG_MOVE_OUT()
1017 #endif /* _AO_SCHEME_H_ */