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