b37e9098fc1d6c144fea88f4e8d05bf3cceda132
[fw/altos] / src / scheme / ao_scheme.h
1 /*
2  * Copyright © 2016 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  */
14
15 #ifndef _AO_SCHEME_H_
16 #define _AO_SCHEME_H_
17
18 #ifndef DBG_MEM
19 #define DBG_MEM         0
20 #endif
21 #ifndef DBG_EVAL
22 #define DBG_EVAL        0
23 #endif
24 #ifndef DBG_READ
25 #define DBG_READ        0
26 #endif
27 #ifndef DBG_FREE_CONS
28 #define DBG_FREE_CONS   0
29 #endif
30 #define NDEBUG          1
31
32 #include <stdint.h>
33 #include <string.h>
34 #include <stdbool.h>
35 #define AO_SCHEME_BUILTIN_FEATURES
36 #include "ao_scheme_builtin.h"
37 #undef AO_SCHEME_BUILTIN_FEATURES
38 #include <ao_scheme_os.h>
39 #ifndef __BYTE_ORDER
40 #include <endian.h>
41 #endif
42
43 typedef uint16_t        ao_poly;
44 typedef int16_t         ao_signed_poly;
45
46 #if AO_SCHEME_SAVE
47
48 struct ao_scheme_os_save {
49         ao_poly         atoms;
50         ao_poly         globals;
51         uint16_t        const_checksum;
52         uint16_t        const_checksum_inv;
53 };
54
55 #ifndef AO_SCHEME_POOL_TOTAL
56 #error Must define AO_SCHEME_POOL_TOTAL for AO_SCHEME_SAVE
57 #endif
58
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))
61
62 int
63 ao_scheme_os_save(void);
64
65 int
66 ao_scheme_os_restore_save(struct ao_scheme_os_save *save, int offset);
67
68 int
69 ao_scheme_os_restore(void);
70
71 #endif
72
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
78
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))
81
82 #define _ao_scheme_bool_true    _bool(1)
83 #define _ao_scheme_bool_false   _bool(0)
84
85 #define _ao_scheme_atom_eof     _atom("eof")
86 #define _ao_scheme_atom_else    _atom("else")
87
88 #define AO_SCHEME_BUILTIN_ATOMS
89 #include "ao_scheme_builtin.h"
90
91 #else
92 #include "ao_scheme_const.h"
93 #ifndef AO_SCHEME_POOL
94 #error Must define AO_SCHEME_POOL
95 #endif
96 #ifndef AO_SCHEME_POOL_EXTRA
97 #define AO_SCHEME_POOL_EXTRA 0
98 #endif
99 extern uint8_t          ao_scheme_pool[AO_SCHEME_POOL + AO_SCHEME_POOL_EXTRA] __attribute__((aligned(4)));
100 #endif
101
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
107
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
112
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
125 #else
126 #define _AO_SCHEME_FLOAT        12
127 #endif
128 #ifdef AO_SCHEME_FEATURE_VECTOR
129 #define AO_SCHEME_VECTOR        13
130 #define _AO_SCHEME_VECTOR       AO_SCHEME_VECTOR
131 #else
132 #define _AO_SCHEME_VECTOR       _AO_SCHEME_FLOAT
133 #endif
134 #define AO_SCHEME_NUM_TYPE      (_AO_SCHEME_VECTOR+1)
135
136 /* Leave two bits for types to use as they please */
137 #define AO_SCHEME_OTHER_TYPE_MASK       0x3f
138
139 #define AO_SCHEME_NIL   0
140
141 extern uint16_t         ao_scheme_top;
142
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
150
151 extern uint8_t          ao_scheme_exception;
152
153 static inline int
154 ao_scheme_is_const(ao_poly poly) {
155         return poly & AO_SCHEME_CONST;
156 }
157
158 #define AO_SCHEME_IS_CONST(a)   (ao_scheme_const <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_scheme_const + AO_SCHEME_POOL_CONST)
159 #define AO_SCHEME_IS_POOL(a)    (ao_scheme_pool <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_scheme_pool + AO_SCHEME_POOL)
160 #define AO_SCHEME_IS_INT(p)     (ao_scheme_poly_base_type(p) == AO_SCHEME_INT)
161 #define AO_SCHEME_IS_CONS(p)    (ao_scheme_poly_base_type(p) == AO_SCHEME_CONS)
162
163 void *
164 ao_scheme_ref(ao_poly poly);
165
166 ao_poly
167 ao_scheme_poly(const void *addr, ao_poly type);
168
169 struct ao_scheme_type {
170         int     (*size)(void *addr);
171         void    (*mark)(void *addr);
172         void    (*move)(void *addr);
173         char    name[];
174 };
175
176 struct ao_scheme_cons {
177         ao_poly         car;
178         ao_poly         cdr;
179 };
180
181 struct ao_scheme_atom {
182         uint8_t         type;
183         uint8_t         pad[1];
184         ao_poly         next;
185         char            name[];
186 };
187
188 struct ao_scheme_string {
189         uint8_t         type;
190         char            val[];
191 };
192
193 struct ao_scheme_val {
194         ao_poly         atom;
195         ao_poly         val;
196 };
197
198 struct ao_scheme_frame_vals {
199         uint8_t                 type;
200         uint8_t                 size;
201         struct ao_scheme_val    vals[];
202 };
203
204 struct ao_scheme_frame {
205         uint8_t                 type;
206         uint8_t                 num;
207         ao_poly                 prev;
208         ao_poly                 vals;
209 };
210
211 struct ao_scheme_bool {
212         uint8_t                 type;
213         uint8_t                 value;
214         uint16_t                pad;
215 };
216
217
218 #ifdef AO_SCHEME_FEATURE_FLOAT
219 struct ao_scheme_float {
220         uint8_t                 type;
221         uint8_t                 pad1;
222         uint16_t                pad2;
223         float                   value;
224 };
225 #endif
226
227 #ifdef AO_SCHEME_FEATURE_VECTOR
228 struct ao_scheme_vector {
229         uint8_t                 type;
230         uint8_t                 pad1;
231         uint16_t                length;
232         ao_poly                 vals[];
233 };
234 #endif
235
236 #define AO_SCHEME_MIN_INT       (-(1 << (15 - AO_SCHEME_TYPE_SHIFT)))
237 #define AO_SCHEME_MAX_INT       ((1 << (15 - AO_SCHEME_TYPE_SHIFT)) - 1)
238
239 #ifdef AO_SCHEME_FEATURE_BIGINT
240
241 struct ao_scheme_bigint {
242         uint32_t                value;
243 };
244
245 #define AO_SCHEME_MIN_BIGINT    INT32_MIN
246 #define AO_SCHEME_MAX_BIGINT    INT32_MAX
247
248 #endif  /* AO_SCHEME_FEATURE_BIGINT */
249
250 /* Set on type when the frame escapes the lambda */
251 #define AO_SCHEME_FRAME_MARK    0x80
252
253 static inline int ao_scheme_frame_marked(struct ao_scheme_frame *f) {
254         return f->type & AO_SCHEME_FRAME_MARK;
255 }
256
257 static inline struct ao_scheme_frame *
258 ao_scheme_poly_frame(ao_poly poly) {
259         return ao_scheme_ref(poly);
260 }
261
262 static inline ao_poly
263 ao_scheme_frame_poly(struct ao_scheme_frame *frame) {
264         return ao_scheme_poly(frame, AO_SCHEME_OTHER);
265 }
266
267 static inline struct ao_scheme_frame_vals *
268 ao_scheme_poly_frame_vals(ao_poly poly) {
269         return ao_scheme_ref(poly);
270 }
271
272 static inline ao_poly
273 ao_scheme_frame_vals_poly(struct ao_scheme_frame_vals *vals) {
274         return ao_scheme_poly(vals, AO_SCHEME_OTHER);
275 }
276
277 enum eval_state {
278         eval_sexpr,             /* Evaluate an sexpr */
279         eval_val,               /* Value computed */
280         eval_formal,            /* Formal computed */
281         eval_exec,              /* Start a lambda evaluation */
282         eval_apply,             /* Execute apply */
283         eval_cond,              /* Start next cond clause */
284         eval_cond_test,         /* Check cond condition */
285         eval_begin,             /* Start next begin entry */
286         eval_while,             /* Start while condition */
287         eval_while_test,        /* Check while condition */
288         eval_macro,             /* Finished with macro generation */
289 };
290
291 struct ao_scheme_stack {
292         uint8_t                 type;           /* AO_SCHEME_STACK */
293         uint8_t                 state;          /* enum eval_state */
294         ao_poly                 prev;           /* previous stack frame */
295         ao_poly                 sexprs;         /* expressions to evaluate */
296         ao_poly                 values;         /* values computed */
297         ao_poly                 values_tail;    /* end of the values list for easy appending */
298         ao_poly                 frame;          /* current lookup frame */
299         ao_poly                 list;           /* most recent function call */
300 };
301
302 #define AO_SCHEME_STACK_MARK    0x80    /* set on type when a reference has been taken */
303
304 static inline int ao_scheme_stack_marked(struct ao_scheme_stack *s) {
305         return s->type & AO_SCHEME_STACK_MARK;
306 }
307
308 static inline void ao_scheme_stack_mark(struct ao_scheme_stack *s) {
309         s->type |= AO_SCHEME_STACK_MARK;
310 }
311
312 static inline struct ao_scheme_stack *
313 ao_scheme_poly_stack(ao_poly p)
314 {
315         return ao_scheme_ref(p);
316 }
317
318 static inline ao_poly
319 ao_scheme_stack_poly(struct ao_scheme_stack *stack)
320 {
321         return ao_scheme_poly(stack, AO_SCHEME_OTHER);
322 }
323
324 extern ao_poly                  ao_scheme_v;
325
326 #define AO_SCHEME_FUNC_LAMBDA           0
327 #define AO_SCHEME_FUNC_NLAMBDA          1
328 #define AO_SCHEME_FUNC_MACRO            2
329
330 #define AO_SCHEME_FUNC_FREE_ARGS        0x80
331 #define AO_SCHEME_FUNC_MASK             0x7f
332
333 #define AO_SCHEME_FUNC_F_LAMBDA         (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_LAMBDA)
334 #define AO_SCHEME_FUNC_F_NLAMBDA        (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_NLAMBDA)
335 #define AO_SCHEME_FUNC_F_MACRO          (AO_SCHEME_FUNC_FREE_ARGS | AO_SCHEME_FUNC_MACRO)
336
337 struct ao_scheme_builtin {
338         uint8_t         type;
339         uint8_t         args;
340         uint16_t        func;
341 };
342
343 #define AO_SCHEME_BUILTIN_ID
344 #include "ao_scheme_builtin.h"
345
346 typedef ao_poly (*ao_scheme_func_t)(struct ao_scheme_cons *cons);
347
348 extern const ao_scheme_func_t   ao_scheme_builtins[];
349
350 static inline ao_scheme_func_t
351 ao_scheme_func(struct ao_scheme_builtin *b)
352 {
353         return ao_scheme_builtins[b->func];
354 }
355
356 struct ao_scheme_lambda {
357         uint8_t         type;
358         uint8_t         args;
359         ao_poly         code;
360         ao_poly         frame;
361 };
362
363 static inline struct ao_scheme_lambda *
364 ao_scheme_poly_lambda(ao_poly poly)
365 {
366         return ao_scheme_ref(poly);
367 }
368
369 static inline ao_poly
370 ao_scheme_lambda_poly(struct ao_scheme_lambda *lambda)
371 {
372         return ao_scheme_poly(lambda, AO_SCHEME_OTHER);
373 }
374
375 static inline void *
376 ao_scheme_poly_other(ao_poly poly) {
377         return ao_scheme_ref(poly);
378 }
379
380 static inline uint8_t
381 ao_scheme_other_type(void *other) {
382 #if DBG_MEM
383         if ((*((uint8_t *) other) & AO_SCHEME_OTHER_TYPE_MASK) >= AO_SCHEME_NUM_TYPE)
384                 ao_scheme_abort();
385 #endif
386         return *((uint8_t *) other) & AO_SCHEME_OTHER_TYPE_MASK;
387 }
388
389 static inline ao_poly
390 ao_scheme_other_poly(const void *other)
391 {
392         return ao_scheme_poly(other, AO_SCHEME_OTHER);
393 }
394
395 static inline int
396 ao_scheme_size_round(int size)
397 {
398         return (size + 3) & ~3;
399 }
400
401 static inline int
402 ao_scheme_size(const struct ao_scheme_type *type, void *addr)
403 {
404         return ao_scheme_size_round(type->size(addr));
405 }
406
407 #define AO_SCHEME_OTHER_POLY(other) ((ao_poly)(other) + AO_SCHEME_OTHER)
408
409 static inline int ao_scheme_poly_base_type(ao_poly poly) {
410         return poly & AO_SCHEME_TYPE_MASK;
411 }
412
413 static inline int ao_scheme_poly_type(ao_poly poly) {
414         int     type = poly & AO_SCHEME_TYPE_MASK;
415         if (type == AO_SCHEME_OTHER)
416                 return ao_scheme_other_type(ao_scheme_poly_other(poly));
417         return type;
418 }
419
420 static inline int
421 ao_scheme_is_cons(ao_poly poly) {
422         return (ao_scheme_poly_base_type(poly) == AO_SCHEME_CONS);
423 }
424
425 static inline int
426 ao_scheme_is_pair(ao_poly poly) {
427         return poly != AO_SCHEME_NIL && (ao_scheme_poly_base_type(poly) == AO_SCHEME_CONS);
428 }
429
430 static inline struct ao_scheme_cons *
431 ao_scheme_poly_cons(ao_poly poly)
432 {
433         return ao_scheme_ref(poly);
434 }
435
436 static inline ao_poly
437 ao_scheme_cons_poly(struct ao_scheme_cons *cons)
438 {
439         return ao_scheme_poly(cons, AO_SCHEME_CONS);
440 }
441
442 static inline int32_t
443 ao_scheme_poly_int(ao_poly poly)
444 {
445         return (int32_t) ((ao_signed_poly) poly >> AO_SCHEME_TYPE_SHIFT);
446 }
447
448 static inline ao_poly
449 ao_scheme_int_poly(int32_t i)
450 {
451         return ((ao_poly) i << 2) | AO_SCHEME_INT;
452 }
453
454 #ifdef AO_SCHEME_FEATURE_BIGINT
455 static inline struct ao_scheme_bigint *
456 ao_scheme_poly_bigint(ao_poly poly)
457 {
458         return ao_scheme_ref(poly);
459 }
460
461 static inline ao_poly
462 ao_scheme_bigint_poly(struct ao_scheme_bigint *bi)
463 {
464         return ao_scheme_poly(bi, AO_SCHEME_BIGINT);
465 }
466 #endif /* AO_SCHEME_FEATURE_BIGINT */
467
468 static inline struct ao_scheme_string *
469 ao_scheme_poly_string(ao_poly poly)
470 {
471         return ao_scheme_ref(poly);
472 }
473
474 static inline ao_poly
475 ao_scheme_string_poly(struct ao_scheme_string *s)
476 {
477         return ao_scheme_poly(s, AO_SCHEME_OTHER);
478 }
479
480 static inline struct ao_scheme_atom *
481 ao_scheme_poly_atom(ao_poly poly)
482 {
483         return ao_scheme_ref(poly);
484 }
485
486 static inline ao_poly
487 ao_scheme_atom_poly(struct ao_scheme_atom *a)
488 {
489         return ao_scheme_poly(a, AO_SCHEME_OTHER);
490 }
491
492 static inline struct ao_scheme_builtin *
493 ao_scheme_poly_builtin(ao_poly poly)
494 {
495         return ao_scheme_ref(poly);
496 }
497
498 static inline ao_poly
499 ao_scheme_builtin_poly(struct ao_scheme_builtin *b)
500 {
501         return ao_scheme_poly(b, AO_SCHEME_OTHER);
502 }
503
504 static inline ao_poly
505 ao_scheme_bool_poly(struct ao_scheme_bool *b)
506 {
507         return ao_scheme_poly(b, AO_SCHEME_OTHER);
508 }
509
510 static inline struct ao_scheme_bool *
511 ao_scheme_poly_bool(ao_poly poly)
512 {
513         return ao_scheme_ref(poly);
514 }
515
516 #ifdef AO_SCHEME_FEATURE_FLOAT
517 static inline ao_poly
518 ao_scheme_float_poly(struct ao_scheme_float *f)
519 {
520         return ao_scheme_poly(f, AO_SCHEME_OTHER);
521 }
522
523 static inline struct ao_scheme_float *
524 ao_scheme_poly_float(ao_poly poly)
525 {
526         return ao_scheme_ref(poly);
527 }
528
529 float
530 ao_scheme_poly_number(ao_poly p);
531 #endif
532
533 #ifdef AO_SCHEME_FEATURE_VECTOR
534 static inline ao_poly
535 ao_scheme_vector_poly(struct ao_scheme_vector *v)
536 {
537         return ao_scheme_poly(v, AO_SCHEME_OTHER);
538 }
539
540 static inline struct ao_scheme_vector *
541 ao_scheme_poly_vector(ao_poly poly)
542 {
543         return ao_scheme_ref(poly);
544 }
545 #endif
546
547 /* memory functions */
548
549 extern uint64_t ao_scheme_collects[2];
550 extern uint64_t ao_scheme_freed[2];
551 extern uint64_t ao_scheme_loops[2];
552
553 /* returns 1 if the object was already marked */
554 int
555 ao_scheme_mark_memory(const struct ao_scheme_type *type, void *addr);
556
557 /* returns 1 if the object was already moved */
558 int
559 ao_scheme_move_memory(const struct ao_scheme_type *type, void **ref);
560
561 void *
562 ao_scheme_alloc(int size);
563
564 /* Marks an object as being printed, returns 1 if it was already marked */
565 int
566 ao_scheme_print_mark_addr(void *addr);
567
568 void
569 ao_scheme_print_clear_addr(void *addr);
570
571 /* Notes that printing has started */
572 void
573 ao_scheme_print_start(void);
574
575 /* Notes that printing has ended, returns 1 if printing is still happening */
576 int
577 ao_scheme_print_stop(void);
578
579 #define AO_SCHEME_COLLECT_FULL          1
580 #define AO_SCHEME_COLLECT_INCREMENTAL   0
581
582 int
583 ao_scheme_collect(uint8_t style);
584
585 #if DBG_FREE_CONS
586 void
587 ao_scheme_cons_check(struct ao_scheme_cons *cons);
588 #endif
589
590 void
591 ao_scheme_cons_stash(int id, struct ao_scheme_cons *cons);
592
593 struct ao_scheme_cons *
594 ao_scheme_cons_fetch(int id);
595
596 void
597 ao_scheme_poly_stash(int id, ao_poly poly);
598
599 ao_poly
600 ao_scheme_poly_fetch(int id);
601
602 void
603 ao_scheme_string_stash(int id, struct ao_scheme_string *string);
604
605 struct ao_scheme_string *
606 ao_scheme_string_fetch(int id);
607
608 static inline void
609 ao_scheme_stack_stash(int id, struct ao_scheme_stack *stack) {
610         ao_scheme_poly_stash(id, ao_scheme_stack_poly(stack));
611 }
612
613 static inline struct ao_scheme_stack *
614 ao_scheme_stack_fetch(int id) {
615         return ao_scheme_poly_stack(ao_scheme_poly_fetch(id));
616 }
617
618 void
619 ao_scheme_frame_stash(int id, struct ao_scheme_frame *frame);
620
621 struct ao_scheme_frame *
622 ao_scheme_frame_fetch(int id);
623
624 /* bool */
625
626 extern const struct ao_scheme_type ao_scheme_bool_type;
627
628 void
629 ao_scheme_bool_write(ao_poly v, bool write);
630
631 #ifdef AO_SCHEME_MAKE_CONST
632 extern struct ao_scheme_bool    *ao_scheme_true, *ao_scheme_false;
633
634 struct ao_scheme_bool *
635 ao_scheme_bool_get(uint8_t value);
636 #endif
637
638 /* cons */
639 extern const struct ao_scheme_type ao_scheme_cons_type;
640
641 struct ao_scheme_cons *
642 ao_scheme_cons_cons(ao_poly car, ao_poly cdr);
643
644 /* Return a cons or NULL for a proper list, else error */
645 struct ao_scheme_cons *
646 ao_scheme_cons_cdr(struct ao_scheme_cons *cons);
647
648 ao_poly
649 ao_scheme__cons(ao_poly car, ao_poly cdr);
650
651 extern struct ao_scheme_cons *ao_scheme_cons_free_list;
652
653 void
654 ao_scheme_cons_free(struct ao_scheme_cons *cons);
655
656 void
657 ao_scheme_cons_write(ao_poly, bool write);
658
659 int
660 ao_scheme_cons_length(struct ao_scheme_cons *cons);
661
662 struct ao_scheme_cons *
663 ao_scheme_cons_copy(struct ao_scheme_cons *cons);
664
665 /* string */
666 extern const struct ao_scheme_type ao_scheme_string_type;
667
668 struct ao_scheme_string *
669 ao_scheme_string_copy(struct ao_scheme_string *a);
670
671 struct ao_scheme_string *
672 ao_scheme_string_make(char *a);
673
674 struct ao_scheme_string *
675 ao_scheme_atom_to_string(struct ao_scheme_atom *a);
676
677 struct ao_scheme_string *
678 ao_scheme_string_cat(struct ao_scheme_string *a, struct ao_scheme_string *b);
679
680 ao_poly
681 ao_scheme_string_pack(struct ao_scheme_cons *cons);
682
683 ao_poly
684 ao_scheme_string_unpack(struct ao_scheme_string *a);
685
686 void
687 ao_scheme_string_write(ao_poly s, bool write);
688
689 /* atom */
690 extern const struct ao_scheme_type ao_scheme_atom_type;
691
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;
695
696 void
697 ao_scheme_atom_write(ao_poly a, bool write);
698
699 struct ao_scheme_atom *
700 ao_scheme_string_to_atom(struct ao_scheme_string *string);
701
702 struct ao_scheme_atom *
703 ao_scheme_atom_intern(char *name);
704
705 ao_poly *
706 ao_scheme_atom_ref(ao_poly atom, struct ao_scheme_frame **frame_ref);
707
708 ao_poly
709 ao_scheme_atom_get(ao_poly atom);
710
711 ao_poly
712 ao_scheme_atom_set(ao_poly atom, ao_poly val);
713
714 ao_poly
715 ao_scheme_atom_def(ao_poly atom, ao_poly val);
716
717 /* int */
718 void
719 ao_scheme_int_write(ao_poly i, bool write);
720
721 #ifdef AO_SCHEME_FEATURE_BIGINT
722 int32_t
723 ao_scheme_poly_integer(ao_poly p, bool *fail);
724
725 ao_poly
726 ao_scheme_integer_poly(int32_t i);
727
728 static inline int
729 ao_scheme_integer_typep(uint8_t t)
730 {
731         return (t == AO_SCHEME_INT) || (t == AO_SCHEME_BIGINT);
732 }
733
734 void
735 ao_scheme_bigint_write(ao_poly i, bool write);
736
737 extern const struct ao_scheme_type      ao_scheme_bigint_type;
738
739 #else
740
741 #define ao_scheme_poly_integer(a,b) ao_scheme_poly_int(a)
742 #define ao_scheme_integer_poly ao_scheme_int_poly
743
744 static inline int
745 ao_scheme_integer_typep(uint8_t t)
746 {
747         return (t == AO_SCHEME_INT);
748 }
749
750 #endif /* AO_SCHEME_FEATURE_BIGINT */
751
752 /* vector */
753
754 void
755 ao_scheme_vector_write(ao_poly v, bool write);
756
757 struct ao_scheme_vector *
758 ao_scheme_vector_alloc(uint16_t length, ao_poly fill);
759
760 ao_poly
761 ao_scheme_vector_get(ao_poly v, ao_poly i);
762
763 ao_poly
764 ao_scheme_vector_set(ao_poly v, ao_poly i, ao_poly p);
765
766 struct ao_scheme_vector *
767 ao_scheme_list_to_vector(struct ao_scheme_cons *cons);
768
769 struct ao_scheme_cons *
770 ao_scheme_vector_to_list(struct ao_scheme_vector *vector);
771
772 extern const struct ao_scheme_type      ao_scheme_vector_type;
773
774 /* prim */
775 void (*ao_scheme_poly_write_func(ao_poly p))(ao_poly p, bool write);
776
777 static inline void
778 ao_scheme_poly_write(ao_poly p, bool write) { (*ao_scheme_poly_write_func(p))(p, write); }
779
780 int
781 ao_scheme_poly_mark(ao_poly p, uint8_t note_cons);
782
783 /* returns 1 if the object has already been moved */
784 int
785 ao_scheme_poly_move(ao_poly *p, uint8_t note_cons);
786
787 /* eval */
788
789 void
790 ao_scheme_eval_clear_globals(void);
791
792 int
793 ao_scheme_eval_restart(void);
794
795 ao_poly
796 ao_scheme_eval(ao_poly p);
797
798 ao_poly
799 ao_scheme_set_cond(struct ao_scheme_cons *cons);
800
801 /* float */
802 #ifdef AO_SCHEME_FEATURE_FLOAT
803 extern const struct ao_scheme_type ao_scheme_float_type;
804
805 void
806 ao_scheme_float_write(ao_poly p, bool write);
807
808 ao_poly
809 ao_scheme_float_get(float value);
810 #endif
811
812 #ifdef AO_SCHEME_FEATURE_FLOAT
813 static inline uint8_t
814 ao_scheme_number_typep(uint8_t t)
815 {
816         return ao_scheme_integer_typep(t) || (t == AO_SCHEME_FLOAT);
817 }
818 #else
819 #define ao_scheme_number_typep ao_scheme_integer_typep
820 #endif
821
822 /* builtin */
823 void
824 ao_scheme_builtin_write(ao_poly b, bool write);
825
826 extern const struct ao_scheme_type ao_scheme_builtin_type;
827
828 /* Check argument count */
829 ao_poly
830 ao_scheme_check_argc(ao_poly name, struct ao_scheme_cons *cons, int min, int max);
831
832 /* Check argument type */
833 ao_poly
834 ao_scheme_check_argt(ao_poly name, struct ao_scheme_cons *cons, int argc, int type, int nil_ok);
835
836 /* Fetch an arg (nil if off the end) */
837 ao_poly
838 ao_scheme_arg(struct ao_scheme_cons *cons, int argc);
839
840 char *
841 ao_scheme_args_name(uint8_t args);
842
843 /* read */
844 extern int                      ao_scheme_read_list;
845 extern struct ao_scheme_cons    *ao_scheme_read_cons;
846 extern struct ao_scheme_cons    *ao_scheme_read_cons_tail;
847 extern struct ao_scheme_cons    *ao_scheme_read_stack;
848
849 ao_poly
850 ao_scheme_read(void);
851
852 /* rep */
853 ao_poly
854 ao_scheme_read_eval_print(void);
855
856 /* frame */
857 extern const struct ao_scheme_type ao_scheme_frame_type;
858 extern const struct ao_scheme_type ao_scheme_frame_vals_type;
859
860 #define AO_SCHEME_FRAME_FREE    6
861
862 extern struct ao_scheme_frame   *ao_scheme_frame_free_list[AO_SCHEME_FRAME_FREE];
863
864 ao_poly
865 ao_scheme_frame_mark(struct ao_scheme_frame *frame);
866
867 ao_poly *
868 ao_scheme_frame_ref(struct ao_scheme_frame *frame, ao_poly atom);
869
870 struct ao_scheme_frame *
871 ao_scheme_frame_new(int num);
872
873 void
874 ao_scheme_frame_free(struct ao_scheme_frame *frame);
875
876 void
877 ao_scheme_frame_bind(struct ao_scheme_frame *frame, int num, ao_poly atom, ao_poly val);
878
879 ao_poly
880 ao_scheme_frame_add(struct ao_scheme_frame *frame, ao_poly atom, ao_poly val);
881
882 void
883 ao_scheme_frame_write(ao_poly p, bool write);
884
885 void
886 ao_scheme_frame_init(void);
887
888 /* lambda */
889 extern const struct ao_scheme_type ao_scheme_lambda_type;
890
891 extern const char * const ao_scheme_state_names[];
892
893 struct ao_scheme_lambda *
894 ao_scheme_lambda_new(ao_poly cons);
895
896 void
897 ao_scheme_lambda_write(ao_poly lambda, bool write);
898
899 ao_poly
900 ao_scheme_lambda_eval(void);
901
902 /* stack */
903
904 extern const struct ao_scheme_type ao_scheme_stack_type;
905 extern struct ao_scheme_stack   *ao_scheme_stack;
906 extern struct ao_scheme_stack   *ao_scheme_stack_free_list;
907
908 extern int                      ao_scheme_frame_print_indent;
909
910 void
911 ao_scheme_stack_reset(struct ao_scheme_stack *stack);
912
913 int
914 ao_scheme_stack_push(void);
915
916 void
917 ao_scheme_stack_pop(void);
918
919 void
920 ao_scheme_stack_clear(void);
921
922 void
923 ao_scheme_stack_write(ao_poly stack, bool write);
924
925 ao_poly
926 ao_scheme_stack_eval(void);
927
928 /* error */
929
930 void
931 ao_scheme_vprintf(const char *format, va_list args);
932
933 void
934 ao_scheme_printf(const char *format, ...);
935
936 ao_poly
937 ao_scheme_error(int error, const char *format, ...);
938
939 /* builtins */
940
941 #define AO_SCHEME_BUILTIN_DECLS
942 #include "ao_scheme_builtin.h"
943
944 /* debugging macros */
945
946 #if DBG_EVAL || DBG_READ
947 int ao_scheme_stack_depth;
948 #endif
949
950 #if DBG_EVAL
951 #define DBG_DO(a)       a
952 #define DBG_INDENT()    do { int _s; for(_s = 0; _s < ao_scheme_stack_depth; _s++) printf("  "); } while(0)
953 #define DBG_IN()        (++ao_scheme_stack_depth)
954 #define DBG_OUT()       (--ao_scheme_stack_depth)
955 #define DBG_RESET()     (ao_scheme_stack_depth = 0)
956 #define DBG(...)        ao_scheme_printf(__VA_ARGS__)
957 #define DBGI(...)       do { printf("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
958 #define DBG_CONS(a)     ao_scheme_cons_write(ao_scheme_cons_poly(a), true)
959 #define DBG_POLY(a)     ao_scheme_poly_write(a, true)
960 #define OFFSET(a)       ((a) ? (int) ((uint8_t *) a - ao_scheme_pool) : -1)
961 #define DBG_STACK()     ao_scheme_stack_write(ao_scheme_stack_poly(ao_scheme_stack), true)
962 static inline void
963 ao_scheme_frames_dump(void)
964 {
965         struct ao_scheme_stack *s;
966         DBGI(".. current frame: "); DBG_POLY(ao_scheme_frame_poly(ao_scheme_frame_current)); DBG("\n");
967         for (s = ao_scheme_stack; s; s = ao_scheme_poly_stack(s->prev)) {
968                 DBGI(".. stack frame: "); DBG_POLY(s->frame); DBG("\n");
969         }
970 }
971 #define DBG_FRAMES()    ao_scheme_frames_dump()
972 #else
973 #define DBG_DO(a)
974 #define DBG_INDENT()
975 #define DBG_IN()
976 #define DBG_OUT()
977 #define DBG(...)
978 #define DBGI(...)
979 #define DBG_CONS(a)
980 #define DBG_POLY(a)
981 #define DBG_RESET()
982 #define DBG_STACK()
983 #define DBG_FRAMES()
984 #endif
985
986 #if DBG_READ
987 #define RDBGI(...)      do { printf("%4d: ", __LINE__); DBG_INDENT(); ao_scheme_printf(__VA_ARGS__); } while (0)
988 #define RDBG_IN()       (++ao_scheme_stack_depth)
989 #define RDBG_OUT()      (--ao_scheme_stack_depth)
990 #else
991 #define RDBGI(...)
992 #define RDBG_IN()
993 #define RDBG_OUT()
994 #endif
995
996 static inline int
997 ao_scheme_mdbg_offset(void *a)
998 {
999         uint8_t         *u = a;
1000
1001         if (u == 0)
1002                 return -1;
1003
1004         if (ao_scheme_pool <= u && u < ao_scheme_pool + AO_SCHEME_POOL)
1005                 return u - ao_scheme_pool;
1006
1007 #ifndef AO_SCHEME_MAKE_CONST
1008         if (ao_scheme_const <= u && u < ao_scheme_const + AO_SCHEME_POOL_CONST)
1009                 return - (int) (u - ao_scheme_const);
1010 #endif
1011         return -2;
1012 }
1013
1014 #define MDBG_OFFSET(a)  ao_scheme_mdbg_offset(a)
1015
1016 #if DBG_MEM
1017
1018 #define DBG_MEM_START   1
1019
1020 #include <assert.h>
1021 extern int dbg_move_depth;
1022 #define MDBG_DUMP 1
1023
1024 extern int dbg_mem;
1025
1026 #define MDBG_DO(a)      a
1027 #define MDBG_MOVE(...) do { if (dbg_mem) { int d; for (d = 0; d < dbg_move_depth; d++) printf ("  "); printf(__VA_ARGS__); } } while (0)
1028 #define MDBG_MORE(...) do { if (dbg_mem) printf(__VA_ARGS__); } while (0)
1029 #define MDBG_MOVE_IN()  (dbg_move_depth++)
1030 #define MDBG_MOVE_OUT() (assert(--dbg_move_depth >= 0))
1031
1032 #else
1033
1034 #define MDBG_DO(a)
1035 #define MDBG_MOVE(...)
1036 #define MDBG_MORE(...)
1037 #define MDBG_MOVE_IN()
1038 #define MDBG_MOVE_OUT()
1039
1040 #endif
1041
1042 #endif /* _AO_SCHEME_H_ */