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