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