altos/lisp: add progn, while, read and eval
[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 #include <stdint.h>
19 #include <string.h>
20 //#include <stdio.h>
21 #include <ao_lisp_os.h>
22
23 #ifdef AO_LISP_MAKE_CONST
24 #define AO_LISP_POOL_CONST      16384
25 extern uint8_t ao_lisp_const[AO_LISP_POOL_CONST];
26 #define ao_lisp_pool ao_lisp_const
27 #define AO_LISP_POOL AO_LISP_POOL_CONST
28
29 #define _atom(n) ao_lisp_atom_poly(ao_lisp_atom_intern(n))
30
31 #define _ao_lisp_atom_quote     _atom("quote")
32 #define _ao_lisp_atom_set       _atom("set")
33 #define _ao_lisp_atom_setq      _atom("setq")
34 #define _ao_lisp_atom_t         _atom("t")
35 #define _ao_lisp_atom_car       _atom("car")
36 #define _ao_lisp_atom_cdr       _atom("cdr")
37 #define _ao_lisp_atom_cons      _atom("cons")
38 #define _ao_lisp_atom_last      _atom("last")
39 #define _ao_lisp_atom_cond      _atom("cond")
40 #define _ao_lisp_atom_lambda    _atom("lambda")
41 #define _ao_lisp_atom_led       _atom("led")
42 #define _ao_lisp_atom_delay     _atom("delay")
43 #define _ao_lisp_atom_eval      _atom("eval")
44 #define _ao_lisp_atom_read      _atom("read")
45 #else
46 #include "ao_lisp_const.h"
47 #ifndef AO_LISP_POOL
48 #define AO_LISP_POOL    16384
49 #endif
50 extern uint8_t          ao_lisp_pool[AO_LISP_POOL];
51 #endif
52
53 /* Primitive types */
54 #define AO_LISP_CONS            0
55 #define AO_LISP_INT             1
56 #define AO_LISP_STRING          2
57 #define AO_LISP_OTHER           3
58
59 #define AO_LISP_TYPE_MASK       0x0003
60 #define AO_LISP_TYPE_SHIFT      2
61 #define AO_LISP_REF_MASK        0x7ffc
62 #define AO_LISP_CONST           0x8000
63
64 /* These have a type value at the start of the struct */
65 #define AO_LISP_ATOM            4
66 #define AO_LISP_BUILTIN         5
67 #define AO_LISP_FRAME           6
68 #define AO_LISP_LAMBDA          7
69 #define AO_LISP_NUM_TYPE        8
70
71 #define AO_LISP_NIL     0
72
73 extern uint16_t         ao_lisp_top;
74
75 #define AO_LISP_OOM             0x01
76 #define AO_LISP_DIVIDE_BY_ZERO  0x02
77 #define AO_LISP_INVALID         0x04
78 #define AO_LISP_UNDEFINED       0x08
79
80 extern uint8_t          ao_lisp_exception;
81
82 typedef uint16_t        ao_poly;
83 typedef int16_t         ao_signed_poly;
84
85 static inline int
86 ao_lisp_is_const(ao_poly poly) {
87         return poly & AO_LISP_CONST;
88 }
89
90 #define AO_LISP_POOL_BASE       (ao_lisp_pool - 4)
91 #define AO_LISP_CONST_BASE      (ao_lisp_const - 4)
92
93 #define AO_LISP_IS_CONST(a)     (ao_lisp_const <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_const + AO_LISP_POOL_CONST)
94 #define AO_LISP_IS_POOL(a)      (ao_lisp_pool <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_pool + AO_LISP_POOL)
95
96 static inline void *
97 ao_lisp_ref(ao_poly poly) {
98         if (poly == 0xBEEF)
99                 ao_lisp_abort();
100         if (poly == AO_LISP_NIL)
101                 return NULL;
102         if (poly & AO_LISP_CONST)
103                 return (void *) (AO_LISP_CONST_BASE + (poly & AO_LISP_REF_MASK));
104         return (void *) (AO_LISP_POOL_BASE + (poly & AO_LISP_REF_MASK));
105 }
106
107 static inline ao_poly
108 ao_lisp_poly(const void *addr, ao_poly type) {
109         const uint8_t   *a = addr;
110         if (a == NULL)
111                 return AO_LISP_NIL;
112         if (AO_LISP_IS_CONST(a))
113                 return AO_LISP_CONST | (a - AO_LISP_CONST_BASE) | type;
114         return (a - AO_LISP_POOL_BASE) | type;
115 }
116
117 struct ao_lisp_type {
118         int     (*size)(void *addr);
119         void    (*mark)(void *addr);
120         void    (*move)(void *addr);
121 };
122
123 struct ao_lisp_cons {
124         ao_poly         car;
125         ao_poly         cdr;
126 };
127
128 struct ao_lisp_atom {
129         uint8_t         type;
130         uint8_t         pad[1];
131         ao_poly         next;
132         char            name[];
133 };
134
135 struct ao_lisp_val {
136         ao_poly         atom;
137         ao_poly         val;
138 };
139
140 struct ao_lisp_frame {
141         uint8_t                 type;
142         uint8_t                 num;
143         ao_poly                 next;
144         struct ao_lisp_val      vals[];
145 };
146
147 static inline struct ao_lisp_frame *
148 ao_lisp_poly_frame(ao_poly poly) {
149         return ao_lisp_ref(poly);
150 }
151
152 static inline ao_poly
153 ao_lisp_frame_poly(struct ao_lisp_frame *frame) {
154         return ao_lisp_poly(frame, AO_LISP_OTHER);
155 }
156
157 enum eval_state {
158         eval_sexpr,             /* Evaluate an sexpr */
159         eval_val,
160         eval_formal,
161         eval_exec,
162         eval_cond,
163         eval_cond_test,
164         eval_progn,
165         eval_while,
166         eval_while_test,
167 };
168
169 struct ao_lisp_stack {
170         uint8_t                 state;          /* enum eval_state */
171         ao_poly                 prev;           /* previous stack frame */
172         ao_poly                 sexprs;         /* expressions to evaluate */
173         ao_poly                 values;         /* values computed */
174         ao_poly                 values_tail;    /* end of the values list for easy appending */
175         ao_poly                 frame;          /* current lookup frame */
176         ao_poly                 list;           /* most recent function call */
177 };
178
179 static inline struct ao_lisp_stack *
180 ao_lisp_poly_stack(ao_poly p)
181 {
182         return ao_lisp_ref(p);
183 }
184
185 static inline ao_poly
186 ao_lisp_stack_poly(struct ao_lisp_stack *stack)
187 {
188         return ao_lisp_poly(stack, AO_LISP_OTHER);
189 }
190
191 extern struct ao_lisp_stack     *ao_lisp_stack;
192 extern ao_poly                  ao_lisp_v;
193
194 #define AO_LISP_FUNC_LAMBDA     0
195 #define AO_LISP_FUNC_NLAMBDA    1
196 #define AO_LISP_FUNC_MACRO      2
197 #define AO_LISP_FUNC_LEXPR      3
198
199 struct ao_lisp_builtin {
200         uint8_t         type;
201         uint8_t         args;
202         uint16_t        func;
203 };
204
205 enum ao_lisp_builtin_id {
206         builtin_eval,
207         builtin_read,
208         builtin_lambda,
209         builtin_lexpr,
210         builtin_nlambda,
211         builtin_macro,
212         builtin_car,
213         builtin_cdr,
214         builtin_cons,
215         builtin_last,
216         builtin_quote,
217         builtin_set,
218         builtin_setq,
219         builtin_cond,
220         builtin_progn,
221         builtin_while,
222         builtin_print,
223         builtin_patom,
224         builtin_plus,
225         builtin_minus,
226         builtin_times,
227         builtin_divide,
228         builtin_mod,
229         builtin_equal,
230         builtin_less,
231         builtin_greater,
232         builtin_less_equal,
233         builtin_greater_equal,
234         builtin_delay,
235         builtin_led,
236         _builtin_last
237 };
238
239 typedef ao_poly (*ao_lisp_func_t)(struct ao_lisp_cons *cons);
240
241 extern const ao_lisp_func_t     ao_lisp_builtins[];
242
243 static inline ao_lisp_func_t
244 ao_lisp_func(struct ao_lisp_builtin *b)
245 {
246         return ao_lisp_builtins[b->func];
247 }
248
249 struct ao_lisp_lambda {
250         uint8_t         type;
251         uint8_t         args;
252         ao_poly         code;
253         ao_poly         frame;
254 };
255
256 static inline struct ao_lisp_lambda *
257 ao_lisp_poly_lambda(ao_poly poly)
258 {
259         return ao_lisp_ref(poly);
260 }
261
262 static inline ao_poly
263 ao_lisp_lambda_poly(struct ao_lisp_lambda *lambda)
264 {
265         return ao_lisp_poly(lambda, AO_LISP_OTHER);
266 }
267
268 static inline void *
269 ao_lisp_poly_other(ao_poly poly) {
270         return ao_lisp_ref(poly);
271 }
272
273 static inline uint8_t
274 ao_lisp_other_type(void *other) {
275         return *((uint8_t *) other);
276 }
277
278 static inline ao_poly
279 ao_lisp_other_poly(const void *other)
280 {
281         return ao_lisp_poly(other, AO_LISP_OTHER);
282 }
283
284 static inline int
285 ao_lisp_mem_round(int size)
286 {
287         return (size + 3) & ~3;
288 }
289
290 #define AO_LISP_OTHER_POLY(other) ((ao_poly)(other) + AO_LISP_OTHER)
291
292 static inline int ao_lisp_poly_base_type(ao_poly poly) {
293         return poly & AO_LISP_TYPE_MASK;
294 }
295
296 static inline int ao_lisp_poly_type(ao_poly poly) {
297         int     type = poly & AO_LISP_TYPE_MASK;
298         if (type == AO_LISP_OTHER)
299                 return ao_lisp_other_type(ao_lisp_poly_other(poly));
300         return type;
301 }
302
303 static inline struct ao_lisp_cons *
304 ao_lisp_poly_cons(ao_poly poly)
305 {
306         return ao_lisp_ref(poly);
307 }
308
309 static inline ao_poly
310 ao_lisp_cons_poly(struct ao_lisp_cons *cons)
311 {
312         return ao_lisp_poly(cons, AO_LISP_CONS);
313 }
314
315 static inline int
316 ao_lisp_poly_int(ao_poly poly)
317 {
318         return (int) ((ao_signed_poly) poly >> AO_LISP_TYPE_SHIFT);
319 }
320
321 static inline ao_poly
322 ao_lisp_int_poly(int i)
323 {
324         return ((ao_poly) i << 2) | AO_LISP_INT;
325 }
326
327 static inline char *
328 ao_lisp_poly_string(ao_poly poly)
329 {
330         return ao_lisp_ref(poly);
331 }
332
333 static inline ao_poly
334 ao_lisp_string_poly(char *s)
335 {
336         return ao_lisp_poly(s, AO_LISP_STRING);
337 }
338
339 static inline struct ao_lisp_atom *
340 ao_lisp_poly_atom(ao_poly poly)
341 {
342         return ao_lisp_ref(poly);
343 }
344
345 static inline ao_poly
346 ao_lisp_atom_poly(struct ao_lisp_atom *a)
347 {
348         return ao_lisp_poly(a, AO_LISP_OTHER);
349 }
350
351 static inline struct ao_lisp_builtin *
352 ao_lisp_poly_builtin(ao_poly poly)
353 {
354         return ao_lisp_ref(poly);
355 }
356
357 static inline ao_poly
358 ao_lisp_builtin_poly(struct ao_lisp_builtin *b)
359 {
360         return ao_lisp_poly(b, AO_LISP_OTHER);
361 }
362
363 /* memory functions */
364 /* returns 1 if the object was already marked */
365 int
366 ao_lisp_mark(const struct ao_lisp_type *type, void *addr);
367
368 /* returns 1 if the object was already marked */
369 int
370 ao_lisp_mark_memory(void *addr, int size);
371
372 void *
373 ao_lisp_move_map(void *addr);
374
375 /* returns 1 if the object was already moved */
376 int
377 ao_lisp_move(const struct ao_lisp_type *type, void **ref);
378
379 /* returns 1 if the object was already moved */
380 int
381 ao_lisp_move_memory(void **ref, int size);
382
383 void *
384 ao_lisp_alloc(int size);
385
386 void
387 ao_lisp_collect(void);
388
389 int
390 ao_lisp_root_add(const struct ao_lisp_type *type, void *addr);
391
392 int
393 ao_lisp_root_poly_add(ao_poly *p);
394
395 void
396 ao_lisp_root_clear(void *addr);
397
398 /* cons */
399 extern const struct ao_lisp_type ao_lisp_cons_type;
400
401 struct ao_lisp_cons *
402 ao_lisp_cons_cons(ao_poly car, struct ao_lisp_cons *cdr);
403
404 void
405 ao_lisp_cons_print(ao_poly);
406
407 void
408 ao_lisp_cons_patom(ao_poly);
409
410 /* string */
411 extern const struct ao_lisp_type ao_lisp_string_type;
412
413 char *
414 ao_lisp_string_new(int len);
415
416 char *
417 ao_lisp_string_copy(char *a);
418
419 char *
420 ao_lisp_string_cat(char *a, char *b);
421
422 void
423 ao_lisp_string_print(ao_poly s);
424
425 void
426 ao_lisp_string_patom(ao_poly s);
427
428 /* atom */
429 extern const struct ao_lisp_type ao_lisp_atom_type;
430
431 extern struct ao_lisp_atom      *ao_lisp_atoms;
432 extern struct ao_lisp_frame     *ao_lisp_frame_global;
433 extern struct ao_lisp_frame     *ao_lisp_frame_current;
434
435 void
436 ao_lisp_atom_print(ao_poly a);
437
438 struct ao_lisp_atom *
439 ao_lisp_atom_intern(char *name);
440
441 ao_poly
442 ao_lisp_atom_get(ao_poly atom);
443
444 ao_poly
445 ao_lisp_atom_set(ao_poly atom, ao_poly val);
446
447 /* int */
448 void
449 ao_lisp_int_print(ao_poly i);
450
451 /* prim */
452 void
453 ao_lisp_poly_print(ao_poly p);
454
455 void
456 ao_lisp_poly_patom(ao_poly p);
457
458 int
459 ao_lisp_poly_mark(ao_poly p, uint8_t note_cons);
460
461 /* returns 1 if the object has already been moved */
462 int
463 ao_lisp_poly_move(ao_poly *p, uint8_t note_cons);
464
465 /* eval */
466
467 ao_poly
468 ao_lisp_eval(ao_poly p);
469
470 ao_poly
471 ao_lisp_set_cond(struct ao_lisp_cons *cons);
472
473 /* builtin */
474 void
475 ao_lisp_builtin_print(ao_poly b);
476
477 extern const struct ao_lisp_type ao_lisp_builtin_type;
478
479 /* Check argument count */
480 ao_poly
481 ao_lisp_check_argc(ao_poly name, struct ao_lisp_cons *cons, int min, int max);
482
483 /* Check argument type */
484 ao_poly
485 ao_lisp_check_argt(ao_poly name, struct ao_lisp_cons *cons, int argc, int type, int nil_ok);
486
487 /* Fetch an arg (nil if off the end) */
488 ao_poly
489 ao_lisp_arg(struct ao_lisp_cons *cons, int argc);
490
491 char *
492 ao_lisp_args_name(uint8_t args);
493
494 /* read */
495 ao_poly
496 ao_lisp_read(void);
497
498 /* rep */
499 ao_poly
500 ao_lisp_read_eval_print(void);
501
502 /* frame */
503 extern const struct ao_lisp_type ao_lisp_frame_type;
504
505 ao_poly *
506 ao_lisp_frame_ref(struct ao_lisp_frame *frame, ao_poly atom);
507
508 struct ao_lisp_frame *
509 ao_lisp_frame_new(int num);
510
511 struct ao_lisp_frame *
512 ao_lisp_frame_add(struct ao_lisp_frame *frame, ao_poly atom, ao_poly val);
513
514 void
515 ao_lisp_frame_print(ao_poly p);
516
517 /* lambda */
518 extern const struct ao_lisp_type ao_lisp_lambda_type;
519
520 struct ao_lisp_lambda *
521 ao_lisp_lambda_new(ao_poly cons);
522
523 void
524 ao_lisp_lambda_print(ao_poly lambda);
525
526 ao_poly
527 ao_lisp_lambda(struct ao_lisp_cons *cons);
528
529 ao_poly
530 ao_lisp_lexpr(struct ao_lisp_cons *cons);
531
532 ao_poly
533 ao_lisp_nlambda(struct ao_lisp_cons *cons);
534
535 ao_poly
536 ao_lisp_macro(struct ao_lisp_cons *cons);
537
538 ao_poly
539 ao_lisp_lambda_eval(struct ao_lisp_lambda *lambda,
540                     struct ao_lisp_cons *cons);
541
542 /* error */
543
544 void
545 ao_lisp_stack_print(void);
546
547 ao_poly
548 ao_lisp_error(int error, char *format, ...);
549
550 /* debugging macros */
551
552 #if DBG_EVAL
553 #define DBG_CODE        1
554 int ao_lisp_stack_depth;
555 #define DBG_DO(a)       a
556 #define DBG_INDENT()    do { int _s; for(_s = 0; _s < ao_lisp_stack_depth; _s++) printf("  "); } while(0)
557 #define DBG_IN()        (++ao_lisp_stack_depth)
558 #define DBG_OUT()       (--ao_lisp_stack_depth)
559 #define DBG_RESET()     (ao_lisp_stack_depth = 0)
560 #define DBG(...)        printf(__VA_ARGS__)
561 #define DBGI(...)       do { DBG("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
562 #define DBG_CONS(a)     ao_lisp_cons_print(ao_lisp_cons_poly(a))
563 #define DBG_POLY(a)     ao_lisp_poly_print(a)
564 #define OFFSET(a)       ((a) ? (int) ((uint8_t *) a - ao_lisp_pool) : -1)
565 #define DBG_STACK()     ao_lisp_stack_print()
566 static inline void
567 ao_lisp_frames_dump(void)
568 {
569         struct ao_lisp_stack *s;
570         DBGI(".. current frame: "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
571         for (s = ao_lisp_stack; s; s = ao_lisp_poly_stack(s->prev)) {
572                 DBGI(".. stack frame: "); DBG_POLY(s->frame); DBG("\n");
573         }
574 }
575 #define DBG_FRAMES()    ao_lisp_frames_dump()
576 #else
577 #define DBG_DO(a)
578 #define DBG_INDENT()
579 #define DBG_IN()
580 #define DBG_OUT()
581 #define DBG(...)
582 #define DBGI(...)
583 #define DBG_CONS(a)
584 #define DBG_POLY(a)
585 #define DBG_RESET()
586 #define DBG_STACK()
587 #define DBG_FRAMES()
588 #endif
589
590 #endif /* _AO_LISP_H_ */