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 /* Marks an object as being printed, returns 1 if it was already marked */
560 ao_scheme_print_mark_addr(void *addr);
563 ao_scheme_print_mark_poly(ao_poly poly);
565 /* Notes that printing has started */
567 ao_scheme_print_start(void);
569 /* Notes that printing has ended */
571 ao_scheme_print_stop(void);
573 #define AO_SCHEME_COLLECT_FULL 1
574 #define AO_SCHEME_COLLECT_INCREMENTAL 0
577 ao_scheme_collect(uint8_t style);
581 ao_scheme_cons_check(struct ao_scheme_cons *cons);
585 ao_scheme_cons_stash(int id, struct ao_scheme_cons *cons);
587 struct ao_scheme_cons *
588 ao_scheme_cons_fetch(int id);
591 ao_scheme_poly_stash(int id, ao_poly poly);
594 ao_scheme_poly_fetch(int id);
597 ao_scheme_string_stash(int id, struct ao_scheme_string *string);
599 struct ao_scheme_string *
600 ao_scheme_string_fetch(int id);
603 ao_scheme_stack_stash(int id, struct ao_scheme_stack *stack) {
604 ao_scheme_poly_stash(id, ao_scheme_stack_poly(stack));
607 static inline struct ao_scheme_stack *
608 ao_scheme_stack_fetch(int id) {
609 return ao_scheme_poly_stack(ao_scheme_poly_fetch(id));
613 ao_scheme_frame_stash(int id, struct ao_scheme_frame *frame);
615 struct ao_scheme_frame *
616 ao_scheme_frame_fetch(int id);
620 extern const struct ao_scheme_type ao_scheme_bool_type;
623 ao_scheme_bool_write(ao_poly v);
625 #ifdef AO_SCHEME_MAKE_CONST
626 extern struct ao_scheme_bool *ao_scheme_true, *ao_scheme_false;
628 struct ao_scheme_bool *
629 ao_scheme_bool_get(uint8_t value);
633 extern const struct ao_scheme_type ao_scheme_cons_type;
635 struct ao_scheme_cons *
636 ao_scheme_cons_cons(ao_poly car, ao_poly cdr);
638 /* Return a cons or NULL for a proper list, else error */
639 struct ao_scheme_cons *
640 ao_scheme_cons_cdr(struct ao_scheme_cons *cons);
643 ao_scheme__cons(ao_poly car, ao_poly cdr);
645 extern struct ao_scheme_cons *ao_scheme_cons_free_list;
648 ao_scheme_cons_free(struct ao_scheme_cons *cons);
651 ao_scheme_cons_write(ao_poly);
654 ao_scheme_cons_display(ao_poly);
657 ao_scheme_cons_length(struct ao_scheme_cons *cons);
659 struct ao_scheme_cons *
660 ao_scheme_cons_copy(struct ao_scheme_cons *cons);
663 extern const struct ao_scheme_type ao_scheme_string_type;
665 struct ao_scheme_string *
666 ao_scheme_string_copy(struct ao_scheme_string *a);
668 struct ao_scheme_string *
669 ao_scheme_string_make(char *a);
671 struct ao_scheme_string *
672 ao_scheme_atom_to_string(struct ao_scheme_atom *a);
674 struct ao_scheme_string *
675 ao_scheme_string_cat(struct ao_scheme_string *a, struct ao_scheme_string *b);
678 ao_scheme_string_pack(struct ao_scheme_cons *cons);
681 ao_scheme_string_unpack(struct ao_scheme_string *a);
684 ao_scheme_string_write(ao_poly s);
687 ao_scheme_string_display(ao_poly s);
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);
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);
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);
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);
758 ao_scheme_vector_display(ao_poly v);
760 struct ao_scheme_vector *
761 ao_scheme_vector_alloc(uint16_t length, ao_poly fill);
764 ao_scheme_vector_get(ao_poly v, ao_poly i);
767 ao_scheme_vector_set(ao_poly v, ao_poly i, ao_poly p);
769 struct ao_scheme_vector *
770 ao_scheme_list_to_vector(struct ao_scheme_cons *cons);
772 struct ao_scheme_cons *
773 ao_scheme_vector_to_list(struct ao_scheme_vector *vector);
775 extern const struct ao_scheme_type ao_scheme_vector_type;
778 void (*ao_scheme_poly_write_func(ao_poly p))(ao_poly p);
779 void (*ao_scheme_poly_display_func(ao_poly p))(ao_poly p);
782 ao_scheme_poly_write(ao_poly p) { (*ao_scheme_poly_write_func(p))(p); }
785 ao_scheme_poly_display(ao_poly p) { (*ao_scheme_poly_display_func(p))(p); }
788 ao_scheme_poly_mark(ao_poly p, uint8_t note_cons);
790 /* returns 1 if the object has already been moved */
792 ao_scheme_poly_move(ao_poly *p, uint8_t note_cons);
797 ao_scheme_eval_clear_globals(void);
800 ao_scheme_eval_restart(void);
803 ao_scheme_eval(ao_poly p);
806 ao_scheme_set_cond(struct ao_scheme_cons *cons);
809 #ifdef AO_SCHEME_FEATURE_FLOAT
810 extern const struct ao_scheme_type ao_scheme_float_type;
813 ao_scheme_float_write(ao_poly p);
816 ao_scheme_float_get(float value);
819 #ifdef AO_SCHEME_FEATURE_FLOAT
820 static inline uint8_t
821 ao_scheme_number_typep(uint8_t t)
823 return ao_scheme_integer_typep(t) || (t == AO_SCHEME_FLOAT);
826 #define ao_scheme_number_typep ao_scheme_integer_typep
831 ao_scheme_builtin_write(ao_poly b);
833 extern const struct ao_scheme_type ao_scheme_builtin_type;
835 /* Check argument count */
837 ao_scheme_check_argc(ao_poly name, struct ao_scheme_cons *cons, int min, int max);
839 /* Check argument type */
841 ao_scheme_check_argt(ao_poly name, struct ao_scheme_cons *cons, int argc, int type, int nil_ok);
843 /* Fetch an arg (nil if off the end) */
845 ao_scheme_arg(struct ao_scheme_cons *cons, int argc);
848 ao_scheme_args_name(uint8_t args);
851 extern int ao_scheme_read_list;
852 extern struct ao_scheme_cons *ao_scheme_read_cons;
853 extern struct ao_scheme_cons *ao_scheme_read_cons_tail;
854 extern struct ao_scheme_cons *ao_scheme_read_stack;
857 ao_scheme_read(void);
861 ao_scheme_read_eval_print(void);
864 extern const struct ao_scheme_type ao_scheme_frame_type;
865 extern const struct ao_scheme_type ao_scheme_frame_vals_type;
867 #define AO_SCHEME_FRAME_FREE 6
869 extern struct ao_scheme_frame *ao_scheme_frame_free_list[AO_SCHEME_FRAME_FREE];
872 ao_scheme_frame_mark(struct ao_scheme_frame *frame);
875 ao_scheme_frame_ref(struct ao_scheme_frame *frame, ao_poly atom);
877 struct ao_scheme_frame *
878 ao_scheme_frame_new(int num);
881 ao_scheme_frame_free(struct ao_scheme_frame *frame);
884 ao_scheme_frame_bind(struct ao_scheme_frame *frame, int num, ao_poly atom, ao_poly val);
887 ao_scheme_frame_add(struct ao_scheme_frame *frame, ao_poly atom, ao_poly val);
890 ao_scheme_frame_write(ao_poly p);
893 ao_scheme_frame_init(void);
896 extern const struct ao_scheme_type ao_scheme_lambda_type;
898 extern const char * const ao_scheme_state_names[];
900 struct ao_scheme_lambda *
901 ao_scheme_lambda_new(ao_poly cons);
904 ao_scheme_lambda_write(ao_poly lambda);
907 ao_scheme_lambda_eval(void);
911 extern const struct ao_scheme_type ao_scheme_stack_type;
912 extern struct ao_scheme_stack *ao_scheme_stack;
913 extern struct ao_scheme_stack *ao_scheme_stack_free_list;
916 ao_scheme_stack_reset(struct ao_scheme_stack *stack);
919 ao_scheme_stack_push(void);
922 ao_scheme_stack_pop(void);
925 ao_scheme_stack_clear(void);
928 ao_scheme_stack_write(ao_poly stack);
931 ao_scheme_stack_eval(void);
936 ao_scheme_vprintf(const char *format, va_list args);
939 ao_scheme_printf(const char *format, ...);
942 ao_scheme_error_poly(const char *name, ao_poly poly, ao_poly last);
945 ao_scheme_error_frame(int indent, const char *name, struct ao_scheme_frame *frame);
948 ao_scheme_error(int error, const char *format, ...);
952 #define AO_SCHEME_BUILTIN_DECLS
953 #include "ao_scheme_builtin.h"
955 /* debugging macros */
957 #if DBG_EVAL || DBG_READ || DBG_MEM
959 int ao_scheme_stack_depth;
961 #define DBG_INDENT() do { int _s; for(_s = 0; _s < ao_scheme_stack_depth; _s++) printf(" "); } while(0)
962 #define DBG_IN() (++ao_scheme_stack_depth)
963 #define DBG_OUT() (--ao_scheme_stack_depth)
964 #define DBG_RESET() (ao_scheme_stack_depth = 0)
965 #define DBG(...) ao_scheme_printf(__VA_ARGS__)
966 #define DBGI(...) do { printf("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
967 #define DBG_CONS(a) ao_scheme_cons_write(ao_scheme_cons_poly(a))
968 #define DBG_POLY(a) ao_scheme_poly_write(a)
969 #define OFFSET(a) ((a) ? (int) ((uint8_t *) a - ao_scheme_pool) : -1)
970 #define DBG_STACK() ao_scheme_stack_write(ao_scheme_stack_poly(ao_scheme_stack))
972 ao_scheme_frames_dump(void)
974 struct ao_scheme_stack *s;
975 DBGI(".. current frame: "); DBG_POLY(ao_scheme_frame_poly(ao_scheme_frame_current)); DBG("\n");
976 for (s = ao_scheme_stack; s; s = ao_scheme_poly_stack(s->prev)) {
977 DBGI(".. stack frame: "); DBG_POLY(s->frame); DBG("\n");
980 #define DBG_FRAMES() ao_scheme_frames_dump()
996 #define RDBGI(...) DBGI(__VA_ARGS__)
997 #define RDBG_IN() DBG_IN()
998 #define RDBG_OUT() DBG_OUT()
1005 #define DBG_MEM_START 1
1010 extern int dbg_move_depth;
1012 #define MDBG_OFFSET(a) ((a) ? (int) ((uint8_t *) (a) - ao_scheme_pool) : -1)
1016 #define MDBG_DO(a) DBG_DO(a)
1017 #define MDBG_MOVE(...) do { if (dbg_mem) { int d; for (d = 0; d < dbg_move_depth; d++) printf (" "); printf(__VA_ARGS__); } } while (0)
1018 #define MDBG_MORE(...) do { if (dbg_mem) printf(__VA_ARGS__); } while (0)
1019 #define MDBG_MOVE_IN() (dbg_move_depth++)
1020 #define MDBG_MOVE_OUT() (assert(--dbg_move_depth >= 0))
1025 #define MDBG_MOVE(...)
1026 #define MDBG_MORE(...)
1027 #define MDBG_MOVE_IN()
1028 #define MDBG_MOVE_OUT()
1032 #endif /* _AO_SCHEME_H_ */