2 * Copyright © 2016 Keith Packard <keithp@keithp.com>
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.
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.
20 #if !defined(AO_LISP_TEST) && !defined(AO_LISP_MAKE_CONST)
22 #define AO_LISP_ALTOS 1
23 #define abort() ao_panic(1)
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"))
38 #include "ao_lisp_const.h"
39 #define AO_LISP_POOL 1024
40 extern uint8_t ao_lisp_pool[AO_LISP_POOL];
44 #define AO_LISP_CONS 0
46 #define AO_LISP_STRING 2
47 #define AO_LISP_OTHER 3
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
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
62 extern uint16_t ao_lisp_top;
64 #define AO_LISP_OOM 0x01
65 #define AO_LISP_DIVIDE_BY_ZERO 0x02
66 #define AO_LISP_INVALID 0x04
68 extern uint8_t ao_lisp_exception;
70 typedef uint16_t ao_poly;
73 ao_lisp_is_const(ao_poly poly) {
74 return poly & AO_LISP_CONST;
77 #define AO_LISP_POOL_BASE (ao_lisp_pool - 4)
78 #define AO_LISP_CONST_BASE (ao_lisp_const - 4)
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)
84 ao_lisp_ref(ao_poly poly) {
85 if (poly == AO_LISP_NIL)
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));
93 ao_lisp_poly(const void *addr, ao_poly type) {
94 const uint8_t *a = addr;
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;
102 struct ao_lisp_type {
103 void (*mark)(void *addr);
104 int (*size)(void *addr);
105 void (*move)(void *addr);
108 struct ao_lisp_cons {
113 struct ao_lisp_atom {
125 struct ao_lisp_frame {
129 struct ao_lisp_val vals[];
132 static inline struct ao_lisp_frame *
133 ao_lisp_poly_frame(ao_poly poly) {
134 return ao_lisp_ref(poly);
137 static inline ao_poly
138 ao_lisp_frame_poly(struct ao_lisp_frame *frame) {
139 return ao_lisp_poly(frame, AO_LISP_OTHER);
142 #define AO_LISP_LAMBDA 0
143 #define AO_LISP_NLAMBDA 1
144 #define AO_LISP_MACRO 2
145 #define AO_LISP_LEXPR 3
147 struct ao_lisp_builtin {
153 enum ao_lisp_builtin_id {
169 typedef ao_poly (*ao_lisp_func_t)(struct ao_lisp_cons *cons);
171 extern ao_lisp_func_t ao_lisp_builtins[];
173 static inline ao_lisp_func_t
174 ao_lisp_func(struct ao_lisp_builtin *b)
176 return ao_lisp_builtins[b->func];
180 ao_lisp_poly_other(ao_poly poly) {
181 return ao_lisp_ref(poly);
184 static inline uint8_t
185 ao_lisp_other_type(void *other) {
186 return *((uint8_t *) other);
189 static inline ao_poly
190 ao_lisp_other_poly(const void *other)
192 return ao_lisp_poly(other, AO_LISP_OTHER);
196 ao_lisp_mem_round(int size)
198 return (size + 3) & ~3;
201 #define AO_LISP_OTHER_POLY(other) ((ao_poly)(other) + AO_LISP_OTHER)
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));
210 static inline struct ao_lisp_cons *
211 ao_lisp_poly_cons(ao_poly poly)
213 return ao_lisp_ref(poly);
216 static inline ao_poly
217 ao_lisp_cons_poly(struct ao_lisp_cons *cons)
219 return ao_lisp_poly(cons, AO_LISP_CONS);
223 ao_lisp_poly_int(ao_poly poly)
225 return (int) poly >> AO_LISP_TYPE_SHIFT;
228 static inline ao_poly
229 ao_lisp_int_poly(int i)
231 return ((ao_poly) i << 2) + AO_LISP_INT;
235 ao_lisp_poly_string(ao_poly poly)
237 return ao_lisp_ref(poly);
240 static inline ao_poly
241 ao_lisp_string_poly(char *s)
243 return ao_lisp_poly(s, AO_LISP_STRING);
246 static inline struct ao_lisp_atom *
247 ao_lisp_poly_atom(ao_poly poly)
249 return ao_lisp_ref(poly);
252 static inline ao_poly
253 ao_lisp_atom_poly(struct ao_lisp_atom *a)
255 return ao_lisp_poly(a, AO_LISP_OTHER);
258 static inline struct ao_lisp_builtin *
259 ao_lisp_poly_builtin(ao_poly poly)
261 return ao_lisp_ref(poly);
264 static inline ao_poly
265 ao_lisp_builtin_poly(struct ao_lisp_builtin *b)
267 return ao_lisp_poly(b, AO_LISP_OTHER);
270 /* memory functions */
272 ao_lisp_mark(const struct ao_lisp_type *type, void *addr);
274 /* returns 1 if the object was already marked */
276 ao_lisp_mark_memory(void *addr, int size);
279 ao_lisp_move_map(void *addr);
282 ao_lisp_move(const struct ao_lisp_type *type, void *addr);
284 /* returns NULL if the object was already moved */
286 ao_lisp_move_memory(void *addr, int size);
289 ao_lisp_alloc(int size);
292 ao_lisp_collect(void);
295 ao_lisp_root_add(const struct ao_lisp_type *type, void *addr);
298 ao_lisp_root_clear(void *addr);
301 extern const struct ao_lisp_type ao_lisp_cons_type;
303 struct ao_lisp_cons *
304 ao_lisp_cons_cons(ao_poly car, struct ao_lisp_cons *cdr);
307 ao_lisp_cons_print(ao_poly);
310 extern const struct ao_lisp_type ao_lisp_string_type;
313 ao_lisp_string_new(int len);
316 ao_lisp_string_copy(char *a);
319 ao_lisp_string_cat(char *a, char *b);
322 ao_lisp_string_print(ao_poly s);
325 extern const struct ao_lisp_type ao_lisp_atom_type;
327 extern struct ao_lisp_atom *ao_lisp_atoms;
330 ao_lisp_atom_init(void);
333 ao_lisp_atom_print(ao_poly a);
335 struct ao_lisp_atom *
336 ao_lisp_atom_intern(char *name);
339 ao_lisp_atom_get(ao_poly atom);
342 ao_lisp_atom_set(ao_poly atom, ao_poly val);
346 ao_lisp_int_print(ao_poly i);
350 ao_lisp_poly_print(ao_poly p);
353 ao_lisp_poly_mark(ao_poly p);
356 ao_lisp_poly_move(ao_poly p);
360 ao_lisp_eval(ao_poly p);
364 ao_lisp_builtin_print(ao_poly b);
366 extern const struct ao_lisp_type ao_lisp_builtin_type;
374 ao_lisp_read_eval_print(void);
377 extern const struct ao_lisp_type ao_lisp_frame_type;
380 ao_lisp_frame_set(struct ao_lisp_frame *frame, ao_poly atom, ao_poly val);
383 ao_lisp_frame_get(struct ao_lisp_frame *frame, ao_poly atom);
385 struct ao_lisp_frame *
386 ao_lisp_frame_new(int num, int readonly);
388 struct ao_lisp_frame *
389 ao_lisp_frame_add(struct ao_lisp_frame *frame, ao_poly atom, ao_poly val);
391 #endif /* _AO_LISP_H_ */