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