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