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