98e99acbaec768d4467521f946f19f9741a0be3e
[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 #define _ao_lisp_atom_quote ao_lisp_atom_poly(ao_lisp_atom_intern("quote"))
36 #define _ao_lisp_atom_set ao_lisp_atom_poly(ao_lisp_atom_intern("set"))
37 #else
38 #include "ao_lisp_const.h"
39 #define AO_LISP_POOL    1024
40 extern uint8_t          ao_lisp_pool[AO_LISP_POOL];
41 #endif
42
43 /* Primitive types */
44 #define AO_LISP_CONS            0
45 #define AO_LISP_INT             1
46 #define AO_LISP_STRING          2
47 #define AO_LISP_OTHER           3
48
49 #define AO_LISP_TYPE_MASK       0x0003
50 #define AO_LISP_TYPE_SHIFT      2
51 #define AO_LISP_REF_MASK        0x7ffc
52 #define AO_LISP_CONST           0x8000
53
54 /* These have a type value at the start of the struct */
55 #define AO_LISP_ATOM            4
56 #define AO_LISP_BUILTIN         5
57 #define AO_LISP_FRAME           6
58 #define AO_LISP_NUM_TYPE        7
59
60 #define AO_LISP_NIL     0
61
62 extern uint16_t         ao_lisp_top;
63
64 #define AO_LISP_OOM             0x01
65 #define AO_LISP_DIVIDE_BY_ZERO  0x02
66 #define AO_LISP_INVALID         0x04
67
68 extern uint8_t          ao_lisp_exception;
69
70 typedef uint16_t        ao_poly;
71
72 static inline int
73 ao_lisp_is_const(ao_poly poly) {
74         return poly & AO_LISP_CONST;
75 }
76
77 #define AO_LISP_POOL_BASE       (ao_lisp_pool - 4)
78 #define AO_LISP_CONST_BASE      (ao_lisp_const - 4)
79
80 #define AO_LISP_IS_CONST(a)     (ao_lisp_const <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_const + AO_LISP_POOL_CONST)
81 #define AO_LISP_IS_POOL(a)      (ao_lisp_pool <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_pool + AO_LISP_POOL)
82
83 static inline void *
84 ao_lisp_ref(ao_poly poly) {
85         if (poly == AO_LISP_NIL)
86                 return NULL;
87         if (poly & AO_LISP_CONST)
88                 return (void *) (AO_LISP_CONST_BASE + (poly & AO_LISP_REF_MASK));
89         return (void *) (AO_LISP_POOL_BASE + (poly & AO_LISP_REF_MASK));
90 }
91
92 static inline ao_poly
93 ao_lisp_poly(const void *addr, ao_poly type) {
94         const uint8_t   *a = addr;
95         if (a == NULL)
96                 return AO_LISP_NIL;
97         if (AO_LISP_IS_CONST(a))
98                 return AO_LISP_CONST | (a - AO_LISP_CONST_BASE) | type;
99         return (a - AO_LISP_POOL_BASE) | type;
100 }
101
102 struct ao_lisp_type {
103         void    (*mark)(void *addr);
104         int     (*size)(void *addr);
105         void    (*move)(void *addr);
106 };
107
108 struct ao_lisp_cons {
109         ao_poly         car;
110         ao_poly         cdr;
111 };
112
113 struct ao_lisp_atom {
114         uint8_t         type;
115         uint8_t         pad[1];
116         ao_poly         next;
117         char            name[];
118 };
119
120 struct ao_lisp_val {
121         ao_poly         atom;
122         ao_poly         val;
123 };
124
125 struct ao_lisp_frame {
126         uint8_t                 num;
127         uint8_t                 readonly;
128         ao_poly                 next;
129         struct ao_lisp_val      vals[];
130 };
131
132 static inline struct ao_lisp_frame *
133 ao_lisp_poly_frame(ao_poly poly) {
134         return ao_lisp_ref(poly);
135 }
136
137 static inline ao_poly
138 ao_lisp_frame_poly(struct ao_lisp_frame *frame) {
139         return ao_lisp_poly(frame, AO_LISP_OTHER);
140 }
141
142 #define AO_LISP_LAMBDA  0
143 #define AO_LISP_NLAMBDA 1
144 #define AO_LISP_MACRO   2
145 #define AO_LISP_LEXPR   3
146
147 struct ao_lisp_builtin {
148         uint8_t         type;
149         uint8_t         args;
150         uint16_t        func;
151 };
152
153 enum ao_lisp_builtin_id {
154         builtin_car,
155         builtin_cdr,
156         builtin_cons,
157         builtin_quote,
158         builtin_set,
159         builtin_setq,
160         builtin_print,
161         builtin_plus,
162         builtin_minus,
163         builtin_times,
164         builtin_divide,
165         builtin_mod,
166         builtin_last
167 };
168
169 typedef ao_poly (*ao_lisp_func_t)(struct ao_lisp_cons *cons);
170
171 extern ao_lisp_func_t   ao_lisp_builtins[];
172
173 static inline ao_lisp_func_t
174 ao_lisp_func(struct ao_lisp_builtin *b)
175 {
176         return ao_lisp_builtins[b->func];
177 }
178
179 static inline void *
180 ao_lisp_poly_other(ao_poly poly) {
181         return ao_lisp_ref(poly);
182 }
183
184 static inline uint8_t
185 ao_lisp_other_type(void *other) {
186         return *((uint8_t *) other);
187 }
188
189 static inline ao_poly
190 ao_lisp_other_poly(const void *other)
191 {
192         return ao_lisp_poly(other, AO_LISP_OTHER);
193 }
194
195 static inline int
196 ao_lisp_mem_round(int size)
197 {
198         return (size + 3) & ~3;
199 }
200
201 #define AO_LISP_OTHER_POLY(other) ((ao_poly)(other) + AO_LISP_OTHER)
202
203 static inline int ao_lisp_poly_type(ao_poly poly) {
204         int     type = poly & AO_LISP_TYPE_MASK;
205         if (type == AO_LISP_OTHER)
206                 return ao_lisp_other_type(ao_lisp_poly_other(poly));
207         return type;
208 }
209
210 static inline struct ao_lisp_cons *
211 ao_lisp_poly_cons(ao_poly poly)
212 {
213         return ao_lisp_ref(poly);
214 }
215
216 static inline ao_poly
217 ao_lisp_cons_poly(struct ao_lisp_cons *cons)
218 {
219         return ao_lisp_poly(cons, AO_LISP_CONS);
220 }
221
222 static inline int
223 ao_lisp_poly_int(ao_poly poly)
224 {
225         return (int) poly >> AO_LISP_TYPE_SHIFT;
226 }
227
228 static inline ao_poly
229 ao_lisp_int_poly(int i)
230 {
231         return ((ao_poly) i << 2) + AO_LISP_INT;
232 }
233
234 static inline char *
235 ao_lisp_poly_string(ao_poly poly)
236 {
237         return ao_lisp_ref(poly);
238 }
239
240 static inline ao_poly
241 ao_lisp_string_poly(char *s)
242 {
243         return ao_lisp_poly(s, AO_LISP_STRING);
244 }
245
246 static inline struct ao_lisp_atom *
247 ao_lisp_poly_atom(ao_poly poly)
248 {
249         return ao_lisp_ref(poly);
250 }
251
252 static inline ao_poly
253 ao_lisp_atom_poly(struct ao_lisp_atom *a)
254 {
255         return ao_lisp_poly(a, AO_LISP_OTHER);
256 }
257
258 static inline struct ao_lisp_builtin *
259 ao_lisp_poly_builtin(ao_poly poly)
260 {
261         return ao_lisp_ref(poly);
262 }
263
264 static inline ao_poly
265 ao_lisp_builtin_poly(struct ao_lisp_builtin *b)
266 {
267         return ao_lisp_poly(b, AO_LISP_OTHER);
268 }
269
270 /* memory functions */
271 void
272 ao_lisp_mark(const struct ao_lisp_type *type, void *addr);
273
274 /* returns 1 if the object was already marked */
275 int
276 ao_lisp_mark_memory(void *addr, int size);
277
278 void *
279 ao_lisp_move_map(void *addr);
280
281 void *
282 ao_lisp_move(const struct ao_lisp_type *type, void *addr);
283
284 /* returns NULL if the object was already moved */
285 void *
286 ao_lisp_move_memory(void *addr, int size);
287
288 void *
289 ao_lisp_alloc(int size);
290
291 void
292 ao_lisp_collect(void);
293
294 int
295 ao_lisp_root_add(const struct ao_lisp_type *type, void *addr);
296
297 void
298 ao_lisp_root_clear(void *addr);
299
300 /* cons */
301 extern const struct ao_lisp_type ao_lisp_cons_type;
302
303 struct ao_lisp_cons *
304 ao_lisp_cons_cons(ao_poly car, struct ao_lisp_cons *cdr);
305
306 void
307 ao_lisp_cons_print(ao_poly);
308
309 /* string */
310 extern const struct ao_lisp_type ao_lisp_string_type;
311
312 char *
313 ao_lisp_string_new(int len);
314
315 char *
316 ao_lisp_string_copy(char *a);
317
318 char *
319 ao_lisp_string_cat(char *a, char *b);
320
321 void
322 ao_lisp_string_print(ao_poly s);
323
324 /* atom */
325 extern const struct ao_lisp_type ao_lisp_atom_type;
326
327 extern struct ao_lisp_atom *ao_lisp_atoms;
328
329 void
330 ao_lisp_atom_init(void);
331
332 void
333 ao_lisp_atom_print(ao_poly a);
334
335 struct ao_lisp_atom *
336 ao_lisp_atom_intern(char *name);
337
338 ao_poly
339 ao_lisp_atom_get(ao_poly atom);
340
341 ao_poly
342 ao_lisp_atom_set(ao_poly atom, ao_poly val);
343
344 /* int */
345 void
346 ao_lisp_int_print(ao_poly i);
347
348 /* prim */
349 ao_poly
350 ao_lisp_poly_print(ao_poly p);
351
352 void
353 ao_lisp_poly_mark(ao_poly p);
354
355 ao_poly
356 ao_lisp_poly_move(ao_poly p);
357
358 /* eval */
359 ao_poly
360 ao_lisp_eval(ao_poly p);
361
362 /* builtin */
363 void
364 ao_lisp_builtin_print(ao_poly b);
365
366 extern const struct ao_lisp_type ao_lisp_builtin_type;
367
368 /* read */
369 ao_poly
370 ao_lisp_read(void);
371
372 /* rep */
373 ao_poly
374 ao_lisp_read_eval_print(void);
375
376 /* frame */
377 extern const struct ao_lisp_type ao_lisp_frame_type;
378
379 int
380 ao_lisp_frame_set(struct ao_lisp_frame *frame, ao_poly atom, ao_poly val);
381
382 ao_poly
383 ao_lisp_frame_get(struct ao_lisp_frame *frame, ao_poly atom);
384
385 struct ao_lisp_frame *
386 ao_lisp_frame_new(int num, int readonly);
387
388 struct ao_lisp_frame *
389 ao_lisp_frame_add(struct ao_lisp_frame *frame, ao_poly atom, ao_poly val);
390
391 #endif /* _AO_LISP_H_ */