altos: Add lambda support to lisp
[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_cond      _atom("cond")
46 #else
47 #include "ao_lisp_const.h"
48 #ifndef AO_LISP_POOL
49 #define AO_LISP_POOL    1024
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_NUM_TYPE        7
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
79 extern uint8_t          ao_lisp_exception;
80
81 typedef uint16_t        ao_poly;
82 typedef int16_t         ao_signed_poly;
83
84 static inline int
85 ao_lisp_is_const(ao_poly poly) {
86         return poly & AO_LISP_CONST;
87 }
88
89 #define AO_LISP_POOL_BASE       (ao_lisp_pool - 4)
90 #define AO_LISP_CONST_BASE      (ao_lisp_const - 4)
91
92 #define AO_LISP_IS_CONST(a)     (ao_lisp_const <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_const + AO_LISP_POOL_CONST)
93 #define AO_LISP_IS_POOL(a)      (ao_lisp_pool <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_pool + AO_LISP_POOL)
94
95 static inline void *
96 ao_lisp_ref(ao_poly poly) {
97         if (poly == AO_LISP_NIL)
98                 return NULL;
99         if (poly & AO_LISP_CONST)
100                 return (void *) (AO_LISP_CONST_BASE + (poly & AO_LISP_REF_MASK));
101         return (void *) (AO_LISP_POOL_BASE + (poly & AO_LISP_REF_MASK));
102 }
103
104 static inline ao_poly
105 ao_lisp_poly(const void *addr, ao_poly type) {
106         const uint8_t   *a = addr;
107         if (a == NULL)
108                 return AO_LISP_NIL;
109         if (AO_LISP_IS_CONST(a))
110                 return AO_LISP_CONST | (a - AO_LISP_CONST_BASE) | type;
111         return (a - AO_LISP_POOL_BASE) | type;
112 }
113
114 struct ao_lisp_type {
115         void    (*mark)(void *addr);
116         int     (*size)(void *addr);
117         void    (*move)(void *addr);
118 };
119
120 struct ao_lisp_cons {
121         ao_poly         car;
122         ao_poly         cdr;
123 };
124
125 struct ao_lisp_atom {
126         uint8_t         type;
127         uint8_t         pad[1];
128         ao_poly         next;
129         char            name[];
130 };
131
132 struct ao_lisp_val {
133         ao_poly         atom;
134         ao_poly         val;
135 };
136
137 struct ao_lisp_frame {
138         uint8_t                 num;
139         uint8_t                 readonly;
140         ao_poly                 next;
141         struct ao_lisp_val      vals[];
142 };
143
144 static inline struct ao_lisp_frame *
145 ao_lisp_poly_frame(ao_poly poly) {
146         return ao_lisp_ref(poly);
147 }
148
149 static inline ao_poly
150 ao_lisp_frame_poly(struct ao_lisp_frame *frame) {
151         return ao_lisp_poly(frame, AO_LISP_OTHER);
152 }
153
154 #define AO_LISP_LAMBDA  0
155 #define AO_LISP_NLAMBDA 1
156 #define AO_LISP_MACRO   2
157 #define AO_LISP_LEXPR   3
158
159 struct ao_lisp_builtin {
160         uint8_t         type;
161         uint8_t         args;
162         uint16_t        func;
163 };
164
165 enum ao_lisp_builtin_id {
166         builtin_car,
167         builtin_cdr,
168         builtin_cons,
169         builtin_quote,
170         builtin_set,
171         builtin_setq,
172         builtin_cond,
173         builtin_print,
174         builtin_plus,
175         builtin_minus,
176         builtin_times,
177         builtin_divide,
178         builtin_mod,
179         builtin_last
180 };
181
182 typedef ao_poly (*ao_lisp_func_t)(struct ao_lisp_cons *cons);
183
184 extern ao_lisp_func_t   ao_lisp_builtins[];
185
186 static inline ao_lisp_func_t
187 ao_lisp_func(struct ao_lisp_builtin *b)
188 {
189         return ao_lisp_builtins[b->func];
190 }
191
192 static inline void *
193 ao_lisp_poly_other(ao_poly poly) {
194         return ao_lisp_ref(poly);
195 }
196
197 static inline uint8_t
198 ao_lisp_other_type(void *other) {
199         return *((uint8_t *) other);
200 }
201
202 static inline ao_poly
203 ao_lisp_other_poly(const void *other)
204 {
205         return ao_lisp_poly(other, AO_LISP_OTHER);
206 }
207
208 static inline int
209 ao_lisp_mem_round(int size)
210 {
211         return (size + 3) & ~3;
212 }
213
214 #define AO_LISP_OTHER_POLY(other) ((ao_poly)(other) + AO_LISP_OTHER)
215
216 static inline int ao_lisp_poly_type(ao_poly poly) {
217         int     type = poly & AO_LISP_TYPE_MASK;
218         if (type == AO_LISP_OTHER)
219                 return ao_lisp_other_type(ao_lisp_poly_other(poly));
220         return type;
221 }
222
223 static inline struct ao_lisp_cons *
224 ao_lisp_poly_cons(ao_poly poly)
225 {
226         return ao_lisp_ref(poly);
227 }
228
229 static inline ao_poly
230 ao_lisp_cons_poly(struct ao_lisp_cons *cons)
231 {
232         return ao_lisp_poly(cons, AO_LISP_CONS);
233 }
234
235 static inline int
236 ao_lisp_poly_int(ao_poly poly)
237 {
238         return (int) ((ao_signed_poly) poly >> AO_LISP_TYPE_SHIFT);
239 }
240
241 static inline ao_poly
242 ao_lisp_int_poly(int i)
243 {
244         return ((ao_poly) i << 2) | AO_LISP_INT;
245 }
246
247 static inline char *
248 ao_lisp_poly_string(ao_poly poly)
249 {
250         return ao_lisp_ref(poly);
251 }
252
253 static inline ao_poly
254 ao_lisp_string_poly(char *s)
255 {
256         return ao_lisp_poly(s, AO_LISP_STRING);
257 }
258
259 static inline struct ao_lisp_atom *
260 ao_lisp_poly_atom(ao_poly poly)
261 {
262         return ao_lisp_ref(poly);
263 }
264
265 static inline ao_poly
266 ao_lisp_atom_poly(struct ao_lisp_atom *a)
267 {
268         return ao_lisp_poly(a, AO_LISP_OTHER);
269 }
270
271 static inline struct ao_lisp_builtin *
272 ao_lisp_poly_builtin(ao_poly poly)
273 {
274         return ao_lisp_ref(poly);
275 }
276
277 static inline ao_poly
278 ao_lisp_builtin_poly(struct ao_lisp_builtin *b)
279 {
280         return ao_lisp_poly(b, AO_LISP_OTHER);
281 }
282
283 /* memory functions */
284 void
285 ao_lisp_mark(const struct ao_lisp_type *type, void *addr);
286
287 /* returns 1 if the object was already marked */
288 int
289 ao_lisp_mark_memory(void *addr, int size);
290
291 void *
292 ao_lisp_move_map(void *addr);
293
294 void *
295 ao_lisp_move(const struct ao_lisp_type *type, void *addr);
296
297 /* returns NULL if the object was already moved */
298 void *
299 ao_lisp_move_memory(void *addr, int size);
300
301 void *
302 ao_lisp_alloc(int size);
303
304 void
305 ao_lisp_collect(void);
306
307 int
308 ao_lisp_root_add(const struct ao_lisp_type *type, void *addr);
309
310 void
311 ao_lisp_root_clear(void *addr);
312
313 /* cons */
314 extern const struct ao_lisp_type ao_lisp_cons_type;
315
316 struct ao_lisp_cons *
317 ao_lisp_cons_cons(ao_poly car, struct ao_lisp_cons *cdr);
318
319 void
320 ao_lisp_cons_print(ao_poly);
321
322 /* string */
323 extern const struct ao_lisp_type ao_lisp_string_type;
324
325 char *
326 ao_lisp_string_new(int len);
327
328 char *
329 ao_lisp_string_copy(char *a);
330
331 char *
332 ao_lisp_string_cat(char *a, char *b);
333
334 void
335 ao_lisp_string_print(ao_poly s);
336
337 /* atom */
338 extern const struct ao_lisp_type ao_lisp_atom_type;
339
340 extern struct ao_lisp_atom *ao_lisp_atoms;
341
342 extern struct ao_lisp_frame *ao_lisp_frame_current;
343
344 void
345 ao_lisp_atom_print(ao_poly a);
346
347 struct ao_lisp_atom *
348 ao_lisp_atom_intern(char *name);
349
350 ao_poly
351 ao_lisp_atom_get(ao_poly atom);
352
353 ao_poly
354 ao_lisp_atom_set(ao_poly atom, ao_poly val);
355
356 /* int */
357 void
358 ao_lisp_int_print(ao_poly i);
359
360 /* prim */
361 ao_poly
362 ao_lisp_poly_print(ao_poly p);
363
364 void
365 ao_lisp_poly_mark(ao_poly p);
366
367 ao_poly
368 ao_lisp_poly_move(ao_poly p);
369
370 /* eval */
371 ao_poly
372 ao_lisp_eval(ao_poly p);
373
374 ao_poly
375 ao_lisp_set_cond(struct ao_lisp_cons *cons);
376
377 /* builtin */
378 void
379 ao_lisp_builtin_print(ao_poly b);
380
381 extern const struct ao_lisp_type ao_lisp_builtin_type;
382
383 /* Check argument count */
384 ao_poly
385 ao_lisp_check_argc(ao_poly name, struct ao_lisp_cons *cons, int min, int max);
386
387 /* Check argument type */
388 ao_poly
389 ao_lisp_check_argt(ao_poly name, struct ao_lisp_cons *cons, int argc, int type, int nil_ok);
390
391 /* Fetch an arg (nil if off the end) */
392 ao_poly
393 ao_lisp_arg(struct ao_lisp_cons *cons, int argc);
394
395 /* read */
396 ao_poly
397 ao_lisp_read(void);
398
399 /* rep */
400 ao_poly
401 ao_lisp_read_eval_print(void);
402
403 /* frame */
404 extern const struct ao_lisp_type ao_lisp_frame_type;
405
406 ao_poly *
407 ao_lisp_frame_ref(struct ao_lisp_frame *frame, ao_poly atom);
408
409 struct ao_lisp_frame *
410 ao_lisp_frame_new(int num, int readonly);
411
412 struct ao_lisp_frame *
413 ao_lisp_frame_add(struct ao_lisp_frame *frame, ao_poly atom, ao_poly val);
414
415 /* error */
416
417 ao_poly
418 ao_lisp_error(int error, char *format, ...);
419
420 #endif /* _AO_LISP_H_ */