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