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
26 #define AO_SCHEME_BUILTIN_FEATURES
27 #include "ao_scheme_builtin.h"
28 #undef AO_SCHEME_BUILTIN_FEATURES
29 #include <ao_scheme_os.h>
34 typedef uint16_t ao_poly;
35 typedef int16_t ao_signed_poly;
39 struct ao_scheme_os_save {
42 uint16_t const_checksum;
43 uint16_t const_checksum_inv;
46 #define AO_SCHEME_POOL_EXTRA (sizeof(struct ao_scheme_os_save))
47 #define AO_SCHEME_POOL ((int) (AO_SCHEME_POOL_TOTAL - AO_SCHEME_POOL_EXTRA))
50 ao_scheme_os_save(void);
53 ao_scheme_os_restore_save(struct ao_scheme_os_save *save, int offset);
56 ao_scheme_os_restore(void);
60 #ifdef AO_SCHEME_MAKE_CONST
61 #define AO_SCHEME_POOL_CONST 16384
62 extern uint8_t ao_scheme_const[AO_SCHEME_POOL_CONST] __attribute__((aligned(4)));
63 #define ao_scheme_pool ao_scheme_const
64 #define AO_SCHEME_POOL AO_SCHEME_POOL_CONST
66 #define _atom(n) ao_scheme_atom_poly(ao_scheme_atom_intern((char *) n))
67 #define _bool(v) ao_scheme_bool_poly(ao_scheme_bool_get(v))
69 #define _ao_scheme_bool_true _bool(1)
70 #define _ao_scheme_bool_false _bool(0)
72 #define _ao_scheme_atom_eof _atom("eof")
73 #define _ao_scheme_atom_else _atom("else")
75 #define AO_SCHEME_BUILTIN_ATOMS
76 #include "ao_scheme_builtin.h"
79 #include "ao_scheme_const.h"
80 #ifndef AO_SCHEME_POOL
81 #define AO_SCHEME_POOL 3072
83 #ifndef AO_SCHEME_POOL_EXTRA
84 #define AO_SCHEME_POOL_EXTRA 0
86 extern uint8_t ao_scheme_pool[AO_SCHEME_POOL + AO_SCHEME_POOL_EXTRA] __attribute__((aligned(4)));
90 #define AO_SCHEME_CONS 0
91 #define AO_SCHEME_INT 1
92 #define AO_SCHEME_STRING 2
93 #define AO_SCHEME_OTHER 3
95 #define AO_SCHEME_TYPE_MASK 0x0003
96 #define AO_SCHEME_TYPE_SHIFT 2
97 #define AO_SCHEME_REF_MASK 0x7ffc
98 #define AO_SCHEME_CONST 0x8000
100 /* These have a type value at the start of the struct */
101 #define AO_SCHEME_ATOM 4
102 #define AO_SCHEME_BUILTIN 5
103 #define AO_SCHEME_FRAME 6
104 #define AO_SCHEME_FRAME_VALS 7
105 #define AO_SCHEME_LAMBDA 8
106 #define AO_SCHEME_STACK 9
107 #define AO_SCHEME_BOOL 10
108 #ifdef AO_SCHEME_FEATURE_BIGINT
109 #define AO_SCHEME_BIGINT 11
110 #define _AO_SCHEME_BIGINT AO_SCHEME_BIGINT
112 #define _AO_SCHEME_BIGINT AO_SCHEME_BOOL
114 #ifdef AO_SCHEME_FEATURE_FLOAT
115 #define AO_SCHEME_FLOAT (_AO_SCHEME_BIGINT + 1)
116 #define _AO_SCHEME_FLOAT AO_SCHEME_FLOAT
118 #define _AO_SCHEME_FLOAT _AO_SCHEME_BIGINT
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_val {
184 struct ao_scheme_frame_vals {
187 struct ao_scheme_val vals[];
190 struct ao_scheme_frame {
197 struct ao_scheme_bool {
204 #ifdef AO_SCHEME_FEATURE_FLOAT
205 struct ao_scheme_float {
213 #ifdef AO_SCHEME_FEATURE_VECTOR
214 struct ao_scheme_vector {
222 #define AO_SCHEME_MIN_INT (-(1 << (15 - AO_SCHEME_TYPE_SHIFT)))
223 #define AO_SCHEME_MAX_INT ((1 << (15 - AO_SCHEME_TYPE_SHIFT)) - 1)
225 #ifdef AO_SCHEME_FEATURE_BIGINT
226 struct ao_scheme_bigint {
230 #define AO_SCHEME_MIN_BIGINT (-(1 << 24))
231 #define AO_SCHEME_MAX_BIGINT ((1 << 24) - 1)
233 #if __BYTE_ORDER == __LITTLE_ENDIAN
235 static inline uint32_t
236 ao_scheme_int_bigint(int32_t i) {
237 return AO_SCHEME_BIGINT | (i << 8);
239 static inline int32_t
240 ao_scheme_bigint_int(uint32_t bi) {
241 return (int32_t) bi >> 8;
244 static inline uint32_t
245 ao_scheme_int_bigint(int32_t i) {
246 return (uint32_t) (i & 0xffffff) | (AO_SCHEME_BIGINT << 24);
248 static inlint int32_t
249 ao_scheme_bigint_int(uint32_t bi) {
250 return (int32_t) (bi << 8) >> 8;
253 #endif /* __BYTE_ORDER */
254 #endif /* AO_SCHEME_FEATURE_BIGINT */
256 #define AO_SCHEME_NOT_INTEGER 0x7fffffff
258 /* Set on type when the frame escapes the lambda */
259 #define AO_SCHEME_FRAME_MARK 0x80
260 #define AO_SCHEME_FRAME_PRINT 0x40
262 static inline int ao_scheme_frame_marked(struct ao_scheme_frame *f) {
263 return f->type & AO_SCHEME_FRAME_MARK;
266 static inline struct ao_scheme_frame *
267 ao_scheme_poly_frame(ao_poly poly) {
268 return ao_scheme_ref(poly);
271 static inline ao_poly
272 ao_scheme_frame_poly(struct ao_scheme_frame *frame) {
273 return ao_scheme_poly(frame, AO_SCHEME_OTHER);
276 static inline struct ao_scheme_frame_vals *
277 ao_scheme_poly_frame_vals(ao_poly poly) {
278 return ao_scheme_ref(poly);
281 static inline ao_poly
282 ao_scheme_frame_vals_poly(struct ao_scheme_frame_vals *vals) {
283 return ao_scheme_poly(vals, AO_SCHEME_OTHER);
287 eval_sexpr, /* Evaluate an sexpr */
288 eval_val, /* Value computed */
289 eval_formal, /* Formal computed */
290 eval_exec, /* Start a lambda evaluation */
291 eval_apply, /* Execute apply */
292 eval_cond, /* Start next cond clause */
293 eval_cond_test, /* Check cond condition */
294 eval_begin, /* Start next begin entry */
295 eval_while, /* Start while condition */
296 eval_while_test, /* Check while condition */
297 eval_macro, /* Finished with macro generation */
300 struct ao_scheme_stack {
301 uint8_t type; /* AO_SCHEME_STACK */
302 uint8_t state; /* enum eval_state */
303 ao_poly prev; /* previous stack frame */
304 ao_poly sexprs; /* expressions to evaluate */
305 ao_poly values; /* values computed */
306 ao_poly values_tail; /* end of the values list for easy appending */
307 ao_poly frame; /* current lookup frame */
308 ao_poly list; /* most recent function call */
311 #define AO_SCHEME_STACK_MARK 0x80 /* set on type when a reference has been taken */
312 #define AO_SCHEME_STACK_PRINT 0x40 /* stack is being printed */
314 static inline int ao_scheme_stack_marked(struct ao_scheme_stack *s) {
315 return s->type & AO_SCHEME_STACK_MARK;
318 static inline void ao_scheme_stack_mark(struct ao_scheme_stack *s) {
319 s->type |= AO_SCHEME_STACK_MARK;
322 static inline struct ao_scheme_stack *
323 ao_scheme_poly_stack(ao_poly p)
325 return ao_scheme_ref(p);
328 static inline ao_poly
329 ao_scheme_stack_poly(struct ao_scheme_stack *stack)
331 return ao_scheme_poly(stack, AO_SCHEME_OTHER);
334 extern ao_poly ao_scheme_v;
336 #define AO_SCHEME_FUNC_LAMBDA 0
337 #define AO_SCHEME_FUNC_NLAMBDA 1
338 #define AO_SCHEME_FUNC_MACRO 2
340 #define AO_SCHEME_FUNC_FREE_ARGS 0x80
341 #define AO_SCHEME_FUNC_MASK 0x7f
343 #define AO_SCHEME_FUNC_F_LAMBDA (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_LAMBDA)
344 #define AO_SCHEME_FUNC_F_NLAMBDA (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_NLAMBDA)
345 #define AO_SCHEME_FUNC_F_MACRO (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_MACRO)
347 struct ao_scheme_builtin {
353 #define AO_SCHEME_BUILTIN_ID
354 #include "ao_scheme_builtin.h"
356 typedef ao_poly (*ao_scheme_func_t)(struct ao_scheme_cons *cons);
358 extern const ao_scheme_func_t ao_scheme_builtins[];
360 static inline ao_scheme_func_t
361 ao_scheme_func(struct ao_scheme_builtin *b)
363 return ao_scheme_builtins[b->func];
366 struct ao_scheme_lambda {
373 static inline struct ao_scheme_lambda *
374 ao_scheme_poly_lambda(ao_poly poly)
376 return ao_scheme_ref(poly);
379 static inline ao_poly
380 ao_scheme_lambda_poly(struct ao_scheme_lambda *lambda)
382 return ao_scheme_poly(lambda, AO_SCHEME_OTHER);
386 ao_scheme_poly_other(ao_poly poly) {
387 return ao_scheme_ref(poly);
390 static inline uint8_t
391 ao_scheme_other_type(void *other) {
393 if ((*((uint8_t *) other) & AO_SCHEME_OTHER_TYPE_MASK) >= AO_SCHEME_NUM_TYPE)
396 return *((uint8_t *) other) & AO_SCHEME_OTHER_TYPE_MASK;
399 static inline ao_poly
400 ao_scheme_other_poly(const void *other)
402 return ao_scheme_poly(other, AO_SCHEME_OTHER);
406 ao_scheme_size_round(int size)
408 return (size + 3) & ~3;
412 ao_scheme_size(const struct ao_scheme_type *type, void *addr)
414 return ao_scheme_size_round(type->size(addr));
417 #define AO_SCHEME_OTHER_POLY(other) ((ao_poly)(other) + AO_SCHEME_OTHER)
419 static inline int ao_scheme_poly_base_type(ao_poly poly) {
420 return poly & AO_SCHEME_TYPE_MASK;
423 static inline int ao_scheme_poly_type(ao_poly poly) {
424 int type = poly & AO_SCHEME_TYPE_MASK;
425 if (type == AO_SCHEME_OTHER)
426 return ao_scheme_other_type(ao_scheme_poly_other(poly));
431 ao_scheme_is_cons(ao_poly poly) {
432 return (ao_scheme_poly_base_type(poly) == AO_SCHEME_CONS);
436 ao_scheme_is_pair(ao_poly poly) {
437 return poly != AO_SCHEME_NIL && (ao_scheme_poly_base_type(poly) == AO_SCHEME_CONS);
440 static inline struct ao_scheme_cons *
441 ao_scheme_poly_cons(ao_poly poly)
443 return ao_scheme_ref(poly);
446 static inline ao_poly
447 ao_scheme_cons_poly(struct ao_scheme_cons *cons)
449 return ao_scheme_poly(cons, AO_SCHEME_CONS);
452 static inline int32_t
453 ao_scheme_poly_int(ao_poly poly)
455 return (int32_t) ((ao_signed_poly) poly >> AO_SCHEME_TYPE_SHIFT);
458 static inline ao_poly
459 ao_scheme_int_poly(int32_t i)
461 return ((ao_poly) i << 2) | AO_SCHEME_INT;
464 #ifdef AO_SCHEME_FEATURE_BIGINT
465 static inline struct ao_scheme_bigint *
466 ao_scheme_poly_bigint(ao_poly poly)
468 return ao_scheme_ref(poly);
471 static inline ao_poly
472 ao_scheme_bigint_poly(struct ao_scheme_bigint *bi)
474 return ao_scheme_poly(bi, AO_SCHEME_OTHER);
476 #endif /* AO_SCHEME_FEATURE_BIGINT */
479 ao_scheme_poly_string(ao_poly poly)
481 return ao_scheme_ref(poly);
484 static inline ao_poly
485 ao_scheme_string_poly(char *s)
487 return ao_scheme_poly(s, AO_SCHEME_STRING);
490 static inline struct ao_scheme_atom *
491 ao_scheme_poly_atom(ao_poly poly)
493 return ao_scheme_ref(poly);
496 static inline ao_poly
497 ao_scheme_atom_poly(struct ao_scheme_atom *a)
499 return ao_scheme_poly(a, AO_SCHEME_OTHER);
502 static inline struct ao_scheme_builtin *
503 ao_scheme_poly_builtin(ao_poly poly)
505 return ao_scheme_ref(poly);
508 static inline ao_poly
509 ao_scheme_builtin_poly(struct ao_scheme_builtin *b)
511 return ao_scheme_poly(b, AO_SCHEME_OTHER);
514 static inline ao_poly
515 ao_scheme_bool_poly(struct ao_scheme_bool *b)
517 return ao_scheme_poly(b, AO_SCHEME_OTHER);
520 static inline struct ao_scheme_bool *
521 ao_scheme_poly_bool(ao_poly poly)
523 return ao_scheme_ref(poly);
526 #ifdef AO_SCHEME_FEATURE_FLOAT
527 static inline ao_poly
528 ao_scheme_float_poly(struct ao_scheme_float *f)
530 return ao_scheme_poly(f, AO_SCHEME_OTHER);
533 static inline struct ao_scheme_float *
534 ao_scheme_poly_float(ao_poly poly)
536 return ao_scheme_ref(poly);
540 ao_scheme_poly_number(ao_poly p);
543 #ifdef AO_SCHEME_FEATURE_VECTOR
544 static inline ao_poly
545 ao_scheme_vector_poly(struct ao_scheme_vector *v)
547 return ao_scheme_poly(v, AO_SCHEME_OTHER);
550 static inline struct ao_scheme_vector *
551 ao_scheme_poly_vector(ao_poly poly)
553 return ao_scheme_ref(poly);
557 /* memory functions */
559 extern uint64_t ao_scheme_collects[2];
560 extern uint64_t ao_scheme_freed[2];
561 extern uint64_t ao_scheme_loops[2];
563 /* returns 1 if the object was already marked */
565 ao_scheme_mark(const struct ao_scheme_type *type, void *addr);
567 /* returns 1 if the object was already marked */
569 ao_scheme_mark_memory(const struct ao_scheme_type *type, void *addr);
572 ao_scheme_move_map(void *addr);
574 /* returns 1 if the object was already moved */
576 ao_scheme_move(const struct ao_scheme_type *type, void **ref);
578 /* returns 1 if the object was already moved */
580 ao_scheme_move_memory(const struct ao_scheme_type *type, void **ref);
583 ao_scheme_alloc(int size);
585 #define AO_SCHEME_COLLECT_FULL 1
586 #define AO_SCHEME_COLLECT_INCREMENTAL 0
589 ao_scheme_collect(uint8_t style);
593 ao_scheme_cons_check(struct ao_scheme_cons *cons);
597 ao_scheme_cons_stash(int id, struct ao_scheme_cons *cons);
599 struct ao_scheme_cons *
600 ao_scheme_cons_fetch(int id);
603 ao_scheme_poly_stash(int id, ao_poly poly);
606 ao_scheme_poly_fetch(int id);
609 ao_scheme_string_stash(int id, char *string);
612 ao_scheme_string_fetch(int id);
615 ao_scheme_stack_stash(int id, struct ao_scheme_stack *stack) {
616 ao_scheme_poly_stash(id, ao_scheme_stack_poly(stack));
619 static inline struct ao_scheme_stack *
620 ao_scheme_stack_fetch(int id) {
621 return ao_scheme_poly_stack(ao_scheme_poly_fetch(id));
625 ao_scheme_frame_stash(int id, struct ao_scheme_frame *frame);
627 struct ao_scheme_frame *
628 ao_scheme_frame_fetch(int id);
632 extern const struct ao_scheme_type ao_scheme_bool_type;
635 ao_scheme_bool_write(ao_poly v);
637 #ifdef AO_SCHEME_MAKE_CONST
638 struct ao_scheme_bool *ao_scheme_true, *ao_scheme_false;
640 struct ao_scheme_bool *
641 ao_scheme_bool_get(uint8_t value);
645 extern const struct ao_scheme_type ao_scheme_cons_type;
647 struct ao_scheme_cons *
648 ao_scheme_cons_cons(ao_poly car, ao_poly cdr);
650 /* Return a cons or NULL for a proper list, else error */
651 struct ao_scheme_cons *
652 ao_scheme_cons_cdr(struct ao_scheme_cons *cons);
655 ao_scheme__cons(ao_poly car, ao_poly cdr);
657 extern struct ao_scheme_cons *ao_scheme_cons_free_list;
660 ao_scheme_cons_free(struct ao_scheme_cons *cons);
663 ao_scheme_cons_write(ao_poly);
666 ao_scheme_cons_display(ao_poly);
669 ao_scheme_cons_length(struct ao_scheme_cons *cons);
671 struct ao_scheme_cons *
672 ao_scheme_cons_copy(struct ao_scheme_cons *cons);
675 extern const struct ao_scheme_type ao_scheme_string_type;
678 ao_scheme_string_copy(char *a);
681 ao_scheme_string_cat(char *a, char *b);
684 ao_scheme_string_pack(struct ao_scheme_cons *cons);
687 ao_scheme_string_unpack(char *a);
690 ao_scheme_string_write(ao_poly s);
693 ao_scheme_string_display(ao_poly s);
696 extern const struct ao_scheme_type ao_scheme_atom_type;
698 extern struct ao_scheme_atom *ao_scheme_atoms;
699 extern struct ao_scheme_frame *ao_scheme_frame_global;
700 extern struct ao_scheme_frame *ao_scheme_frame_current;
703 ao_scheme_atom_write(ao_poly a);
705 struct ao_scheme_atom *
706 ao_scheme_atom_intern(char *name);
709 ao_scheme_atom_ref(ao_poly atom, struct ao_scheme_frame **frame_ref);
712 ao_scheme_atom_get(ao_poly atom);
715 ao_scheme_atom_set(ao_poly atom, ao_poly val);
718 ao_scheme_atom_def(ao_poly atom, ao_poly val);
722 ao_scheme_int_write(ao_poly i);
724 #ifdef AO_SCHEME_FEATURE_BIGINT
726 ao_scheme_poly_integer(ao_poly p);
729 ao_scheme_integer_poly(int32_t i);
732 ao_scheme_integer_typep(uint8_t t)
734 return (t == AO_SCHEME_INT) || (t == AO_SCHEME_BIGINT);
738 ao_scheme_bigint_write(ao_poly i);
740 extern const struct ao_scheme_type ao_scheme_bigint_type;
744 #define ao_scheme_poly_integer ao_scheme_poly_int
745 #define ao_scheme_integer_poly ao_scheme_int_poly
748 ao_scheme_integer_typep(uint8_t t)
750 return (t == AO_SCHEME_INT);
753 #endif /* AO_SCHEME_FEATURE_BIGINT */
758 ao_scheme_vector_write(ao_poly v);
761 ao_scheme_vector_display(ao_poly v);
763 struct ao_scheme_vector *
764 ao_scheme_vector_alloc(uint16_t length, ao_poly fill);
767 ao_scheme_vector_get(ao_poly v, ao_poly i);
770 ao_scheme_vector_set(ao_poly v, ao_poly i, ao_poly p);
772 struct ao_scheme_vector *
773 ao_scheme_list_to_vector(struct ao_scheme_cons *cons);
775 struct ao_scheme_cons *
776 ao_scheme_vector_to_list(struct ao_scheme_vector *vector);
778 extern const struct ao_scheme_type ao_scheme_vector_type;
781 void (*ao_scheme_poly_write_func(ao_poly p))(ao_poly p);
782 void (*ao_scheme_poly_display_func(ao_poly p))(ao_poly p);
785 ao_scheme_poly_write(ao_poly p) { (*ao_scheme_poly_write_func(p))(p); }
788 ao_scheme_poly_display(ao_poly p) { (*ao_scheme_poly_display_func(p))(p); }
791 ao_scheme_poly_mark(ao_poly p, uint8_t note_cons);
793 /* returns 1 if the object has already been moved */
795 ao_scheme_poly_move(ao_poly *p, uint8_t note_cons);
800 ao_scheme_eval_clear_globals(void);
803 ao_scheme_eval_restart(void);
806 ao_scheme_eval(ao_poly p);
809 ao_scheme_set_cond(struct ao_scheme_cons *cons);
812 #ifdef AO_SCHEME_FEATURE_FLOAT
813 extern const struct ao_scheme_type ao_scheme_float_type;
816 ao_scheme_float_write(ao_poly p);
819 ao_scheme_float_get(float value);
822 #ifdef AO_SCHEME_FEATURE_FLOAT
823 static inline uint8_t
824 ao_scheme_number_typep(uint8_t t)
826 return ao_scheme_integer_typep(t) || (t == AO_SCHEME_FLOAT);
830 ao_scheme_poly_number(ao_poly p);
832 #define ao_scheme_number_typep ao_scheme_integer_typep
833 #define ao_scheme_poly_number ao_scheme_poly_integer
838 ao_scheme_builtin_write(ao_poly b);
840 extern const struct ao_scheme_type ao_scheme_builtin_type;
842 /* Check argument count */
844 ao_scheme_check_argc(ao_poly name, struct ao_scheme_cons *cons, int min, int max);
846 /* Check argument type */
848 ao_scheme_check_argt(ao_poly name, struct ao_scheme_cons *cons, int argc, int type, int nil_ok);
850 /* Fetch an arg (nil if off the end) */
852 ao_scheme_arg(struct ao_scheme_cons *cons, int argc);
855 ao_scheme_args_name(uint8_t args);
858 extern int ao_scheme_read_list;
859 extern struct ao_scheme_cons *ao_scheme_read_cons;
860 extern struct ao_scheme_cons *ao_scheme_read_cons_tail;
861 extern struct ao_scheme_cons *ao_scheme_read_stack;
864 ao_scheme_read(void);
868 ao_scheme_read_eval_print(void);
871 extern const struct ao_scheme_type ao_scheme_frame_type;
872 extern const struct ao_scheme_type ao_scheme_frame_vals_type;
874 #define AO_SCHEME_FRAME_FREE 6
876 extern struct ao_scheme_frame *ao_scheme_frame_free_list[AO_SCHEME_FRAME_FREE];
879 ao_scheme_frame_mark(struct ao_scheme_frame *frame);
882 ao_scheme_frame_ref(struct ao_scheme_frame *frame, ao_poly atom);
884 struct ao_scheme_frame *
885 ao_scheme_frame_new(int num);
888 ao_scheme_frame_free(struct ao_scheme_frame *frame);
891 ao_scheme_frame_bind(struct ao_scheme_frame *frame, int num, ao_poly atom, ao_poly val);
894 ao_scheme_frame_add(struct ao_scheme_frame *frame, ao_poly atom, ao_poly val);
897 ao_scheme_frame_write(ao_poly p);
900 ao_scheme_frame_init(void);
903 extern const struct ao_scheme_type ao_scheme_lambda_type;
905 extern const char * const ao_scheme_state_names[];
907 struct ao_scheme_lambda *
908 ao_scheme_lambda_new(ao_poly cons);
911 ao_scheme_lambda_write(ao_poly lambda);
914 ao_scheme_lambda_eval(void);
918 extern const struct ao_scheme_type ao_scheme_stack_type;
919 extern struct ao_scheme_stack *ao_scheme_stack;
920 extern struct ao_scheme_stack *ao_scheme_stack_free_list;
923 ao_scheme_stack_reset(struct ao_scheme_stack *stack);
926 ao_scheme_stack_push(void);
929 ao_scheme_stack_pop(void);
932 ao_scheme_stack_clear(void);
935 ao_scheme_stack_write(ao_poly stack);
938 ao_scheme_stack_eval(void);
943 ao_scheme_vprintf(const char *format, va_list args);
946 ao_scheme_printf(const char *format, ...);
949 ao_scheme_error_poly(const char *name, ao_poly poly, ao_poly last);
952 ao_scheme_error_frame(int indent, const char *name, struct ao_scheme_frame *frame);
955 ao_scheme_error(int error, const char *format, ...);
959 #define AO_SCHEME_BUILTIN_DECLS
960 #include "ao_scheme_builtin.h"
962 /* debugging macros */
964 #if DBG_EVAL || DBG_READ || DBG_MEM
966 int ao_scheme_stack_depth;
968 #define DBG_INDENT() do { int _s; for(_s = 0; _s < ao_scheme_stack_depth; _s++) printf(" "); } while(0)
969 #define DBG_IN() (++ao_scheme_stack_depth)
970 #define DBG_OUT() (--ao_scheme_stack_depth)
971 #define DBG_RESET() (ao_scheme_stack_depth = 0)
972 #define DBG(...) ao_scheme_printf(__VA_ARGS__)
973 #define DBGI(...) do { printf("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
974 #define DBG_CONS(a) ao_scheme_cons_write(ao_scheme_cons_poly(a))
975 #define DBG_POLY(a) ao_scheme_poly_write(a)
976 #define OFFSET(a) ((a) ? (int) ((uint8_t *) a - ao_scheme_pool) : -1)
977 #define DBG_STACK() ao_scheme_stack_write(ao_scheme_stack_poly(ao_scheme_stack))
979 ao_scheme_frames_dump(void)
981 struct ao_scheme_stack *s;
982 DBGI(".. current frame: "); DBG_POLY(ao_scheme_frame_poly(ao_scheme_frame_current)); DBG("\n");
983 for (s = ao_scheme_stack; s; s = ao_scheme_poly_stack(s->prev)) {
984 DBGI(".. stack frame: "); DBG_POLY(s->frame); DBG("\n");
987 #define DBG_FRAMES() ao_scheme_frames_dump()
1003 #define RDBGI(...) DBGI(__VA_ARGS__)
1004 #define RDBG_IN() DBG_IN()
1005 #define RDBG_OUT() DBG_OUT()
1012 #define DBG_MEM_START 1
1017 extern int dbg_move_depth;
1019 #define MDBG_OFFSET(a) ((a) ? (int) ((uint8_t *) (a) - ao_scheme_pool) : -1)
1023 #define MDBG_DO(a) DBG_DO(a)
1024 #define MDBG_MOVE(...) do { if (dbg_mem) { int d; for (d = 0; d < dbg_move_depth; d++) printf (" "); printf(__VA_ARGS__); } } while (0)
1025 #define MDBG_MORE(...) do { if (dbg_mem) printf(__VA_ARGS__); } while (0)
1026 #define MDBG_MOVE_IN() (dbg_move_depth++)
1027 #define MDBG_MOVE_OUT() (assert(--dbg_move_depth >= 0))
1032 #define MDBG_MOVE(...)
1033 #define MDBG_MORE(...)
1034 #define MDBG_MOVE_IN()
1035 #define MDBG_MOVE_OUT()
1039 #endif /* _AO_SCHEME_H_ */