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.
18 #if !defined(AO_LISP_TEST) && !defined(AO_LISP_MAKE_CONST)
20 #define AO_LISP_ALTOS 1
27 #ifdef AO_LISP_MAKE_CONST
28 #define AO_LISP_POOL_CONST 16384
29 extern uint8_t ao_lisp_const[AO_LISP_POOL_CONST];
31 #include "ao_lisp_const.h"
35 #define AO_LISP_CONS 0
37 #define AO_LISP_STRING 2
38 #define AO_LISP_OTHER 3
40 #define AO_LISP_TYPE_MASK 0x0003
41 #define AO_LISP_TYPE_SHIFT 2
42 #define AO_LISP_REF_MASK 0x7ffc
43 #define AO_LISP_CONST 0x8000
45 /* These have a type value at the start of the struct */
46 #define AO_LISP_ATOM 4
47 #define AO_LISP_BUILTIN 5
48 #define AO_LISP_NUM_TYPE 6
52 #define AO_LISP_POOL 1024
54 extern uint8_t ao_lisp_pool[AO_LISP_POOL];
55 extern uint16_t ao_lisp_top;
57 #define AO_LISP_OOM 0x01
58 #define AO_LISP_DIVIDE_BY_ZERO 0x02
59 #define AO_LISP_INVALID 0x04
61 extern uint8_t ao_lisp_exception;
63 typedef uint16_t ao_poly;
66 ao_lisp_ref(ao_poly poly) {
67 if (poly == AO_LISP_NIL)
69 if (poly & AO_LISP_CONST)
70 return (void *) ((ao_lisp_const - 4) + (poly & AO_LISP_REF_MASK));
72 return (void *) ((ao_lisp_pool - 4) + (poly & AO_LISP_REF_MASK));
76 ao_lisp_poly(const void *addr, ao_poly type) {
77 const uint8_t *a = addr;
80 if (ao_lisp_pool <= a && a < ao_lisp_pool + AO_LISP_POOL)
81 return (a - (ao_lisp_pool - 4)) | type;
82 else if (ao_lisp_const <= a && a <= ao_lisp_const + AO_LISP_POOL_CONST)
83 return AO_LISP_CONST | (a - (ao_lisp_const - 4)) | type;
85 ao_lisp_exception |= AO_LISP_INVALID;
90 #define AO_LISP_POLY(addr, type) (((ao_lisp_pool <= ((uint8_t *) (a)) && \
91 ((uint8_t *) (a)) < ao_lisp_pool + AO_LISP_POOL) ? \
92 ((uint8_t *) (a) - (ao_lisp_pool - 4)) : \
93 (((uint8_t *) (a) - (ao_lisp_const - 4)) | AO_LISP_POOL_CONST)) | \
97 void (*mark)(void *addr);
98 int (*size)(void *addr);
99 void (*move)(void *addr);
102 struct ao_lisp_cons {
107 struct ao_lisp_atom {
115 #define AO_LISP_LAMBDA 0
116 #define AO_LISP_NLAMBDA 1
117 #define AO_LISP_MACRO 2
118 #define AO_LISP_LEXPR 3
120 struct ao_lisp_builtin {
126 enum ao_lisp_builtin_id {
140 typedef ao_poly (*ao_lisp_func_t)(struct ao_lisp_cons *cons);
142 extern ao_lisp_func_t ao_lisp_builtins[];
144 static inline ao_lisp_func_t
145 ao_lisp_func(struct ao_lisp_builtin *b)
147 return ao_lisp_builtins[b->func];
151 ao_lisp_poly_other(ao_poly poly) {
152 return ao_lisp_ref(poly);
155 static inline ao_poly
156 ao_lisp_other_poly(const void *other)
158 return ao_lisp_poly(other, AO_LISP_OTHER);
162 ao_lisp_mem_round(int size)
164 return (size + 3) & ~3;
167 #define AO_LISP_OTHER_POLY(other) ((ao_poly)(other) + AO_LISP_OTHER)
169 static inline int ao_lisp_poly_type(ao_poly poly) {
171 if (type == AO_LISP_OTHER)
172 return *((uint8_t *) ao_lisp_poly_other(poly));
176 static inline struct ao_lisp_cons *
177 ao_lisp_poly_cons(ao_poly poly)
179 return ao_lisp_ref(poly);
182 static inline ao_poly
183 ao_lisp_cons_poly(struct ao_lisp_cons *cons)
185 return ao_lisp_poly(cons, AO_LISP_CONS);
189 ao_lisp_poly_int(ao_poly poly)
191 return (int) poly >> AO_LISP_TYPE_SHIFT;
194 static inline ao_poly
195 ao_lisp_int_poly(int i)
197 return ((ao_poly) i << 2) + AO_LISP_INT;
201 ao_lisp_poly_string(ao_poly poly)
203 return ao_lisp_ref(poly);
206 static inline ao_poly
207 ao_lisp_string_poly(char *s)
209 return ao_lisp_poly(s, AO_LISP_STRING);
212 static inline struct ao_lisp_atom *
213 ao_lisp_poly_atom(ao_poly poly)
215 return ao_lisp_ref(poly);
218 static inline ao_poly
219 ao_lisp_atom_poly(struct ao_lisp_atom *a)
221 return ao_lisp_poly(a, AO_LISP_OTHER);
224 static inline struct ao_lisp_builtin *
225 ao_lisp_poly_builtin(ao_poly poly)
227 return ao_lisp_ref(poly);
230 static inline ao_poly
231 ao_lisp_builtin_poly(struct ao_lisp_builtin *b)
233 return ao_lisp_poly(b, AO_LISP_OTHER);
236 /* memory functions */
238 ao_lisp_mark(const struct ao_lisp_type *type, void *addr);
240 /* returns 1 if the object was already marked */
242 ao_lisp_mark_memory(void *addr, int size);
245 ao_lisp_move(const struct ao_lisp_type *type, void *addr);
247 /* returns NULL if the object was already moved */
249 ao_lisp_move_memory(void *addr, int size);
252 ao_lisp_alloc(int size);
255 ao_lisp_root_add(const struct ao_lisp_type *type, void *addr);
258 ao_lisp_root_clear(void *addr);
261 extern const struct ao_lisp_type ao_lisp_cons_type;
263 struct ao_lisp_cons *
264 ao_lisp_cons_cons(ao_poly car, struct ao_lisp_cons *cdr);
267 ao_lisp_cons_print(ao_poly);
270 extern const struct ao_lisp_type ao_lisp_string_type;
273 ao_lisp_string_new(int len);
276 ao_lisp_string_copy(char *a);
279 ao_lisp_string_cat(char *a, char *b);
282 ao_lisp_string_print(ao_poly s);
285 extern const struct ao_lisp_type ao_lisp_atom_type;
287 extern struct ao_lisp_atom *ao_lisp_atoms;
290 ao_lisp_atom_init(void);
293 ao_lisp_atom_print(ao_poly a);
295 struct ao_lisp_atom *
296 ao_lisp_atom_intern(char *name);
300 ao_lisp_int_print(ao_poly i);
304 ao_lisp_poly_print(ao_poly p);
307 ao_lisp_poly_mark(ao_poly p);
310 ao_lisp_poly_move(ao_poly p);
314 ao_lisp_eval(ao_poly p);
318 ao_lisp_builtin_print(ao_poly b);
326 ao_lisp_read_eval_print(void);
328 #endif /* _AO_LISP_H_ */