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