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