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_last _atom("last")
46 #define _ao_lisp_atom_cond _atom("cond")
47 #define _ao_lisp_atom_lambda _atom("lambda")
49 #include "ao_lisp_const.h"
51 #define AO_LISP_POOL 16384
53 extern uint8_t ao_lisp_pool[AO_LISP_POOL];
57 #define AO_LISP_CONS 0
59 #define AO_LISP_STRING 2
60 #define AO_LISP_OTHER 3
62 #define AO_LISP_TYPE_MASK 0x0003
63 #define AO_LISP_TYPE_SHIFT 2
64 #define AO_LISP_REF_MASK 0x7ffc
65 #define AO_LISP_CONST 0x8000
67 /* These have a type value at the start of the struct */
68 #define AO_LISP_ATOM 4
69 #define AO_LISP_BUILTIN 5
70 #define AO_LISP_FRAME 6
71 #define AO_LISP_LAMBDA 7
72 #define AO_LISP_NUM_TYPE 8
76 extern uint16_t ao_lisp_top;
78 #define AO_LISP_OOM 0x01
79 #define AO_LISP_DIVIDE_BY_ZERO 0x02
80 #define AO_LISP_INVALID 0x04
82 extern uint8_t ao_lisp_exception;
84 typedef uint16_t ao_poly;
85 typedef int16_t ao_signed_poly;
88 ao_lisp_is_const(ao_poly poly) {
89 return poly & AO_LISP_CONST;
92 #define AO_LISP_POOL_BASE (ao_lisp_pool - 4)
93 #define AO_LISP_CONST_BASE (ao_lisp_const - 4)
95 #define AO_LISP_IS_CONST(a) (ao_lisp_const <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_const + AO_LISP_POOL_CONST)
96 #define AO_LISP_IS_POOL(a) (ao_lisp_pool <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_pool + AO_LISP_POOL)
99 ao_lisp_ref(ao_poly poly) {
102 if (poly == AO_LISP_NIL)
104 if (poly & AO_LISP_CONST)
105 return (void *) (AO_LISP_CONST_BASE + (poly & AO_LISP_REF_MASK));
106 return (void *) (AO_LISP_POOL_BASE + (poly & AO_LISP_REF_MASK));
109 static inline ao_poly
110 ao_lisp_poly(const void *addr, ao_poly type) {
111 const uint8_t *a = addr;
114 if (AO_LISP_IS_CONST(a))
115 return AO_LISP_CONST | (a - AO_LISP_CONST_BASE) | type;
116 return (a - AO_LISP_POOL_BASE) | type;
119 struct ao_lisp_type {
120 int (*size)(void *addr);
121 void (*mark)(void *addr);
122 void (*move)(void *addr);
125 struct ao_lisp_cons {
130 struct ao_lisp_atom {
142 struct ao_lisp_frame {
146 struct ao_lisp_val vals[];
149 static inline struct ao_lisp_frame *
150 ao_lisp_poly_frame(ao_poly poly) {
151 return ao_lisp_ref(poly);
154 static inline ao_poly
155 ao_lisp_frame_poly(struct ao_lisp_frame *frame) {
156 return ao_lisp_poly(frame, AO_LISP_OTHER);
159 struct ao_lisp_stack {
181 static inline struct ao_lisp_stack *
182 ao_lisp_poly_stack(ao_poly p)
184 return ao_lisp_ref(p);
187 static inline ao_poly
188 ao_lisp_stack_poly(struct ao_lisp_stack *stack)
190 return ao_lisp_poly(stack, AO_LISP_OTHER);
193 extern struct ao_lisp_stack *ao_lisp_stack;
194 extern ao_poly ao_lisp_v;
196 #define AO_LISP_FUNC_LAMBDA 0
197 #define AO_LISP_FUNC_NLAMBDA 1
198 #define AO_LISP_FUNC_MACRO 2
199 #define AO_LISP_FUNC_LEXPR 3
201 struct ao_lisp_builtin {
207 enum ao_lisp_builtin_id {
231 builtin_greater_equal,
235 typedef ao_poly (*ao_lisp_func_t)(struct ao_lisp_cons *cons);
237 extern ao_lisp_func_t ao_lisp_builtins[];
239 static inline ao_lisp_func_t
240 ao_lisp_func(struct ao_lisp_builtin *b)
242 return ao_lisp_builtins[b->func];
245 struct ao_lisp_lambda {
252 static inline struct ao_lisp_lambda *
253 ao_lisp_poly_lambda(ao_poly poly)
255 return ao_lisp_ref(poly);
258 static inline ao_poly
259 ao_lisp_lambda_poly(struct ao_lisp_lambda *lambda)
261 return ao_lisp_poly(lambda, AO_LISP_OTHER);
265 ao_lisp_poly_other(ao_poly poly) {
266 return ao_lisp_ref(poly);
269 static inline uint8_t
270 ao_lisp_other_type(void *other) {
271 return *((uint8_t *) other);
274 static inline ao_poly
275 ao_lisp_other_poly(const void *other)
277 return ao_lisp_poly(other, AO_LISP_OTHER);
281 ao_lisp_mem_round(int size)
283 return (size + 3) & ~3;
286 #define AO_LISP_OTHER_POLY(other) ((ao_poly)(other) + AO_LISP_OTHER)
288 static inline int ao_lisp_poly_base_type(ao_poly poly) {
289 return poly & AO_LISP_TYPE_MASK;
292 static inline int ao_lisp_poly_type(ao_poly poly) {
293 int type = poly & AO_LISP_TYPE_MASK;
294 if (type == AO_LISP_OTHER)
295 return ao_lisp_other_type(ao_lisp_poly_other(poly));
299 static inline struct ao_lisp_cons *
300 ao_lisp_poly_cons(ao_poly poly)
302 return ao_lisp_ref(poly);
305 static inline ao_poly
306 ao_lisp_cons_poly(struct ao_lisp_cons *cons)
308 return ao_lisp_poly(cons, AO_LISP_CONS);
312 ao_lisp_poly_int(ao_poly poly)
314 return (int) ((ao_signed_poly) poly >> AO_LISP_TYPE_SHIFT);
317 static inline ao_poly
318 ao_lisp_int_poly(int i)
320 return ((ao_poly) i << 2) | AO_LISP_INT;
324 ao_lisp_poly_string(ao_poly poly)
326 return ao_lisp_ref(poly);
329 static inline ao_poly
330 ao_lisp_string_poly(char *s)
332 return ao_lisp_poly(s, AO_LISP_STRING);
335 static inline struct ao_lisp_atom *
336 ao_lisp_poly_atom(ao_poly poly)
338 return ao_lisp_ref(poly);
341 static inline ao_poly
342 ao_lisp_atom_poly(struct ao_lisp_atom *a)
344 return ao_lisp_poly(a, AO_LISP_OTHER);
347 static inline struct ao_lisp_builtin *
348 ao_lisp_poly_builtin(ao_poly poly)
350 return ao_lisp_ref(poly);
353 static inline ao_poly
354 ao_lisp_builtin_poly(struct ao_lisp_builtin *b)
356 return ao_lisp_poly(b, AO_LISP_OTHER);
359 /* memory functions */
360 /* returns 1 if the object was already marked */
362 ao_lisp_mark(const struct ao_lisp_type *type, void *addr);
364 /* returns 1 if the object was already marked */
366 ao_lisp_mark_memory(void *addr, int size);
369 ao_lisp_move_map(void *addr);
371 /* returns 1 if the object was already moved */
373 ao_lisp_move(const struct ao_lisp_type *type, void **ref);
375 /* returns 1 if the object was already moved */
377 ao_lisp_move_memory(void **ref, int size);
380 ao_lisp_alloc(int size);
383 ao_lisp_collect(void);
386 ao_lisp_root_add(const struct ao_lisp_type *type, void *addr);
389 ao_lisp_root_poly_add(ao_poly *p);
392 ao_lisp_root_clear(void *addr);
395 extern const struct ao_lisp_type ao_lisp_cons_type;
397 struct ao_lisp_cons *
398 ao_lisp_cons_cons(ao_poly car, struct ao_lisp_cons *cdr);
401 ao_lisp_cons_print(ao_poly);
404 ao_lisp_cons_patom(ao_poly);
407 extern const struct ao_lisp_type ao_lisp_string_type;
410 ao_lisp_string_new(int len);
413 ao_lisp_string_copy(char *a);
416 ao_lisp_string_cat(char *a, char *b);
419 ao_lisp_string_print(ao_poly s);
422 ao_lisp_string_patom(ao_poly s);
425 extern const struct ao_lisp_type ao_lisp_atom_type;
427 extern struct ao_lisp_atom *ao_lisp_atoms;
428 extern struct ao_lisp_frame *ao_lisp_frame_global;
429 extern struct ao_lisp_frame *ao_lisp_frame_current;
432 ao_lisp_atom_print(ao_poly a);
434 struct ao_lisp_atom *
435 ao_lisp_atom_intern(char *name);
438 ao_lisp_atom_get(ao_poly atom);
441 ao_lisp_atom_set(ao_poly atom, ao_poly val);
445 ao_lisp_int_print(ao_poly i);
449 ao_lisp_poly_print(ao_poly p);
452 ao_lisp_poly_patom(ao_poly p);
455 ao_lisp_poly_mark(ao_poly p, uint8_t note_cons);
457 /* returns 1 if the object has already been moved */
459 ao_lisp_poly_move(ao_poly *p, uint8_t note_cons);
464 ao_lisp_eval(ao_poly p);
467 ao_lisp_set_cond(struct ao_lisp_cons *cons);
471 ao_lisp_builtin_print(ao_poly b);
473 extern const struct ao_lisp_type ao_lisp_builtin_type;
475 /* Check argument count */
477 ao_lisp_check_argc(ao_poly name, struct ao_lisp_cons *cons, int min, int max);
479 /* Check argument type */
481 ao_lisp_check_argt(ao_poly name, struct ao_lisp_cons *cons, int argc, int type, int nil_ok);
483 /* Fetch an arg (nil if off the end) */
485 ao_lisp_arg(struct ao_lisp_cons *cons, int argc);
488 ao_lisp_args_name(uint8_t args);
496 ao_lisp_read_eval_print(void);
499 extern const struct ao_lisp_type ao_lisp_frame_type;
502 ao_lisp_frame_ref(struct ao_lisp_frame *frame, ao_poly atom);
504 struct ao_lisp_frame *
505 ao_lisp_frame_new(int num);
507 struct ao_lisp_frame *
508 ao_lisp_frame_add(struct ao_lisp_frame *frame, ao_poly atom, ao_poly val);
511 ao_lisp_frame_print(ao_poly p);
514 extern const struct ao_lisp_type ao_lisp_lambda_type;
516 struct ao_lisp_lambda *
517 ao_lisp_lambda_new(ao_poly cons);
520 ao_lisp_lambda_print(ao_poly lambda);
523 ao_lisp_lambda(struct ao_lisp_cons *cons);
526 ao_lisp_lexpr(struct ao_lisp_cons *cons);
529 ao_lisp_nlambda(struct ao_lisp_cons *cons);
532 ao_lisp_macro(struct ao_lisp_cons *cons);
535 ao_lisp_lambda_eval(struct ao_lisp_lambda *lambda,
536 struct ao_lisp_cons *cons);
541 ao_lisp_stack_print(void);
544 ao_lisp_error(int error, char *format, ...);
546 /* debugging macros */
550 int ao_lisp_stack_depth;
552 #define DBG_INDENT() do { int _s; for(_s = 0; _s < ao_lisp_stack_depth; _s++) printf(" "); } while(0)
553 #define DBG_IN() (++ao_lisp_stack_depth)
554 #define DBG_OUT() (--ao_lisp_stack_depth)
555 #define DBG_RESET() (ao_lisp_stack_depth = 0)
556 #define DBG(...) printf(__VA_ARGS__)
557 #define DBGI(...) do { DBG("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
558 #define DBG_CONS(a) ao_lisp_cons_print(ao_lisp_cons_poly(a))
559 #define DBG_POLY(a) ao_lisp_poly_print(a)
560 #define OFFSET(a) ((a) ? (int) ((uint8_t *) a - ao_lisp_pool) : -1)
561 #define DBG_STACK() ao_lisp_stack_print()
575 #endif /* _AO_LISP_H_ */