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