7cd8b5a5959ca29f3bcea8aeda154ba789325715
[fw/altos] / src / lisp / ao_lisp.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_LISP_H_
16 #define _AO_LISP_H_
17
18 #define DBG_MEM         0
19 #define DBG_EVAL        0
20
21 #include <stdint.h>
22 #include <string.h>
23 #include <ao_lisp_os.h>
24 #ifndef __BYTE_ORDER
25 #include <endian.h>
26 #endif
27
28 typedef uint16_t        ao_poly;
29 typedef int16_t         ao_signed_poly;
30
31 #ifdef AO_LISP_SAVE
32
33 struct ao_lisp_os_save {
34         ao_poly         atoms;
35         ao_poly         globals;
36         uint16_t        const_checksum;
37         uint16_t        const_checksum_inv;
38 };
39
40 #define AO_LISP_POOL_EXTRA      (sizeof(struct ao_lisp_os_save))
41 #define AO_LISP_POOL    ((int) (AO_LISP_POOL_TOTAL - AO_LISP_POOL_EXTRA))
42
43 int
44 ao_lisp_os_save(void);
45
46 int
47 ao_lisp_os_restore_save(struct ao_lisp_os_save *save, int offset);
48
49 int
50 ao_lisp_os_restore(void);
51
52 #endif
53
54 #ifdef AO_LISP_MAKE_CONST
55 #define AO_LISP_POOL_CONST      16384
56 extern uint8_t ao_lisp_const[AO_LISP_POOL_CONST] __attribute__((aligned(4)));
57 #define ao_lisp_pool ao_lisp_const
58 #define AO_LISP_POOL AO_LISP_POOL_CONST
59
60 #define _atom(n) ao_lisp_atom_poly(ao_lisp_atom_intern(n))
61 #define _bool(v) ao_lisp_bool_poly(ao_lisp_bool_get(v))
62
63 #define _ao_lisp_bool_true      _bool(1)
64 #define _ao_lisp_bool_false     _bool(0)
65
66 #define _ao_lisp_atom_eof       _atom("eof")
67 #define _ao_lisp_atom_else      _atom("else")
68
69 #define AO_LISP_BUILTIN_ATOMS
70 #include "ao_lisp_builtin.h"
71
72 #else
73 #include "ao_lisp_const.h"
74 #ifndef AO_LISP_POOL
75 #define AO_LISP_POOL    3072
76 #endif
77 extern uint8_t          ao_lisp_pool[AO_LISP_POOL + AO_LISP_POOL_EXTRA] __attribute__((aligned(4)));
78 #endif
79
80 /* Primitive types */
81 #define AO_LISP_CONS            0
82 #define AO_LISP_INT             1
83 #define AO_LISP_STRING          2
84 #define AO_LISP_OTHER           3
85
86 #define AO_LISP_TYPE_MASK       0x0003
87 #define AO_LISP_TYPE_SHIFT      2
88 #define AO_LISP_REF_MASK        0x7ffc
89 #define AO_LISP_CONST           0x8000
90
91 /* These have a type value at the start of the struct */
92 #define AO_LISP_ATOM            4
93 #define AO_LISP_BUILTIN         5
94 #define AO_LISP_FRAME           6
95 #define AO_LISP_FRAME_VALS      7
96 #define AO_LISP_LAMBDA          8
97 #define AO_LISP_STACK           9
98 #define AO_LISP_BOOL            10
99 #define AO_LISP_BIGINT          11
100 #define AO_LISP_FLOAT           12
101 #define AO_LISP_NUM_TYPE        13
102
103 /* Leave two bits for types to use as they please */
104 #define AO_LISP_OTHER_TYPE_MASK 0x3f
105
106 #define AO_LISP_NIL     0
107
108 extern uint16_t         ao_lisp_top;
109
110 #define AO_LISP_OOM             0x01
111 #define AO_LISP_DIVIDE_BY_ZERO  0x02
112 #define AO_LISP_INVALID         0x04
113 #define AO_LISP_UNDEFINED       0x08
114 #define AO_LISP_REDEFINED       0x10
115 #define AO_LISP_EOF             0x20
116 #define AO_LISP_EXIT            0x40
117
118 extern uint8_t          ao_lisp_exception;
119
120 static inline int
121 ao_lisp_is_const(ao_poly poly) {
122         return poly & AO_LISP_CONST;
123 }
124
125 #define AO_LISP_IS_CONST(a)     (ao_lisp_const <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_const + AO_LISP_POOL_CONST)
126 #define AO_LISP_IS_POOL(a)      (ao_lisp_pool <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_pool + AO_LISP_POOL)
127 #define AO_LISP_IS_INT(p)       (ao_lisp_poly_base_type(p) == AO_LISP_INT)
128
129 void *
130 ao_lisp_ref(ao_poly poly);
131
132 ao_poly
133 ao_lisp_poly(const void *addr, ao_poly type);
134
135 struct ao_lisp_type {
136         int     (*size)(void *addr);
137         void    (*mark)(void *addr);
138         void    (*move)(void *addr);
139         char    name[];
140 };
141
142 struct ao_lisp_cons {
143         ao_poly         car;
144         ao_poly         cdr;
145 };
146
147 struct ao_lisp_atom {
148         uint8_t         type;
149         uint8_t         pad[1];
150         ao_poly         next;
151         char            name[];
152 };
153
154 struct ao_lisp_val {
155         ao_poly         atom;
156         ao_poly         val;
157 };
158
159 struct ao_lisp_frame_vals {
160         uint8_t                 type;
161         uint8_t                 size;
162         struct ao_lisp_val      vals[];
163 };
164
165 struct ao_lisp_frame {
166         uint8_t                 type;
167         uint8_t                 num;
168         ao_poly                 prev;
169         ao_poly                 vals;
170 };
171
172 struct ao_lisp_bool {
173         uint8_t                 type;
174         uint8_t                 value;
175         uint16_t                pad;
176 };
177
178 struct ao_lisp_bigint {
179         uint32_t                value;
180 };
181
182 struct ao_lisp_float {
183         uint8_t                 type;
184         uint8_t                 pad1;
185         uint16_t                pad2;
186         float                   value;
187 };
188
189 #if __BYTE_ORDER == __LITTLE_ENDIAN
190 static inline uint32_t
191 ao_lisp_int_bigint(int32_t i) {
192         return AO_LISP_BIGINT | (i << 8);
193 }
194 static inline int32_t
195 ao_lisp_bigint_int(uint32_t bi) {
196         return (int32_t) bi >> 8;
197 }
198 #else
199 static inline uint32_t
200 ao_lisp_int_bigint(int32_t i) {
201         return (uint32_t) (i & 0xffffff) | (AO_LISP_BIGINT << 24);
202 }
203 static inlint int32_t
204 ao_lisp_bigint_int(uint32_t bi) {
205         return (int32_t) (bi << 8) >> 8;
206 }
207 #endif
208
209 #define AO_LISP_MIN_INT         (-(1 << (15 - AO_LISP_TYPE_SHIFT)))
210 #define AO_LISP_MAX_INT         ((1 << (15 - AO_LISP_TYPE_SHIFT)) - 1)
211 #define AO_LISP_MIN_BIGINT      (-(1 << 24))
212 #define AO_LISP_MAX_BIGINT      ((1 << 24) - 1)
213
214 #define AO_LISP_NOT_INTEGER     0x7fffffff
215
216 /* Set on type when the frame escapes the lambda */
217 #define AO_LISP_FRAME_MARK      0x80
218 #define AO_LISP_FRAME_PRINT     0x40
219
220 static inline int ao_lisp_frame_marked(struct ao_lisp_frame *f) {
221         return f->type & AO_LISP_FRAME_MARK;
222 }
223
224 static inline struct ao_lisp_frame *
225 ao_lisp_poly_frame(ao_poly poly) {
226         return ao_lisp_ref(poly);
227 }
228
229 static inline ao_poly
230 ao_lisp_frame_poly(struct ao_lisp_frame *frame) {
231         return ao_lisp_poly(frame, AO_LISP_OTHER);
232 }
233
234 static inline struct ao_lisp_frame_vals *
235 ao_lisp_poly_frame_vals(ao_poly poly) {
236         return ao_lisp_ref(poly);
237 }
238
239 static inline ao_poly
240 ao_lisp_frame_vals_poly(struct ao_lisp_frame_vals *vals) {
241         return ao_lisp_poly(vals, AO_LISP_OTHER);
242 }
243
244 enum eval_state {
245         eval_sexpr,             /* Evaluate an sexpr */
246         eval_val,               /* Value computed */
247         eval_formal,            /* Formal computed */
248         eval_exec,              /* Start a lambda evaluation */
249         eval_apply,             /* Execute apply */
250         eval_cond,              /* Start next cond clause */
251         eval_cond_test,         /* Check cond condition */
252         eval_begin,             /* Start next begin entry */
253         eval_while,             /* Start while condition */
254         eval_while_test,        /* Check while condition */
255         eval_macro,             /* Finished with macro generation */
256 };
257
258 struct ao_lisp_stack {
259         uint8_t                 type;           /* AO_LISP_STACK */
260         uint8_t                 state;          /* enum eval_state */
261         ao_poly                 prev;           /* previous stack frame */
262         ao_poly                 sexprs;         /* expressions to evaluate */
263         ao_poly                 values;         /* values computed */
264         ao_poly                 values_tail;    /* end of the values list for easy appending */
265         ao_poly                 frame;          /* current lookup frame */
266         ao_poly                 list;           /* most recent function call */
267 };
268
269 #define AO_LISP_STACK_MARK      0x80    /* set on type when a reference has been taken */
270 #define AO_LISP_STACK_PRINT     0x40    /* stack is being printed */
271
272 static inline int ao_lisp_stack_marked(struct ao_lisp_stack *s) {
273         return s->type & AO_LISP_STACK_MARK;
274 }
275
276 static inline void ao_lisp_stack_mark(struct ao_lisp_stack *s) {
277         s->type |= AO_LISP_STACK_MARK;
278 }
279
280 static inline struct ao_lisp_stack *
281 ao_lisp_poly_stack(ao_poly p)
282 {
283         return ao_lisp_ref(p);
284 }
285
286 static inline ao_poly
287 ao_lisp_stack_poly(struct ao_lisp_stack *stack)
288 {
289         return ao_lisp_poly(stack, AO_LISP_OTHER);
290 }
291
292 extern ao_poly                  ao_lisp_v;
293
294 #define AO_LISP_FUNC_LAMBDA     0
295 #define AO_LISP_FUNC_NLAMBDA    1
296 #define AO_LISP_FUNC_MACRO      2
297 #define AO_LISP_FUNC_LEXPR      3
298
299 #define AO_LISP_FUNC_FREE_ARGS  0x80
300 #define AO_LISP_FUNC_MASK       0x7f
301
302 #define AO_LISP_FUNC_F_LAMBDA   (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_LAMBDA)
303 #define AO_LISP_FUNC_F_NLAMBDA  (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_NLAMBDA)
304 #define AO_LISP_FUNC_F_MACRO    (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_MACRO)
305 #define AO_LISP_FUNC_F_LEXPR    (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_LEXPR)
306
307 struct ao_lisp_builtin {
308         uint8_t         type;
309         uint8_t         args;
310         uint16_t        func;
311 };
312
313 #define AO_LISP_BUILTIN_ID
314 #include "ao_lisp_builtin.h"
315
316 typedef ao_poly (*ao_lisp_func_t)(struct ao_lisp_cons *cons);
317
318 extern const ao_lisp_func_t     ao_lisp_builtins[];
319
320 static inline ao_lisp_func_t
321 ao_lisp_func(struct ao_lisp_builtin *b)
322 {
323         return ao_lisp_builtins[b->func];
324 }
325
326 struct ao_lisp_lambda {
327         uint8_t         type;
328         uint8_t         args;
329         ao_poly         code;
330         ao_poly         frame;
331 };
332
333 static inline struct ao_lisp_lambda *
334 ao_lisp_poly_lambda(ao_poly poly)
335 {
336         return ao_lisp_ref(poly);
337 }
338
339 static inline ao_poly
340 ao_lisp_lambda_poly(struct ao_lisp_lambda *lambda)
341 {
342         return ao_lisp_poly(lambda, AO_LISP_OTHER);
343 }
344
345 static inline void *
346 ao_lisp_poly_other(ao_poly poly) {
347         return ao_lisp_ref(poly);
348 }
349
350 static inline uint8_t
351 ao_lisp_other_type(void *other) {
352 #if DBG_MEM
353         if ((*((uint8_t *) other) & AO_LISP_OTHER_TYPE_MASK) >= AO_LISP_NUM_TYPE)
354                 ao_lisp_abort();
355 #endif
356         return *((uint8_t *) other) & AO_LISP_OTHER_TYPE_MASK;
357 }
358
359 static inline ao_poly
360 ao_lisp_other_poly(const void *other)
361 {
362         return ao_lisp_poly(other, AO_LISP_OTHER);
363 }
364
365 static inline int
366 ao_lisp_size_round(int size)
367 {
368         return (size + 3) & ~3;
369 }
370
371 static inline int
372 ao_lisp_size(const struct ao_lisp_type *type, void *addr)
373 {
374         return ao_lisp_size_round(type->size(addr));
375 }
376
377 #define AO_LISP_OTHER_POLY(other) ((ao_poly)(other) + AO_LISP_OTHER)
378
379 static inline int ao_lisp_poly_base_type(ao_poly poly) {
380         return poly & AO_LISP_TYPE_MASK;
381 }
382
383 static inline int ao_lisp_poly_type(ao_poly poly) {
384         int     type = poly & AO_LISP_TYPE_MASK;
385         if (type == AO_LISP_OTHER)
386                 return ao_lisp_other_type(ao_lisp_poly_other(poly));
387         return type;
388 }
389
390 static inline struct ao_lisp_cons *
391 ao_lisp_poly_cons(ao_poly poly)
392 {
393         return ao_lisp_ref(poly);
394 }
395
396 static inline ao_poly
397 ao_lisp_cons_poly(struct ao_lisp_cons *cons)
398 {
399         return ao_lisp_poly(cons, AO_LISP_CONS);
400 }
401
402 static inline int32_t
403 ao_lisp_poly_int(ao_poly poly)
404 {
405         return (int32_t) ((ao_signed_poly) poly >> AO_LISP_TYPE_SHIFT);
406 }
407
408 static inline ao_poly
409 ao_lisp_int_poly(int32_t i)
410 {
411         return ((ao_poly) i << 2) | AO_LISP_INT;
412 }
413
414 static inline struct ao_lisp_bigint *
415 ao_lisp_poly_bigint(ao_poly poly)
416 {
417         return ao_lisp_ref(poly);
418 }
419
420 static inline ao_poly
421 ao_lisp_bigint_poly(struct ao_lisp_bigint *bi)
422 {
423         return ao_lisp_poly(bi, AO_LISP_OTHER);
424 }
425
426 static inline char *
427 ao_lisp_poly_string(ao_poly poly)
428 {
429         return ao_lisp_ref(poly);
430 }
431
432 static inline ao_poly
433 ao_lisp_string_poly(char *s)
434 {
435         return ao_lisp_poly(s, AO_LISP_STRING);
436 }
437
438 static inline struct ao_lisp_atom *
439 ao_lisp_poly_atom(ao_poly poly)
440 {
441         return ao_lisp_ref(poly);
442 }
443
444 static inline ao_poly
445 ao_lisp_atom_poly(struct ao_lisp_atom *a)
446 {
447         return ao_lisp_poly(a, AO_LISP_OTHER);
448 }
449
450 static inline struct ao_lisp_builtin *
451 ao_lisp_poly_builtin(ao_poly poly)
452 {
453         return ao_lisp_ref(poly);
454 }
455
456 static inline ao_poly
457 ao_lisp_builtin_poly(struct ao_lisp_builtin *b)
458 {
459         return ao_lisp_poly(b, AO_LISP_OTHER);
460 }
461
462 static inline ao_poly
463 ao_lisp_bool_poly(struct ao_lisp_bool *b)
464 {
465         return ao_lisp_poly(b, AO_LISP_OTHER);
466 }
467
468 static inline struct ao_lisp_bool *
469 ao_lisp_poly_bool(ao_poly poly)
470 {
471         return ao_lisp_ref(poly);
472 }
473
474 static inline ao_poly
475 ao_lisp_float_poly(struct ao_lisp_float *f)
476 {
477         return ao_lisp_poly(f, AO_LISP_OTHER);
478 }
479
480 static inline struct ao_lisp_float *
481 ao_lisp_poly_float(ao_poly poly)
482 {
483         return ao_lisp_ref(poly);
484 }
485
486 float
487 ao_lisp_poly_number(ao_poly p);
488
489 /* memory functions */
490
491 extern int ao_lisp_collects[2];
492 extern int ao_lisp_freed[2];
493 extern int ao_lisp_loops[2];
494
495 /* returns 1 if the object was already marked */
496 int
497 ao_lisp_mark(const struct ao_lisp_type *type, void *addr);
498
499 /* returns 1 if the object was already marked */
500 int
501 ao_lisp_mark_memory(const struct ao_lisp_type *type, void *addr);
502
503 void *
504 ao_lisp_move_map(void *addr);
505
506 /* returns 1 if the object was already moved */
507 int
508 ao_lisp_move(const struct ao_lisp_type *type, void **ref);
509
510 /* returns 1 if the object was already moved */
511 int
512 ao_lisp_move_memory(const struct ao_lisp_type *type, void **ref);
513
514 void *
515 ao_lisp_alloc(int size);
516
517 #define AO_LISP_COLLECT_FULL            1
518 #define AO_LISP_COLLECT_INCREMENTAL     0
519
520 int
521 ao_lisp_collect(uint8_t style);
522
523 void
524 ao_lisp_cons_stash(int id, struct ao_lisp_cons *cons);
525
526 struct ao_lisp_cons *
527 ao_lisp_cons_fetch(int id);
528
529 void
530 ao_lisp_poly_stash(int id, ao_poly poly);
531
532 ao_poly
533 ao_lisp_poly_fetch(int id);
534
535 void
536 ao_lisp_string_stash(int id, char *string);
537
538 char *
539 ao_lisp_string_fetch(int id);
540
541 static inline void
542 ao_lisp_stack_stash(int id, struct ao_lisp_stack *stack) {
543         ao_lisp_poly_stash(id, ao_lisp_stack_poly(stack));
544 }
545
546 static inline struct ao_lisp_stack *
547 ao_lisp_stack_fetch(int id) {
548         return ao_lisp_poly_stack(ao_lisp_poly_fetch(id));
549 }
550
551 void
552 ao_lisp_frame_stash(int id, struct ao_lisp_frame *frame);
553
554 struct ao_lisp_frame *
555 ao_lisp_frame_fetch(int id);
556
557 /* bool */
558
559 extern const struct ao_lisp_type ao_lisp_bool_type;
560
561 void
562 ao_lisp_bool_write(ao_poly v);
563
564 #ifdef AO_LISP_MAKE_CONST
565 struct ao_lisp_bool     *ao_lisp_true, *ao_lisp_false;
566
567 struct ao_lisp_bool *
568 ao_lisp_bool_get(uint8_t value);
569 #endif
570
571 /* cons */
572 extern const struct ao_lisp_type ao_lisp_cons_type;
573
574 struct ao_lisp_cons *
575 ao_lisp_cons_cons(ao_poly car, ao_poly cdr);
576
577 /* Return a cons or NULL for a proper list, else error */
578 struct ao_lisp_cons *
579 ao_lisp_cons_cdr(struct ao_lisp_cons *cons);
580
581 ao_poly
582 ao_lisp__cons(ao_poly car, ao_poly cdr);
583
584 extern struct ao_lisp_cons *ao_lisp_cons_free_list;
585
586 void
587 ao_lisp_cons_free(struct ao_lisp_cons *cons);
588
589 void
590 ao_lisp_cons_write(ao_poly);
591
592 void
593 ao_lisp_cons_display(ao_poly);
594
595 int
596 ao_lisp_cons_length(struct ao_lisp_cons *cons);
597
598 /* string */
599 extern const struct ao_lisp_type ao_lisp_string_type;
600
601 char *
602 ao_lisp_string_copy(char *a);
603
604 char *
605 ao_lisp_string_cat(char *a, char *b);
606
607 ao_poly
608 ao_lisp_string_pack(struct ao_lisp_cons *cons);
609
610 ao_poly
611 ao_lisp_string_unpack(char *a);
612
613 void
614 ao_lisp_string_write(ao_poly s);
615
616 void
617 ao_lisp_string_display(ao_poly s);
618
619 /* atom */
620 extern const struct ao_lisp_type ao_lisp_atom_type;
621
622 extern struct ao_lisp_atom      *ao_lisp_atoms;
623 extern struct ao_lisp_frame     *ao_lisp_frame_global;
624 extern struct ao_lisp_frame     *ao_lisp_frame_current;
625
626 void
627 ao_lisp_atom_write(ao_poly a);
628
629 struct ao_lisp_atom *
630 ao_lisp_atom_intern(char *name);
631
632 ao_poly *
633 ao_lisp_atom_ref(ao_poly atom);
634
635 ao_poly
636 ao_lisp_atom_get(ao_poly atom);
637
638 ao_poly
639 ao_lisp_atom_set(ao_poly atom, ao_poly val);
640
641 ao_poly
642 ao_lisp_atom_def(ao_poly atom, ao_poly val);
643
644 /* int */
645 void
646 ao_lisp_int_write(ao_poly i);
647
648 int32_t
649 ao_lisp_poly_integer(ao_poly p);
650
651 ao_poly
652 ao_lisp_integer_poly(int32_t i);
653
654 static inline int
655 ao_lisp_integer_typep(uint8_t t)
656 {
657         return (t == AO_LISP_INT) || (t == AO_LISP_BIGINT);
658 }
659
660 void
661 ao_lisp_bigint_write(ao_poly i);
662
663 extern const struct ao_lisp_type        ao_lisp_bigint_type;
664 /* prim */
665 void
666 ao_lisp_poly_write(ao_poly p);
667
668 void
669 ao_lisp_poly_display(ao_poly p);
670
671 int
672 ao_lisp_poly_mark(ao_poly p, uint8_t note_cons);
673
674 /* returns 1 if the object has already been moved */
675 int
676 ao_lisp_poly_move(ao_poly *p, uint8_t note_cons);
677
678 /* eval */
679
680 void
681 ao_lisp_eval_clear_globals(void);
682
683 int
684 ao_lisp_eval_restart(void);
685
686 ao_poly
687 ao_lisp_eval(ao_poly p);
688
689 ao_poly
690 ao_lisp_set_cond(struct ao_lisp_cons *cons);
691
692 /* float */
693 extern const struct ao_lisp_type ao_lisp_float_type;
694
695 void
696 ao_lisp_float_write(ao_poly p);
697
698 ao_poly
699 ao_lisp_float_get(float value);
700
701 static inline uint8_t
702 ao_lisp_number_typep(uint8_t t)
703 {
704         return ao_lisp_integer_typep(t) || (t == AO_LISP_FLOAT);
705 }
706
707 float
708 ao_lisp_poly_number(ao_poly p);
709
710 /* builtin */
711 void
712 ao_lisp_builtin_write(ao_poly b);
713
714 extern const struct ao_lisp_type ao_lisp_builtin_type;
715
716 /* Check argument count */
717 ao_poly
718 ao_lisp_check_argc(ao_poly name, struct ao_lisp_cons *cons, int min, int max);
719
720 /* Check argument type */
721 ao_poly
722 ao_lisp_check_argt(ao_poly name, struct ao_lisp_cons *cons, int argc, int type, int nil_ok);
723
724 /* Fetch an arg (nil if off the end) */
725 ao_poly
726 ao_lisp_arg(struct ao_lisp_cons *cons, int argc);
727
728 char *
729 ao_lisp_args_name(uint8_t args);
730
731 /* read */
732 extern struct ao_lisp_cons      *ao_lisp_read_cons;
733 extern struct ao_lisp_cons      *ao_lisp_read_cons_tail;
734 extern struct ao_lisp_cons      *ao_lisp_read_stack;
735
736 ao_poly
737 ao_lisp_read(void);
738
739 /* rep */
740 ao_poly
741 ao_lisp_read_eval_print(void);
742
743 /* frame */
744 extern const struct ao_lisp_type ao_lisp_frame_type;
745 extern const struct ao_lisp_type ao_lisp_frame_vals_type;
746
747 #define AO_LISP_FRAME_FREE      6
748
749 extern struct ao_lisp_frame     *ao_lisp_frame_free_list[AO_LISP_FRAME_FREE];
750
751 ao_poly
752 ao_lisp_frame_mark(struct ao_lisp_frame *frame);
753
754 ao_poly *
755 ao_lisp_frame_ref(struct ao_lisp_frame *frame, ao_poly atom);
756
757 struct ao_lisp_frame *
758 ao_lisp_frame_new(int num);
759
760 void
761 ao_lisp_frame_free(struct ao_lisp_frame *frame);
762
763 void
764 ao_lisp_frame_bind(struct ao_lisp_frame *frame, int num, ao_poly atom, ao_poly val);
765
766 ao_poly
767 ao_lisp_frame_add(struct ao_lisp_frame *frame, ao_poly atom, ao_poly val);
768
769 void
770 ao_lisp_frame_write(ao_poly p);
771
772 void
773 ao_lisp_frame_init(void);
774
775 /* lambda */
776 extern const struct ao_lisp_type ao_lisp_lambda_type;
777
778 extern const char *ao_lisp_state_names[];
779
780 struct ao_lisp_lambda *
781 ao_lisp_lambda_new(ao_poly cons);
782
783 void
784 ao_lisp_lambda_write(ao_poly lambda);
785
786 ao_poly
787 ao_lisp_lambda_eval(void);
788
789 /* stack */
790
791 extern const struct ao_lisp_type ao_lisp_stack_type;
792 extern struct ao_lisp_stack     *ao_lisp_stack;
793 extern struct ao_lisp_stack     *ao_lisp_stack_free_list;
794
795 void
796 ao_lisp_stack_reset(struct ao_lisp_stack *stack);
797
798 int
799 ao_lisp_stack_push(void);
800
801 void
802 ao_lisp_stack_pop(void);
803
804 void
805 ao_lisp_stack_clear(void);
806
807 void
808 ao_lisp_stack_write(ao_poly stack);
809
810 ao_poly
811 ao_lisp_stack_eval(void);
812
813 /* error */
814
815 void
816 ao_lisp_error_poly(char *name, ao_poly poly, ao_poly last);
817
818 void
819 ao_lisp_error_frame(int indent, char *name, struct ao_lisp_frame *frame);
820
821 ao_poly
822 ao_lisp_error(int error, char *format, ...);
823
824 /* builtins */
825
826 #define AO_LISP_BUILTIN_DECLS
827 #include "ao_lisp_builtin.h"
828
829 /* debugging macros */
830
831 #if DBG_EVAL
832 #define DBG_CODE        1
833 int ao_lisp_stack_depth;
834 #define DBG_DO(a)       a
835 #define DBG_INDENT()    do { int _s; for(_s = 0; _s < ao_lisp_stack_depth; _s++) printf("  "); } while(0)
836 #define DBG_IN()        (++ao_lisp_stack_depth)
837 #define DBG_OUT()       (--ao_lisp_stack_depth)
838 #define DBG_RESET()     (ao_lisp_stack_depth = 0)
839 #define DBG(...)        printf(__VA_ARGS__)
840 #define DBGI(...)       do { DBG("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
841 #define DBG_CONS(a)     ao_lisp_cons_write(ao_lisp_cons_poly(a))
842 #define DBG_POLY(a)     ao_lisp_poly_write(a)
843 #define OFFSET(a)       ((a) ? (int) ((uint8_t *) a - ao_lisp_pool) : -1)
844 #define DBG_STACK()     ao_lisp_stack_write(ao_lisp_stack_poly(ao_lisp_stack))
845 static inline void
846 ao_lisp_frames_dump(void)
847 {
848         struct ao_lisp_stack *s;
849         DBGI(".. current frame: "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
850         for (s = ao_lisp_stack; s; s = ao_lisp_poly_stack(s->prev)) {
851                 DBGI(".. stack frame: "); DBG_POLY(s->frame); DBG("\n");
852         }
853 }
854 #define DBG_FRAMES()    ao_lisp_frames_dump()
855 #else
856 #define DBG_DO(a)
857 #define DBG_INDENT()
858 #define DBG_IN()
859 #define DBG_OUT()
860 #define DBG(...)
861 #define DBGI(...)
862 #define DBG_CONS(a)
863 #define DBG_POLY(a)
864 #define DBG_RESET()
865 #define DBG_STACK()
866 #define DBG_FRAMES()
867 #endif
868
869 #define DBG_MEM_START   1
870
871 #if DBG_MEM
872
873 #include <assert.h>
874 extern int dbg_move_depth;
875 #define MDBG_DUMP 1
876 #define MDBG_OFFSET(a)  ((a) ? (int) ((uint8_t *) (a) - ao_lisp_pool) : -1)
877
878 extern int dbg_mem;
879
880 #define MDBG_DO(a)      a
881 #define MDBG_MOVE(...) do { if (dbg_mem) { int d; for (d = 0; d < dbg_move_depth; d++) printf ("  "); printf(__VA_ARGS__); } } while (0)
882 #define MDBG_MORE(...) do { if (dbg_mem) printf(__VA_ARGS__); } while (0)
883 #define MDBG_MOVE_IN()  (dbg_move_depth++)
884 #define MDBG_MOVE_OUT() (assert(--dbg_move_depth >= 0))
885
886 #else
887
888 #define MDBG_DO(a)
889 #define MDBG_MOVE(...)
890 #define MDBG_MORE(...)
891 #define MDBG_MOVE_IN()
892 #define MDBG_MOVE_OUT()
893
894 #endif
895
896 #endif /* _AO_LISP_H_ */