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