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