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;
159 ao_scheme_is_const_addr(const void *addr) {
160 const uint8_t *a = addr;
161 return (ao_scheme_const <= a) && (a < ao_scheme_const + AO_SCHEME_POOL_CONST);
165 ao_scheme_is_pool_addr(const void *addr) {
166 const uint8_t *a = addr;
167 return (ao_scheme_pool <= a) && (a < ao_scheme_pool + AO_SCHEME_POOL);
171 ao_scheme_ref(ao_poly poly);
174 ao_scheme_poly(const void *addr, ao_poly type);
176 struct ao_scheme_type {
177 int (*size)(void *addr);
178 void (*mark)(void *addr);
179 void (*move)(void *addr);
183 struct ao_scheme_cons {
188 struct ao_scheme_atom {
195 struct ao_scheme_string {
200 struct ao_scheme_val {
205 struct ao_scheme_frame_vals {
208 struct ao_scheme_val vals[];
211 struct ao_scheme_frame {
218 struct ao_scheme_bool {
225 #ifdef AO_SCHEME_FEATURE_FLOAT
226 struct ao_scheme_float {
234 #ifdef AO_SCHEME_FEATURE_VECTOR
235 struct ao_scheme_vector {
243 #define AO_SCHEME_MIN_INT (-(1 << (15 - AO_SCHEME_TYPE_SHIFT)))
244 #define AO_SCHEME_MAX_INT ((1 << (15 - AO_SCHEME_TYPE_SHIFT)) - 1)
246 #ifdef AO_SCHEME_FEATURE_BIGINT
248 struct ao_scheme_bigint {
252 #define AO_SCHEME_MIN_BIGINT INT32_MIN
253 #define AO_SCHEME_MAX_BIGINT INT32_MAX
255 #endif /* AO_SCHEME_FEATURE_BIGINT */
257 /* Set on type when the frame escapes the lambda */
258 #define AO_SCHEME_FRAME_MARK 0x80
260 static inline int ao_scheme_frame_marked(struct ao_scheme_frame *f) {
261 return f->type & AO_SCHEME_FRAME_MARK;
264 static inline struct ao_scheme_frame *
265 ao_scheme_poly_frame(ao_poly poly) {
266 return ao_scheme_ref(poly);
269 static inline ao_poly
270 ao_scheme_frame_poly(struct ao_scheme_frame *frame) {
271 return ao_scheme_poly(frame, AO_SCHEME_OTHER);
274 static inline struct ao_scheme_frame_vals *
275 ao_scheme_poly_frame_vals(ao_poly poly) {
276 return ao_scheme_ref(poly);
279 static inline ao_poly
280 ao_scheme_frame_vals_poly(struct ao_scheme_frame_vals *vals) {
281 return ao_scheme_poly(vals, AO_SCHEME_OTHER);
285 eval_sexpr, /* Evaluate an sexpr */
286 eval_val, /* Value computed */
287 eval_formal, /* Formal computed */
288 eval_exec, /* Start a lambda evaluation */
289 eval_apply, /* Execute apply */
290 eval_cond, /* Start next cond clause */
291 eval_cond_test, /* Check cond condition */
292 eval_begin, /* Start next begin entry */
293 eval_while, /* Start while condition */
294 eval_while_test, /* Check while condition */
295 eval_macro, /* Finished with macro generation */
298 struct ao_scheme_stack {
299 uint8_t type; /* AO_SCHEME_STACK */
300 uint8_t state; /* enum eval_state */
301 ao_poly prev; /* previous stack frame */
302 ao_poly sexprs; /* expressions to evaluate */
303 ao_poly values; /* values computed */
304 ao_poly values_tail; /* end of the values list for easy appending */
305 ao_poly frame; /* current lookup frame */
306 ao_poly list; /* most recent function call */
309 #define AO_SCHEME_STACK_MARK 0x80 /* set on type when a reference has been taken */
311 static inline int ao_scheme_stack_marked(struct ao_scheme_stack *s) {
312 return s->type & AO_SCHEME_STACK_MARK;
315 static inline void ao_scheme_stack_mark(struct ao_scheme_stack *s) {
316 s->type |= AO_SCHEME_STACK_MARK;
319 static inline struct ao_scheme_stack *
320 ao_scheme_poly_stack(ao_poly p)
322 return ao_scheme_ref(p);
325 static inline ao_poly
326 ao_scheme_stack_poly(struct ao_scheme_stack *stack)
328 return ao_scheme_poly(stack, AO_SCHEME_OTHER);
331 extern ao_poly ao_scheme_v;
333 #define AO_SCHEME_FUNC_LAMBDA 0
334 #define AO_SCHEME_FUNC_NLAMBDA 1
335 #define AO_SCHEME_FUNC_MACRO 2
337 #define AO_SCHEME_FUNC_FREE_ARGS 0x80
338 #define AO_SCHEME_FUNC_MASK 0x7f
340 #define AO_SCHEME_FUNC_F_LAMBDA (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_LAMBDA)
341 #define AO_SCHEME_FUNC_F_NLAMBDA (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_NLAMBDA)
342 #define AO_SCHEME_FUNC_F_MACRO (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_MACRO)
344 struct ao_scheme_builtin {
350 #define AO_SCHEME_BUILTIN_ID
351 #include "ao_scheme_builtin.h"
353 typedef ao_poly (*ao_scheme_func_t)(struct ao_scheme_cons *cons);
355 extern const ao_scheme_func_t ao_scheme_builtins[];
357 static inline ao_scheme_func_t
358 ao_scheme_func(struct ao_scheme_builtin *b)
360 return ao_scheme_builtins[b->func];
363 struct ao_scheme_lambda {
370 static inline struct ao_scheme_lambda *
371 ao_scheme_poly_lambda(ao_poly poly)
373 return ao_scheme_ref(poly);
376 static inline ao_poly
377 ao_scheme_lambda_poly(struct ao_scheme_lambda *lambda)
379 return ao_scheme_poly(lambda, AO_SCHEME_OTHER);
383 ao_scheme_poly_other(ao_poly poly) {
384 return ao_scheme_ref(poly);
387 static inline uint8_t
388 ao_scheme_other_type(void *other) {
390 if ((*((uint8_t *) other) & AO_SCHEME_OTHER_TYPE_MASK) >= AO_SCHEME_NUM_TYPE)
393 return *((uint8_t *) other) & AO_SCHEME_OTHER_TYPE_MASK;
396 static inline ao_poly
397 ao_scheme_other_poly(const void *other)
399 return ao_scheme_poly(other, AO_SCHEME_OTHER);
403 ao_scheme_size_round(int size)
405 return (size + 3) & ~3;
409 ao_scheme_size(const struct ao_scheme_type *type, void *addr)
411 return ao_scheme_size_round(type->size(addr));
414 #define AO_SCHEME_OTHER_POLY(other) ((ao_poly)(other) + AO_SCHEME_OTHER)
416 static inline int ao_scheme_poly_base_type(ao_poly poly) {
417 return poly & AO_SCHEME_TYPE_MASK;
420 static inline int ao_scheme_poly_type(ao_poly poly) {
421 int type = poly & AO_SCHEME_TYPE_MASK;
422 if (type == AO_SCHEME_OTHER)
423 return ao_scheme_other_type(ao_scheme_poly_other(poly));
428 ao_scheme_is_cons(ao_poly poly) {
429 return (ao_scheme_poly_base_type(poly) == AO_SCHEME_CONS);
433 ao_scheme_is_pair(ao_poly poly) {
434 return poly != AO_SCHEME_NIL && (ao_scheme_poly_base_type(poly) == AO_SCHEME_CONS);
437 static inline struct ao_scheme_cons *
438 ao_scheme_poly_cons(ao_poly poly)
440 return ao_scheme_ref(poly);
443 static inline ao_poly
444 ao_scheme_cons_poly(struct ao_scheme_cons *cons)
446 return ao_scheme_poly(cons, AO_SCHEME_CONS);
449 static inline int32_t
450 ao_scheme_poly_int(ao_poly poly)
452 return (int32_t) ((ao_signed_poly) poly >> AO_SCHEME_TYPE_SHIFT);
455 static inline ao_poly
456 ao_scheme_int_poly(int32_t i)
458 return ((ao_poly) i << 2) | AO_SCHEME_INT;
461 #ifdef AO_SCHEME_FEATURE_BIGINT
462 static inline struct ao_scheme_bigint *
463 ao_scheme_poly_bigint(ao_poly poly)
465 return ao_scheme_ref(poly);
468 static inline ao_poly
469 ao_scheme_bigint_poly(struct ao_scheme_bigint *bi)
471 return ao_scheme_poly(bi, AO_SCHEME_BIGINT);
473 #endif /* AO_SCHEME_FEATURE_BIGINT */
475 static inline struct ao_scheme_string *
476 ao_scheme_poly_string(ao_poly poly)
478 return ao_scheme_ref(poly);
481 static inline ao_poly
482 ao_scheme_string_poly(struct ao_scheme_string *s)
484 return ao_scheme_poly(s, AO_SCHEME_OTHER);
487 static inline struct ao_scheme_atom *
488 ao_scheme_poly_atom(ao_poly poly)
490 return ao_scheme_ref(poly);
493 static inline ao_poly
494 ao_scheme_atom_poly(struct ao_scheme_atom *a)
496 return ao_scheme_poly(a, AO_SCHEME_OTHER);
499 static inline struct ao_scheme_builtin *
500 ao_scheme_poly_builtin(ao_poly poly)
502 return ao_scheme_ref(poly);
505 static inline ao_poly
506 ao_scheme_builtin_poly(struct ao_scheme_builtin *b)
508 return ao_scheme_poly(b, AO_SCHEME_OTHER);
511 static inline ao_poly
512 ao_scheme_bool_poly(struct ao_scheme_bool *b)
514 return ao_scheme_poly(b, AO_SCHEME_OTHER);
517 static inline struct ao_scheme_bool *
518 ao_scheme_poly_bool(ao_poly poly)
520 return ao_scheme_ref(poly);
523 #ifdef AO_SCHEME_FEATURE_FLOAT
524 static inline ao_poly
525 ao_scheme_float_poly(struct ao_scheme_float *f)
527 return ao_scheme_poly(f, AO_SCHEME_OTHER);
530 static inline struct ao_scheme_float *
531 ao_scheme_poly_float(ao_poly poly)
533 return ao_scheme_ref(poly);
537 ao_scheme_poly_number(ao_poly p);
540 #ifdef AO_SCHEME_FEATURE_VECTOR
541 static inline ao_poly
542 ao_scheme_vector_poly(struct ao_scheme_vector *v)
544 return ao_scheme_poly(v, AO_SCHEME_OTHER);
547 static inline struct ao_scheme_vector *
548 ao_scheme_poly_vector(ao_poly poly)
550 return ao_scheme_ref(poly);
554 /* memory functions */
556 extern uint64_t ao_scheme_collects[2];
557 extern uint64_t ao_scheme_freed[2];
558 extern uint64_t ao_scheme_loops[2];
560 /* returns 1 if the object was already marked */
562 ao_scheme_mark_memory(const struct ao_scheme_type *type, void *addr);
564 /* returns 1 if the object was already moved */
566 ao_scheme_move_memory(const struct ao_scheme_type *type, void **ref);
569 ao_scheme_alloc(int size);
571 /* Marks an object as being printed, returns 1 if it was already marked */
573 ao_scheme_print_mark_addr(void *addr);
576 ao_scheme_print_clear_addr(void *addr);
578 /* Notes that printing has started */
580 ao_scheme_print_start(void);
582 /* Notes that printing has ended, returns 1 if printing is still happening */
584 ao_scheme_print_stop(void);
586 #define AO_SCHEME_COLLECT_FULL 1
587 #define AO_SCHEME_COLLECT_INCREMENTAL 0
590 ao_scheme_collect(uint8_t style);
594 ao_scheme_cons_check(struct ao_scheme_cons *cons);
598 ao_scheme_poly_stash(ao_poly poly);
601 ao_scheme_poly_fetch(void);
604 ao_scheme_cons_stash(struct ao_scheme_cons *cons) {
605 ao_scheme_poly_stash(ao_scheme_cons_poly(cons));
608 static inline struct ao_scheme_cons *
609 ao_scheme_cons_fetch(void) {
610 return ao_scheme_poly_cons(ao_scheme_poly_fetch());
614 ao_scheme_atom_stash(struct ao_scheme_atom *atom) {
615 ao_scheme_poly_stash(ao_scheme_atom_poly(atom));
618 static inline struct ao_scheme_atom *
619 ao_scheme_atom_fetch(void) {
620 return ao_scheme_poly_atom(ao_scheme_poly_fetch());
624 ao_scheme_string_stash(struct ao_scheme_string *string) {
625 ao_scheme_poly_stash(ao_scheme_string_poly(string));
628 static inline struct ao_scheme_string *
629 ao_scheme_string_fetch(void) {
630 return ao_scheme_poly_string(ao_scheme_poly_fetch());
633 #ifdef AO_SCHEME_FEATURE_VECTOR
635 ao_scheme_vector_stash(struct ao_scheme_vector *vector) {
636 ao_scheme_poly_stash(ao_scheme_vector_poly(vector));
639 static inline struct ao_scheme_vector *
640 ao_scheme_vector_fetch(void) {
641 return ao_scheme_poly_vector(ao_scheme_poly_fetch());
646 ao_scheme_stack_stash(struct ao_scheme_stack *stack) {
647 ao_scheme_poly_stash(ao_scheme_stack_poly(stack));
650 static inline struct ao_scheme_stack *
651 ao_scheme_stack_fetch(void) {
652 return ao_scheme_poly_stack(ao_scheme_poly_fetch());
656 ao_scheme_frame_stash(struct ao_scheme_frame *frame) {
657 ao_scheme_poly_stash(ao_scheme_frame_poly(frame));
660 static inline struct ao_scheme_frame *
661 ao_scheme_frame_fetch(void) {
662 return ao_scheme_poly_frame(ao_scheme_poly_fetch());
667 extern const struct ao_scheme_type ao_scheme_bool_type;
670 ao_scheme_bool_write(ao_poly v, bool write);
672 #ifdef AO_SCHEME_MAKE_CONST
673 extern struct ao_scheme_bool *ao_scheme_true, *ao_scheme_false;
675 struct ao_scheme_bool *
676 ao_scheme_bool_get(uint8_t value);
680 extern const struct ao_scheme_type ao_scheme_cons_type;
682 struct ao_scheme_cons *
683 ao_scheme_cons_cons(ao_poly car, ao_poly cdr);
685 /* Return a cons or NULL for a proper list, else error */
686 struct ao_scheme_cons *
687 ao_scheme_cons_cdr(struct ao_scheme_cons *cons);
690 ao_scheme_cons(ao_poly car, ao_poly cdr);
692 extern struct ao_scheme_cons *ao_scheme_cons_free_list;
695 ao_scheme_cons_free(struct ao_scheme_cons *cons);
698 ao_scheme_cons_write(ao_poly, bool write);
701 ao_scheme_cons_length(struct ao_scheme_cons *cons);
703 struct ao_scheme_cons *
704 ao_scheme_cons_copy(struct ao_scheme_cons *cons);
707 extern const struct ao_scheme_type ao_scheme_string_type;
709 struct ao_scheme_string *
710 ao_scheme_string_copy(struct ao_scheme_string *a);
712 struct ao_scheme_string *
713 ao_scheme_string_make(char *a);
715 struct ao_scheme_string *
716 ao_scheme_atom_to_string(struct ao_scheme_atom *a);
718 struct ao_scheme_string *
719 ao_scheme_string_cat(struct ao_scheme_string *a, struct ao_scheme_string *b);
722 ao_scheme_string_pack(struct ao_scheme_cons *cons);
725 ao_scheme_string_unpack(struct ao_scheme_string *a);
728 ao_scheme_string_write(ao_poly s, bool write);
731 extern const struct ao_scheme_type ao_scheme_atom_type;
733 extern struct ao_scheme_atom *ao_scheme_atoms;
734 extern struct ao_scheme_frame *ao_scheme_frame_global;
735 extern struct ao_scheme_frame *ao_scheme_frame_current;
738 ao_scheme_atom_write(ao_poly a, bool write);
740 struct ao_scheme_atom *
741 ao_scheme_string_to_atom(struct ao_scheme_string *string);
743 struct ao_scheme_atom *
744 ao_scheme_atom_intern(char *name);
747 ao_scheme_atom_ref(ao_poly atom, struct ao_scheme_frame **frame_ref);
750 ao_scheme_atom_get(ao_poly atom);
753 ao_scheme_atom_set(ao_poly atom, ao_poly val);
756 ao_scheme_atom_def(ao_poly atom, ao_poly val);
760 ao_scheme_int_write(ao_poly i, bool write);
762 #ifdef AO_SCHEME_FEATURE_BIGINT
764 ao_scheme_poly_integer(ao_poly p, bool *fail);
767 ao_scheme_integer_poly(int32_t i);
770 ao_scheme_integer_typep(uint8_t t)
772 return (t == AO_SCHEME_INT) || (t == AO_SCHEME_BIGINT);
776 ao_scheme_bigint_write(ao_poly i, bool write);
778 extern const struct ao_scheme_type ao_scheme_bigint_type;
782 #define ao_scheme_poly_integer(a,b) ao_scheme_poly_int(a)
783 #define ao_scheme_integer_poly ao_scheme_int_poly
786 ao_scheme_integer_typep(uint8_t t)
788 return (t == AO_SCHEME_INT);
791 #endif /* AO_SCHEME_FEATURE_BIGINT */
796 ao_scheme_vector_write(ao_poly v, bool write);
798 struct ao_scheme_vector *
799 ao_scheme_vector_alloc(uint16_t length, ao_poly fill);
802 ao_scheme_vector_get(ao_poly v, ao_poly i);
805 ao_scheme_vector_set(ao_poly v, ao_poly i, ao_poly p);
807 struct ao_scheme_vector *
808 ao_scheme_list_to_vector(struct ao_scheme_cons *cons);
810 struct ao_scheme_cons *
811 ao_scheme_vector_to_list(struct ao_scheme_vector *vector, int start, int end);
813 extern const struct ao_scheme_type ao_scheme_vector_type;
816 void (*ao_scheme_poly_write_func(ao_poly p))(ao_poly p, bool write);
819 ao_scheme_poly_write(ao_poly p, bool write) { (*ao_scheme_poly_write_func(p))(p, write); }
822 ao_scheme_poly_mark(ao_poly p, uint8_t note_cons);
824 /* returns 1 if the object has already been moved */
826 ao_scheme_poly_move(ao_poly *p, uint8_t note_cons);
831 ao_scheme_eval_clear_globals(void);
834 ao_scheme_eval_restart(void);
837 ao_scheme_eval(ao_poly p);
840 ao_scheme_set_cond(struct ao_scheme_cons *cons);
843 #ifdef AO_SCHEME_FEATURE_FLOAT
844 extern const struct ao_scheme_type ao_scheme_float_type;
847 ao_scheme_float_write(ao_poly p, bool write);
850 ao_scheme_float_get(float value);
853 #ifdef AO_SCHEME_FEATURE_FLOAT
854 static inline uint8_t
855 ao_scheme_number_typep(uint8_t t)
857 return ao_scheme_integer_typep(t) || (t == AO_SCHEME_FLOAT);
860 #define ao_scheme_number_typep ao_scheme_integer_typep
865 ao_scheme_builtin_write(ao_poly b, bool write);
867 extern const struct ao_scheme_type ao_scheme_builtin_type;
869 /* Check argument count */
871 ao_scheme_check_argc(ao_poly name, struct ao_scheme_cons *cons, int min, int max);
873 /* Check argument type */
875 ao_scheme_check_argt(ao_poly name, struct ao_scheme_cons *cons, int argc, int type, int nil_ok);
877 /* Fetch an arg (nil if off the end) */
879 ao_scheme_arg(struct ao_scheme_cons *cons, int argc);
882 ao_scheme_args_name(uint8_t args);
885 extern int ao_scheme_read_list;
886 extern struct ao_scheme_cons *ao_scheme_read_cons;
887 extern struct ao_scheme_cons *ao_scheme_read_cons_tail;
888 extern struct ao_scheme_cons *ao_scheme_read_stack;
891 ao_scheme_read(void);
895 ao_scheme_read_eval_print(void);
898 extern const struct ao_scheme_type ao_scheme_frame_type;
899 extern const struct ao_scheme_type ao_scheme_frame_vals_type;
901 #define AO_SCHEME_FRAME_FREE 6
903 extern struct ao_scheme_frame *ao_scheme_frame_free_list[AO_SCHEME_FRAME_FREE];
906 ao_scheme_frame_mark(struct ao_scheme_frame *frame);
909 ao_scheme_frame_ref(struct ao_scheme_frame *frame, ao_poly atom);
911 struct ao_scheme_frame *
912 ao_scheme_frame_new(int num);
915 ao_scheme_frame_free(struct ao_scheme_frame *frame);
918 ao_scheme_frame_bind(struct ao_scheme_frame *frame, int num, ao_poly atom, ao_poly val);
921 ao_scheme_frame_add(struct ao_scheme_frame *frame, ao_poly atom, ao_poly val);
924 ao_scheme_frame_write(ao_poly p, bool write);
927 ao_scheme_frame_init(void);
930 extern const struct ao_scheme_type ao_scheme_lambda_type;
932 extern const char * const ao_scheme_state_names[];
934 struct ao_scheme_lambda *
935 ao_scheme_lambda_new(ao_poly cons);
938 ao_scheme_lambda_write(ao_poly lambda, bool write);
941 ao_scheme_lambda_eval(void);
945 extern const struct ao_scheme_type ao_scheme_stack_type;
946 extern struct ao_scheme_stack *ao_scheme_stack;
947 extern struct ao_scheme_stack *ao_scheme_stack_free_list;
949 extern int ao_scheme_frame_print_indent;
952 ao_scheme_stack_reset(struct ao_scheme_stack *stack);
955 ao_scheme_stack_push(void);
958 ao_scheme_stack_pop(void);
961 ao_scheme_stack_clear(void);
964 ao_scheme_stack_write(ao_poly stack, bool write);
967 ao_scheme_stack_eval(void);
972 ao_scheme_vprintf(const char *format, va_list args);
975 ao_scheme_printf(const char *format, ...);
978 ao_scheme_error(int error, const char *format, ...);
982 #define AO_SCHEME_BUILTIN_DECLS
983 #include "ao_scheme_builtin.h"
985 /* debugging macros */
987 #if DBG_EVAL || DBG_READ
988 int ao_scheme_stack_depth;
993 #define DBG_INDENT() do { int _s; for(_s = 0; _s < ao_scheme_stack_depth; _s++) printf(" "); } while(0)
994 #define DBG_IN() (++ao_scheme_stack_depth)
995 #define DBG_OUT() (--ao_scheme_stack_depth)
996 #define DBG_RESET() (ao_scheme_stack_depth = 0)
997 #define DBG(...) ao_scheme_printf(__VA_ARGS__)
998 #define DBGI(...) do { printf("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
999 #define DBG_CONS(a) ao_scheme_cons_write(ao_scheme_cons_poly(a), true)
1000 #define DBG_POLY(a) ao_scheme_poly_write(a, true)
1001 #define OFFSET(a) ((a) ? (int) ((uint8_t *) a - ao_scheme_pool) : -1)
1002 #define DBG_STACK() ao_scheme_stack_write(ao_scheme_stack_poly(ao_scheme_stack), true)
1004 ao_scheme_frames_dump(void)
1006 struct ao_scheme_stack *s;
1007 DBGI(".. current frame: "); DBG_POLY(ao_scheme_frame_poly(ao_scheme_frame_current)); DBG("\n");
1008 for (s = ao_scheme_stack; s; s = ao_scheme_poly_stack(s->prev)) {
1009 DBGI(".. stack frame: "); DBG_POLY(s->frame); DBG("\n");
1012 #define DBG_FRAMES() ao_scheme_frames_dump()
1015 #define DBG_INDENT()
1024 #define DBG_FRAMES()
1028 #define RDBGI(...) do { printf("%4d: ", __LINE__); DBG_INDENT(); ao_scheme_printf(__VA_ARGS__); } while (0)
1029 #define RDBG_IN() (++ao_scheme_stack_depth)
1030 #define RDBG_OUT() (--ao_scheme_stack_depth)
1038 ao_scheme_mdbg_offset(void *a)
1045 if (ao_scheme_pool <= u && u < ao_scheme_pool + AO_SCHEME_POOL)
1046 return u - ao_scheme_pool;
1048 #ifndef AO_SCHEME_MAKE_CONST
1049 if (ao_scheme_const <= u && u < ao_scheme_const + AO_SCHEME_POOL_CONST)
1050 return - (int) (u - ao_scheme_const);
1055 #define MDBG_OFFSET(a) ao_scheme_mdbg_offset(a)
1059 #define DBG_MEM_START 1
1062 extern int dbg_move_depth;
1067 #define MDBG_DO(a) a
1068 #define MDBG_MOVE(...) do { if (dbg_mem) { int d; for (d = 0; d < dbg_move_depth; d++) printf (" "); printf(__VA_ARGS__); } } while (0)
1069 #define MDBG_MORE(...) do { if (dbg_mem) printf(__VA_ARGS__); } while (0)
1070 #define MDBG_MOVE_IN() (dbg_move_depth++)
1071 #define MDBG_MOVE_OUT() (assert(--dbg_move_depth >= 0))
1076 #define MDBG_MOVE(...)
1077 #define MDBG_MORE(...)
1078 #define MDBG_MOVE_IN()
1079 #define MDBG_MOVE_OUT()
1083 #endif /* _AO_SCHEME_H_ */