altos/scheme: Use memory manager mark code to note recursive print
[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 #define DBG_MEM         0
19 #define DBG_EVAL        0
20 #define DBG_READ        0
21 #define DBG_FREE_CONS   0
22 #define NDEBUG          1
23
24 #include <stdint.h>
25 #include <string.h>
26 #include <stdbool.h>
27 #define AO_SCHEME_BUILTIN_FEATURES
28 #include "ao_scheme_builtin.h"
29 #undef AO_SCHEME_BUILTIN_FEATURES
30 #include <ao_scheme_os.h>
31 #ifndef __BYTE_ORDER
32 #include <endian.h>
33 #endif
34
35 typedef uint16_t        ao_poly;
36 typedef int16_t         ao_signed_poly;
37
38 #if AO_SCHEME_SAVE
39
40 struct ao_scheme_os_save {
41         ao_poly         atoms;
42         ao_poly         globals;
43         uint16_t        const_checksum;
44         uint16_t        const_checksum_inv;
45 };
46
47 #ifndef AO_SCHEME_POOL_TOTAL
48 #error Must define AO_SCHEME_POOL_TOTAL for AO_SCHEME_SAVE
49 #endif
50
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))
53
54 int
55 ao_scheme_os_save(void);
56
57 int
58 ao_scheme_os_restore_save(struct ao_scheme_os_save *save, int offset);
59
60 int
61 ao_scheme_os_restore(void);
62
63 #endif
64
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
70
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))
73
74 #define _ao_scheme_bool_true    _bool(1)
75 #define _ao_scheme_bool_false   _bool(0)
76
77 #define _ao_scheme_atom_eof     _atom("eof")
78 #define _ao_scheme_atom_else    _atom("else")
79
80 #define AO_SCHEME_BUILTIN_ATOMS
81 #include "ao_scheme_builtin.h"
82
83 #else
84 #include "ao_scheme_const.h"
85 #ifndef AO_SCHEME_POOL
86 #error Must define AO_SCHEME_POOL
87 #endif
88 #ifndef AO_SCHEME_POOL_EXTRA
89 #define AO_SCHEME_POOL_EXTRA 0
90 #endif
91 extern uint8_t          ao_scheme_pool[AO_SCHEME_POOL + AO_SCHEME_POOL_EXTRA] __attribute__((aligned(4)));
92 #endif
93
94 /* Primitive types */
95 #define AO_SCHEME_CONS          0
96 #define AO_SCHEME_INT           1
97 #define AO_SCHEME_BIGINT        2
98 #define AO_SCHEME_OTHER         3
99
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
104
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
117 #else
118 #define _AO_SCHEME_FLOAT        12
119 #endif
120 #ifdef AO_SCHEME_FEATURE_VECTOR
121 #define AO_SCHEME_VECTOR        13
122 #define _AO_SCHEME_VECTOR       AO_SCHEME_VECTOR
123 #else
124 #define _AO_SCHEME_VECTOR       _AO_SCHEME_FLOAT
125 #endif
126 #define AO_SCHEME_NUM_TYPE      (_AO_SCHEME_VECTOR+1)
127
128 /* Leave two bits for types to use as they please */
129 #define AO_SCHEME_OTHER_TYPE_MASK       0x3f
130
131 #define AO_SCHEME_NIL   0
132
133 extern uint16_t         ao_scheme_top;
134
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
142
143 extern uint8_t          ao_scheme_exception;
144
145 static inline int
146 ao_scheme_is_const(ao_poly poly) {
147         return poly & AO_SCHEME_CONST;
148 }
149
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)
154
155 void *
156 ao_scheme_ref(ao_poly poly);
157
158 ao_poly
159 ao_scheme_poly(const void *addr, ao_poly type);
160
161 struct ao_scheme_type {
162         int     (*size)(void *addr);
163         void    (*mark)(void *addr);
164         void    (*move)(void *addr);
165         char    name[];
166 };
167
168 struct ao_scheme_cons {
169         ao_poly         car;
170         ao_poly         cdr;
171 };
172
173 struct ao_scheme_atom {
174         uint8_t         type;
175         uint8_t         pad[1];
176         ao_poly         next;
177         char            name[];
178 };
179
180 struct ao_scheme_string {
181         uint8_t         type;
182         char            val[];
183 };
184
185 struct ao_scheme_val {
186         ao_poly         atom;
187         ao_poly         val;
188 };
189
190 struct ao_scheme_frame_vals {
191         uint8_t                 type;
192         uint8_t                 size;
193         struct ao_scheme_val    vals[];
194 };
195
196 struct ao_scheme_frame {
197         uint8_t                 type;
198         uint8_t                 num;
199         ao_poly                 prev;
200         ao_poly                 vals;
201 };
202
203 struct ao_scheme_bool {
204         uint8_t                 type;
205         uint8_t                 value;
206         uint16_t                pad;
207 };
208
209
210 #ifdef AO_SCHEME_FEATURE_FLOAT
211 struct ao_scheme_float {
212         uint8_t                 type;
213         uint8_t                 pad1;
214         uint16_t                pad2;
215         float                   value;
216 };
217 #endif
218
219 #ifdef AO_SCHEME_FEATURE_VECTOR
220 struct ao_scheme_vector {
221         uint8_t                 type;
222         uint8_t                 pad1;
223         uint16_t                length;
224         ao_poly                 vals[];
225 };
226 #endif
227
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)
230
231 #ifdef AO_SCHEME_FEATURE_BIGINT
232
233 struct ao_scheme_bigint {
234         uint32_t                value;
235 };
236
237 #define AO_SCHEME_MIN_BIGINT    INT32_MIN
238 #define AO_SCHEME_MAX_BIGINT    INT32_MAX
239
240 #endif  /* AO_SCHEME_FEATURE_BIGINT */
241
242 /* Set on type when the frame escapes the lambda */
243 #define AO_SCHEME_FRAME_MARK    0x80
244 #define AO_SCHEME_FRAME_PRINT   0x40
245
246 static inline int ao_scheme_frame_marked(struct ao_scheme_frame *f) {
247         return f->type & AO_SCHEME_FRAME_MARK;
248 }
249
250 static inline struct ao_scheme_frame *
251 ao_scheme_poly_frame(ao_poly poly) {
252         return ao_scheme_ref(poly);
253 }
254
255 static inline ao_poly
256 ao_scheme_frame_poly(struct ao_scheme_frame *frame) {
257         return ao_scheme_poly(frame, AO_SCHEME_OTHER);
258 }
259
260 static inline struct ao_scheme_frame_vals *
261 ao_scheme_poly_frame_vals(ao_poly poly) {
262         return ao_scheme_ref(poly);
263 }
264
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);
268 }
269
270 enum eval_state {
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 */
282 };
283
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 */
293 };
294
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 */
297
298 static inline int ao_scheme_stack_marked(struct ao_scheme_stack *s) {
299         return s->type & AO_SCHEME_STACK_MARK;
300 }
301
302 static inline void ao_scheme_stack_mark(struct ao_scheme_stack *s) {
303         s->type |= AO_SCHEME_STACK_MARK;
304 }
305
306 static inline struct ao_scheme_stack *
307 ao_scheme_poly_stack(ao_poly p)
308 {
309         return ao_scheme_ref(p);
310 }
311
312 static inline ao_poly
313 ao_scheme_stack_poly(struct ao_scheme_stack *stack)
314 {
315         return ao_scheme_poly(stack, AO_SCHEME_OTHER);
316 }
317
318 extern ao_poly                  ao_scheme_v;
319
320 #define AO_SCHEME_FUNC_LAMBDA           0
321 #define AO_SCHEME_FUNC_NLAMBDA          1
322 #define AO_SCHEME_FUNC_MACRO            2
323
324 #define AO_SCHEME_FUNC_FREE_ARGS        0x80
325 #define AO_SCHEME_FUNC_MASK             0x7f
326
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)
330
331 struct ao_scheme_builtin {
332         uint8_t         type;
333         uint8_t         args;
334         uint16_t        func;
335 };
336
337 #define AO_SCHEME_BUILTIN_ID
338 #include "ao_scheme_builtin.h"
339
340 typedef ao_poly (*ao_scheme_func_t)(struct ao_scheme_cons *cons);
341
342 extern const ao_scheme_func_t   ao_scheme_builtins[];
343
344 static inline ao_scheme_func_t
345 ao_scheme_func(struct ao_scheme_builtin *b)
346 {
347         return ao_scheme_builtins[b->func];
348 }
349
350 struct ao_scheme_lambda {
351         uint8_t         type;
352         uint8_t         args;
353         ao_poly         code;
354         ao_poly         frame;
355 };
356
357 static inline struct ao_scheme_lambda *
358 ao_scheme_poly_lambda(ao_poly poly)
359 {
360         return ao_scheme_ref(poly);
361 }
362
363 static inline ao_poly
364 ao_scheme_lambda_poly(struct ao_scheme_lambda *lambda)
365 {
366         return ao_scheme_poly(lambda, AO_SCHEME_OTHER);
367 }
368
369 static inline void *
370 ao_scheme_poly_other(ao_poly poly) {
371         return ao_scheme_ref(poly);
372 }
373
374 static inline uint8_t
375 ao_scheme_other_type(void *other) {
376 #if DBG_MEM
377         if ((*((uint8_t *) other) & AO_SCHEME_OTHER_TYPE_MASK) >= AO_SCHEME_NUM_TYPE)
378                 ao_scheme_abort();
379 #endif
380         return *((uint8_t *) other) & AO_SCHEME_OTHER_TYPE_MASK;
381 }
382
383 static inline ao_poly
384 ao_scheme_other_poly(const void *other)
385 {
386         return ao_scheme_poly(other, AO_SCHEME_OTHER);
387 }
388
389 static inline int
390 ao_scheme_size_round(int size)
391 {
392         return (size + 3) & ~3;
393 }
394
395 static inline int
396 ao_scheme_size(const struct ao_scheme_type *type, void *addr)
397 {
398         return ao_scheme_size_round(type->size(addr));
399 }
400
401 #define AO_SCHEME_OTHER_POLY(other) ((ao_poly)(other) + AO_SCHEME_OTHER)
402
403 static inline int ao_scheme_poly_base_type(ao_poly poly) {
404         return poly & AO_SCHEME_TYPE_MASK;
405 }
406
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));
411         return type;
412 }
413
414 static inline int
415 ao_scheme_is_cons(ao_poly poly) {
416         return (ao_scheme_poly_base_type(poly) == AO_SCHEME_CONS);
417 }
418
419 static inline int
420 ao_scheme_is_pair(ao_poly poly) {
421         return poly != AO_SCHEME_NIL && (ao_scheme_poly_base_type(poly) == AO_SCHEME_CONS);
422 }
423
424 static inline struct ao_scheme_cons *
425 ao_scheme_poly_cons(ao_poly poly)
426 {
427         return ao_scheme_ref(poly);
428 }
429
430 static inline ao_poly
431 ao_scheme_cons_poly(struct ao_scheme_cons *cons)
432 {
433         return ao_scheme_poly(cons, AO_SCHEME_CONS);
434 }
435
436 static inline int32_t
437 ao_scheme_poly_int(ao_poly poly)
438 {
439         return (int32_t) ((ao_signed_poly) poly >> AO_SCHEME_TYPE_SHIFT);
440 }
441
442 static inline ao_poly
443 ao_scheme_int_poly(int32_t i)
444 {
445         return ((ao_poly) i << 2) | AO_SCHEME_INT;
446 }
447
448 #ifdef AO_SCHEME_FEATURE_BIGINT
449 static inline struct ao_scheme_bigint *
450 ao_scheme_poly_bigint(ao_poly poly)
451 {
452         return ao_scheme_ref(poly);
453 }
454
455 static inline ao_poly
456 ao_scheme_bigint_poly(struct ao_scheme_bigint *bi)
457 {
458         return ao_scheme_poly(bi, AO_SCHEME_BIGINT);
459 }
460 #endif /* AO_SCHEME_FEATURE_BIGINT */
461
462 static inline struct ao_scheme_string *
463 ao_scheme_poly_string(ao_poly poly)
464 {
465         return ao_scheme_ref(poly);
466 }
467
468 static inline ao_poly
469 ao_scheme_string_poly(struct ao_scheme_string *s)
470 {
471         return ao_scheme_poly(s, AO_SCHEME_OTHER);
472 }
473
474 static inline struct ao_scheme_atom *
475 ao_scheme_poly_atom(ao_poly poly)
476 {
477         return ao_scheme_ref(poly);
478 }
479
480 static inline ao_poly
481 ao_scheme_atom_poly(struct ao_scheme_atom *a)
482 {
483         return ao_scheme_poly(a, AO_SCHEME_OTHER);
484 }
485
486 static inline struct ao_scheme_builtin *
487 ao_scheme_poly_builtin(ao_poly poly)
488 {
489         return ao_scheme_ref(poly);
490 }
491
492 static inline ao_poly
493 ao_scheme_builtin_poly(struct ao_scheme_builtin *b)
494 {
495         return ao_scheme_poly(b, AO_SCHEME_OTHER);
496 }
497
498 static inline ao_poly
499 ao_scheme_bool_poly(struct ao_scheme_bool *b)
500 {
501         return ao_scheme_poly(b, AO_SCHEME_OTHER);
502 }
503
504 static inline struct ao_scheme_bool *
505 ao_scheme_poly_bool(ao_poly poly)
506 {
507         return ao_scheme_ref(poly);
508 }
509
510 #ifdef AO_SCHEME_FEATURE_FLOAT
511 static inline ao_poly
512 ao_scheme_float_poly(struct ao_scheme_float *f)
513 {
514         return ao_scheme_poly(f, AO_SCHEME_OTHER);
515 }
516
517 static inline struct ao_scheme_float *
518 ao_scheme_poly_float(ao_poly poly)
519 {
520         return ao_scheme_ref(poly);
521 }
522
523 float
524 ao_scheme_poly_number(ao_poly p);
525 #endif
526
527 #ifdef AO_SCHEME_FEATURE_VECTOR
528 static inline ao_poly
529 ao_scheme_vector_poly(struct ao_scheme_vector *v)
530 {
531         return ao_scheme_poly(v, AO_SCHEME_OTHER);
532 }
533
534 static inline struct ao_scheme_vector *
535 ao_scheme_poly_vector(ao_poly poly)
536 {
537         return ao_scheme_ref(poly);
538 }
539 #endif
540
541 /* memory functions */
542
543 extern uint64_t ao_scheme_collects[2];
544 extern uint64_t ao_scheme_freed[2];
545 extern uint64_t ao_scheme_loops[2];
546
547 /* returns 1 if the object was already marked */
548 int
549 ao_scheme_mark_memory(const struct ao_scheme_type *type, void *addr);
550
551 /* returns 1 if the object was already moved */
552 int
553 ao_scheme_move_memory(const struct ao_scheme_type *type, void **ref);
554
555 void *
556 ao_scheme_alloc(int size);
557
558 /* Marks an object as being printed, returns 1 if it was already marked */
559 int
560 ao_scheme_print_mark_addr(void *addr);
561
562 int
563 ao_scheme_print_mark_poly(ao_poly poly);
564
565 /* Notes that printing has started */
566 void
567 ao_scheme_print_start(void);
568
569 /* Notes that printing has ended */
570 void
571 ao_scheme_print_stop(void);
572
573 #define AO_SCHEME_COLLECT_FULL          1
574 #define AO_SCHEME_COLLECT_INCREMENTAL   0
575
576 int
577 ao_scheme_collect(uint8_t style);
578
579 #if DBG_FREE_CONS
580 void
581 ao_scheme_cons_check(struct ao_scheme_cons *cons);
582 #endif
583
584 void
585 ao_scheme_cons_stash(int id, struct ao_scheme_cons *cons);
586
587 struct ao_scheme_cons *
588 ao_scheme_cons_fetch(int id);
589
590 void
591 ao_scheme_poly_stash(int id, ao_poly poly);
592
593 ao_poly
594 ao_scheme_poly_fetch(int id);
595
596 void
597 ao_scheme_string_stash(int id, struct ao_scheme_string *string);
598
599 struct ao_scheme_string *
600 ao_scheme_string_fetch(int id);
601
602 static inline void
603 ao_scheme_stack_stash(int id, struct ao_scheme_stack *stack) {
604         ao_scheme_poly_stash(id, ao_scheme_stack_poly(stack));
605 }
606
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));
610 }
611
612 void
613 ao_scheme_frame_stash(int id, struct ao_scheme_frame *frame);
614
615 struct ao_scheme_frame *
616 ao_scheme_frame_fetch(int id);
617
618 /* bool */
619
620 extern const struct ao_scheme_type ao_scheme_bool_type;
621
622 void
623 ao_scheme_bool_write(ao_poly v);
624
625 #ifdef AO_SCHEME_MAKE_CONST
626 extern struct ao_scheme_bool    *ao_scheme_true, *ao_scheme_false;
627
628 struct ao_scheme_bool *
629 ao_scheme_bool_get(uint8_t value);
630 #endif
631
632 /* cons */
633 extern const struct ao_scheme_type ao_scheme_cons_type;
634
635 struct ao_scheme_cons *
636 ao_scheme_cons_cons(ao_poly car, ao_poly cdr);
637
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);
641
642 ao_poly
643 ao_scheme__cons(ao_poly car, ao_poly cdr);
644
645 extern struct ao_scheme_cons *ao_scheme_cons_free_list;
646
647 void
648 ao_scheme_cons_free(struct ao_scheme_cons *cons);
649
650 void
651 ao_scheme_cons_write(ao_poly);
652
653 void
654 ao_scheme_cons_display(ao_poly);
655
656 int
657 ao_scheme_cons_length(struct ao_scheme_cons *cons);
658
659 struct ao_scheme_cons *
660 ao_scheme_cons_copy(struct ao_scheme_cons *cons);
661
662 /* string */
663 extern const struct ao_scheme_type ao_scheme_string_type;
664
665 struct ao_scheme_string *
666 ao_scheme_string_copy(struct ao_scheme_string *a);
667
668 struct ao_scheme_string *
669 ao_scheme_string_make(char *a);
670
671 struct ao_scheme_string *
672 ao_scheme_atom_to_string(struct ao_scheme_atom *a);
673
674 struct ao_scheme_string *
675 ao_scheme_string_cat(struct ao_scheme_string *a, struct ao_scheme_string *b);
676
677 ao_poly
678 ao_scheme_string_pack(struct ao_scheme_cons *cons);
679
680 ao_poly
681 ao_scheme_string_unpack(struct ao_scheme_string *a);
682
683 void
684 ao_scheme_string_write(ao_poly s);
685
686 void
687 ao_scheme_string_display(ao_poly s);
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);
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);
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);
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);
756
757 void
758 ao_scheme_vector_display(ao_poly v);
759
760 struct ao_scheme_vector *
761 ao_scheme_vector_alloc(uint16_t length, ao_poly fill);
762
763 ao_poly
764 ao_scheme_vector_get(ao_poly v, ao_poly i);
765
766 ao_poly
767 ao_scheme_vector_set(ao_poly v, ao_poly i, ao_poly p);
768
769 struct ao_scheme_vector *
770 ao_scheme_list_to_vector(struct ao_scheme_cons *cons);
771
772 struct ao_scheme_cons *
773 ao_scheme_vector_to_list(struct ao_scheme_vector *vector);
774
775 extern const struct ao_scheme_type      ao_scheme_vector_type;
776
777 /* prim */
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);
780
781 static inline void
782 ao_scheme_poly_write(ao_poly p) { (*ao_scheme_poly_write_func(p))(p); }
783
784 static inline void
785 ao_scheme_poly_display(ao_poly p) { (*ao_scheme_poly_display_func(p))(p); }
786
787 int
788 ao_scheme_poly_mark(ao_poly p, uint8_t note_cons);
789
790 /* returns 1 if the object has already been moved */
791 int
792 ao_scheme_poly_move(ao_poly *p, uint8_t note_cons);
793
794 /* eval */
795
796 void
797 ao_scheme_eval_clear_globals(void);
798
799 int
800 ao_scheme_eval_restart(void);
801
802 ao_poly
803 ao_scheme_eval(ao_poly p);
804
805 ao_poly
806 ao_scheme_set_cond(struct ao_scheme_cons *cons);
807
808 /* float */
809 #ifdef AO_SCHEME_FEATURE_FLOAT
810 extern const struct ao_scheme_type ao_scheme_float_type;
811
812 void
813 ao_scheme_float_write(ao_poly p);
814
815 ao_poly
816 ao_scheme_float_get(float value);
817 #endif
818
819 #ifdef AO_SCHEME_FEATURE_FLOAT
820 static inline uint8_t
821 ao_scheme_number_typep(uint8_t t)
822 {
823         return ao_scheme_integer_typep(t) || (t == AO_SCHEME_FLOAT);
824 }
825 #else
826 #define ao_scheme_number_typep ao_scheme_integer_typep
827 #endif
828
829 /* builtin */
830 void
831 ao_scheme_builtin_write(ao_poly b);
832
833 extern const struct ao_scheme_type ao_scheme_builtin_type;
834
835 /* Check argument count */
836 ao_poly
837 ao_scheme_check_argc(ao_poly name, struct ao_scheme_cons *cons, int min, int max);
838
839 /* Check argument type */
840 ao_poly
841 ao_scheme_check_argt(ao_poly name, struct ao_scheme_cons *cons, int argc, int type, int nil_ok);
842
843 /* Fetch an arg (nil if off the end) */
844 ao_poly
845 ao_scheme_arg(struct ao_scheme_cons *cons, int argc);
846
847 char *
848 ao_scheme_args_name(uint8_t args);
849
850 /* read */
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;
855
856 ao_poly
857 ao_scheme_read(void);
858
859 /* rep */
860 ao_poly
861 ao_scheme_read_eval_print(void);
862
863 /* frame */
864 extern const struct ao_scheme_type ao_scheme_frame_type;
865 extern const struct ao_scheme_type ao_scheme_frame_vals_type;
866
867 #define AO_SCHEME_FRAME_FREE    6
868
869 extern struct ao_scheme_frame   *ao_scheme_frame_free_list[AO_SCHEME_FRAME_FREE];
870
871 ao_poly
872 ao_scheme_frame_mark(struct ao_scheme_frame *frame);
873
874 ao_poly *
875 ao_scheme_frame_ref(struct ao_scheme_frame *frame, ao_poly atom);
876
877 struct ao_scheme_frame *
878 ao_scheme_frame_new(int num);
879
880 void
881 ao_scheme_frame_free(struct ao_scheme_frame *frame);
882
883 void
884 ao_scheme_frame_bind(struct ao_scheme_frame *frame, int num, ao_poly atom, ao_poly val);
885
886 ao_poly
887 ao_scheme_frame_add(struct ao_scheme_frame *frame, ao_poly atom, ao_poly val);
888
889 void
890 ao_scheme_frame_write(ao_poly p);
891
892 void
893 ao_scheme_frame_init(void);
894
895 /* lambda */
896 extern const struct ao_scheme_type ao_scheme_lambda_type;
897
898 extern const char * const ao_scheme_state_names[];
899
900 struct ao_scheme_lambda *
901 ao_scheme_lambda_new(ao_poly cons);
902
903 void
904 ao_scheme_lambda_write(ao_poly lambda);
905
906 ao_poly
907 ao_scheme_lambda_eval(void);
908
909 /* stack */
910
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;
914
915 void
916 ao_scheme_stack_reset(struct ao_scheme_stack *stack);
917
918 int
919 ao_scheme_stack_push(void);
920
921 void
922 ao_scheme_stack_pop(void);
923
924 void
925 ao_scheme_stack_clear(void);
926
927 void
928 ao_scheme_stack_write(ao_poly stack);
929
930 ao_poly
931 ao_scheme_stack_eval(void);
932
933 /* error */
934
935 void
936 ao_scheme_vprintf(const char *format, va_list args);
937
938 void
939 ao_scheme_printf(const char *format, ...);
940
941 void
942 ao_scheme_error_poly(const char *name, ao_poly poly, ao_poly last);
943
944 void
945 ao_scheme_error_frame(int indent, const char *name, struct ao_scheme_frame *frame);
946
947 ao_poly
948 ao_scheme_error(int error, const char *format, ...);
949
950 /* builtins */
951
952 #define AO_SCHEME_BUILTIN_DECLS
953 #include "ao_scheme_builtin.h"
954
955 /* debugging macros */
956
957 #if DBG_EVAL || DBG_READ || DBG_MEM
958 #define DBG_CODE        1
959 int ao_scheme_stack_depth;
960 #define DBG_DO(a)       a
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))
971 static inline void
972 ao_scheme_frames_dump(void)
973 {
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");
978         }
979 }
980 #define DBG_FRAMES()    ao_scheme_frames_dump()
981 #else
982 #define DBG_DO(a)
983 #define DBG_INDENT()
984 #define DBG_IN()
985 #define DBG_OUT()
986 #define DBG(...)
987 #define DBGI(...)
988 #define DBG_CONS(a)
989 #define DBG_POLY(a)
990 #define DBG_RESET()
991 #define DBG_STACK()
992 #define DBG_FRAMES()
993 #endif
994
995 #if DBG_READ
996 #define RDBGI(...)      DBGI(__VA_ARGS__)
997 #define RDBG_IN()       DBG_IN()
998 #define RDBG_OUT()      DBG_OUT()
999 #else
1000 #define RDBGI(...)
1001 #define RDBG_IN()
1002 #define RDBG_OUT()
1003 #endif
1004
1005 #define DBG_MEM_START   1
1006
1007 #if DBG_MEM
1008
1009 #include <assert.h>
1010 extern int dbg_move_depth;
1011 #define MDBG_DUMP 1
1012 #define MDBG_OFFSET(a)  ((a) ? (int) ((uint8_t *) (a) - ao_scheme_pool) : -1)
1013
1014 extern int dbg_mem;
1015
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))
1021
1022 #else
1023
1024 #define MDBG_DO(a)
1025 #define MDBG_MOVE(...)
1026 #define MDBG_MORE(...)
1027 #define MDBG_MOVE_IN()
1028 #define MDBG_MOVE_OUT()
1029
1030 #endif
1031
1032 #endif /* _AO_SCHEME_H_ */