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