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