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
36 #define _atom(n) ao_lisp_atom_poly(ao_lisp_atom_intern(n))
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")
47 #include "ao_lisp_const.h"
49 #define AO_LISP_POOL 16384
51 extern uint8_t ao_lisp_pool[AO_LISP_POOL];
55 #define AO_LISP_CONS 0
57 #define AO_LISP_STRING 2
58 #define AO_LISP_OTHER 3
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
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
73 extern uint16_t ao_lisp_top;
75 #define AO_LISP_OOM 0x01
76 #define AO_LISP_DIVIDE_BY_ZERO 0x02
77 #define AO_LISP_INVALID 0x04
79 extern uint8_t ao_lisp_exception;
81 typedef uint16_t ao_poly;
82 typedef int16_t ao_signed_poly;
85 ao_lisp_is_const(ao_poly poly) {
86 return poly & AO_LISP_CONST;
89 #define AO_LISP_POOL_BASE (ao_lisp_pool - 4)
90 #define AO_LISP_CONST_BASE (ao_lisp_const - 4)
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)
96 ao_lisp_ref(ao_poly poly) {
99 if (poly == AO_LISP_NIL)
101 if (poly & AO_LISP_CONST)
102 return (void *) (AO_LISP_CONST_BASE + (poly & AO_LISP_REF_MASK));
103 return (void *) (AO_LISP_POOL_BASE + (poly & AO_LISP_REF_MASK));
106 static inline ao_poly
107 ao_lisp_poly(const void *addr, ao_poly type) {
108 const uint8_t *a = addr;
111 if (AO_LISP_IS_CONST(a))
112 return AO_LISP_CONST | (a - AO_LISP_CONST_BASE) | type;
113 return (a - AO_LISP_POOL_BASE) | type;
116 struct ao_lisp_type {
117 void (*mark)(void *addr);
118 int (*size)(void *addr);
119 void (*move)(void *addr);
122 struct ao_lisp_cons {
127 struct ao_lisp_atom {
139 struct ao_lisp_frame {
143 struct ao_lisp_val vals[];
146 static inline struct ao_lisp_frame *
147 ao_lisp_poly_frame(ao_poly poly) {
148 return ao_lisp_ref(poly);
151 static inline ao_poly
152 ao_lisp_frame_poly(struct ao_lisp_frame *frame) {
153 return ao_lisp_poly(frame, AO_LISP_OTHER);
156 #define AO_LISP_LAMBDA 0
157 #define AO_LISP_NLAMBDA 1
158 #define AO_LISP_MACRO 2
159 #define AO_LISP_LEXPR 3
161 struct ao_lisp_builtin {
167 enum ao_lisp_builtin_id {
186 builtin_greater_equal,
190 typedef ao_poly (*ao_lisp_func_t)(struct ao_lisp_cons *cons);
192 extern ao_lisp_func_t ao_lisp_builtins[];
194 static inline ao_lisp_func_t
195 ao_lisp_func(struct ao_lisp_builtin *b)
197 return ao_lisp_builtins[b->func];
201 ao_lisp_poly_other(ao_poly poly) {
202 return ao_lisp_ref(poly);
205 static inline uint8_t
206 ao_lisp_other_type(void *other) {
207 return *((uint8_t *) other);
210 static inline ao_poly
211 ao_lisp_other_poly(const void *other)
213 return ao_lisp_poly(other, AO_LISP_OTHER);
217 ao_lisp_mem_round(int size)
219 return (size + 3) & ~3;
222 #define AO_LISP_OTHER_POLY(other) ((ao_poly)(other) + AO_LISP_OTHER)
224 static inline int ao_lisp_poly_base_type(ao_poly poly) {
225 return poly & AO_LISP_TYPE_MASK;
228 static inline int ao_lisp_poly_type(ao_poly poly) {
229 int type = poly & AO_LISP_TYPE_MASK;
230 if (type == AO_LISP_OTHER)
231 return ao_lisp_other_type(ao_lisp_poly_other(poly));
235 static inline struct ao_lisp_cons *
236 ao_lisp_poly_cons(ao_poly poly)
238 return ao_lisp_ref(poly);
241 static inline ao_poly
242 ao_lisp_cons_poly(struct ao_lisp_cons *cons)
244 return ao_lisp_poly(cons, AO_LISP_CONS);
248 ao_lisp_poly_int(ao_poly poly)
250 return (int) ((ao_signed_poly) poly >> AO_LISP_TYPE_SHIFT);
253 static inline ao_poly
254 ao_lisp_int_poly(int i)
256 return ((ao_poly) i << 2) | AO_LISP_INT;
260 ao_lisp_poly_string(ao_poly poly)
262 return ao_lisp_ref(poly);
265 static inline ao_poly
266 ao_lisp_string_poly(char *s)
268 return ao_lisp_poly(s, AO_LISP_STRING);
271 static inline struct ao_lisp_atom *
272 ao_lisp_poly_atom(ao_poly poly)
274 return ao_lisp_ref(poly);
277 static inline ao_poly
278 ao_lisp_atom_poly(struct ao_lisp_atom *a)
280 return ao_lisp_poly(a, AO_LISP_OTHER);
283 static inline struct ao_lisp_builtin *
284 ao_lisp_poly_builtin(ao_poly poly)
286 return ao_lisp_ref(poly);
289 static inline ao_poly
290 ao_lisp_builtin_poly(struct ao_lisp_builtin *b)
292 return ao_lisp_poly(b, AO_LISP_OTHER);
295 /* memory functions */
296 /* returns 1 if the object was already marked */
298 ao_lisp_mark(const struct ao_lisp_type *type, void *addr);
300 /* returns 1 if the object was already marked */
302 ao_lisp_mark_memory(void *addr, int size);
305 ao_lisp_move_map(void *addr);
307 /* returns 1 if the object was already moved */
309 ao_lisp_move(const struct ao_lisp_type *type, void **ref);
311 /* returns 1 if the object was already moved */
313 ao_lisp_move_memory(void **ref, int size);
316 ao_lisp_alloc(int size);
319 ao_lisp_collect(void);
322 ao_lisp_root_add(const struct ao_lisp_type *type, void *addr);
325 ao_lisp_root_poly_add(ao_poly *p);
328 ao_lisp_root_clear(void *addr);
331 extern const struct ao_lisp_type ao_lisp_cons_type;
333 struct ao_lisp_cons *
334 ao_lisp_cons_cons(ao_poly car, struct ao_lisp_cons *cdr);
337 ao_lisp_cons_print(ao_poly);
340 ao_lisp_cons_patom(ao_poly);
343 extern const struct ao_lisp_type ao_lisp_string_type;
346 ao_lisp_string_new(int len);
349 ao_lisp_string_copy(char *a);
352 ao_lisp_string_cat(char *a, char *b);
355 ao_lisp_string_print(ao_poly s);
358 ao_lisp_string_patom(ao_poly s);
361 extern const struct ao_lisp_type ao_lisp_atom_type;
363 extern struct ao_lisp_atom *ao_lisp_atoms;
365 extern struct ao_lisp_frame *ao_lisp_frame_current;
368 ao_lisp_atom_print(ao_poly a);
370 struct ao_lisp_atom *
371 ao_lisp_atom_intern(char *name);
374 ao_lisp_atom_get(ao_poly atom);
377 ao_lisp_atom_set(ao_poly atom, ao_poly val);
381 ao_lisp_int_print(ao_poly i);
385 ao_lisp_poly_print(ao_poly p);
388 ao_lisp_poly_patom(ao_poly p);
391 ao_lisp_poly_mark(ao_poly p, uint8_t note_cons);
393 /* returns 1 if the object has already been moved */
395 ao_lisp_poly_move(ao_poly *p, uint8_t note_cons);
400 ao_lisp_eval(ao_poly p);
403 ao_lisp_set_cond(struct ao_lisp_cons *cons);
407 ao_lisp_builtin_print(ao_poly b);
409 extern const struct ao_lisp_type ao_lisp_builtin_type;
411 /* Check argument count */
413 ao_lisp_check_argc(ao_poly name, struct ao_lisp_cons *cons, int min, int max);
415 /* Check argument type */
417 ao_lisp_check_argt(ao_poly name, struct ao_lisp_cons *cons, int argc, int type, int nil_ok);
419 /* Fetch an arg (nil if off the end) */
421 ao_lisp_arg(struct ao_lisp_cons *cons, int argc);
429 ao_lisp_read_eval_print(void);
432 extern const struct ao_lisp_type ao_lisp_frame_type;
435 ao_lisp_frame_ref(struct ao_lisp_frame *frame, ao_poly atom);
437 struct ao_lisp_frame *
438 ao_lisp_frame_new(int num);
440 struct ao_lisp_frame *
441 ao_lisp_frame_add(struct ao_lisp_frame *frame, ao_poly atom, ao_poly val);
446 ao_lisp_error(int error, char *format, ...);
448 #endif /* _AO_LISP_H_ */