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.
21 #include <ao_lisp_os.h>
23 #ifdef AO_LISP_MAKE_CONST
24 #define AO_LISP_POOL_CONST 16384
25 extern uint8_t ao_lisp_const[AO_LISP_POOL_CONST];
26 #define ao_lisp_pool ao_lisp_const
27 #define AO_LISP_POOL AO_LISP_POOL_CONST
29 #define _atom(n) ao_lisp_atom_poly(ao_lisp_atom_intern(n))
31 #define _ao_lisp_atom_quote _atom("quote")
32 #define _ao_lisp_atom_set _atom("set")
33 #define _ao_lisp_atom_setq _atom("setq")
34 #define _ao_lisp_atom_t _atom("t")
35 #define _ao_lisp_atom_car _atom("car")
36 #define _ao_lisp_atom_cdr _atom("cdr")
37 #define _ao_lisp_atom_cons _atom("cons")
38 #define _ao_lisp_atom_last _atom("last")
39 #define _ao_lisp_atom_cond _atom("cond")
40 #define _ao_lisp_atom_lambda _atom("lambda")
41 #define _ao_lisp_atom_led _atom("led")
42 #define _ao_lisp_atom_delay _atom("delay")
44 #include "ao_lisp_const.h"
46 #define AO_LISP_POOL 16384
48 extern uint8_t ao_lisp_pool[AO_LISP_POOL];
52 #define AO_LISP_CONS 0
54 #define AO_LISP_STRING 2
55 #define AO_LISP_OTHER 3
57 #define AO_LISP_TYPE_MASK 0x0003
58 #define AO_LISP_TYPE_SHIFT 2
59 #define AO_LISP_REF_MASK 0x7ffc
60 #define AO_LISP_CONST 0x8000
62 /* These have a type value at the start of the struct */
63 #define AO_LISP_ATOM 4
64 #define AO_LISP_BUILTIN 5
65 #define AO_LISP_FRAME 6
66 #define AO_LISP_LAMBDA 7
67 #define AO_LISP_NUM_TYPE 8
71 extern uint16_t ao_lisp_top;
73 #define AO_LISP_OOM 0x01
74 #define AO_LISP_DIVIDE_BY_ZERO 0x02
75 #define AO_LISP_INVALID 0x04
76 #define AO_LISP_UNDEFINED 0x08
78 extern uint8_t ao_lisp_exception;
80 typedef uint16_t ao_poly;
81 typedef int16_t ao_signed_poly;
84 ao_lisp_is_const(ao_poly poly) {
85 return poly & AO_LISP_CONST;
88 #define AO_LISP_POOL_BASE (ao_lisp_pool - 4)
89 #define AO_LISP_CONST_BASE (ao_lisp_const - 4)
91 #define AO_LISP_IS_CONST(a) (ao_lisp_const <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_const + AO_LISP_POOL_CONST)
92 #define AO_LISP_IS_POOL(a) (ao_lisp_pool <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_pool + AO_LISP_POOL)
95 ao_lisp_ref(ao_poly poly) {
98 if (poly == AO_LISP_NIL)
100 if (poly & AO_LISP_CONST)
101 return (void *) (AO_LISP_CONST_BASE + (poly & AO_LISP_REF_MASK));
102 return (void *) (AO_LISP_POOL_BASE + (poly & AO_LISP_REF_MASK));
105 static inline ao_poly
106 ao_lisp_poly(const void *addr, ao_poly type) {
107 const uint8_t *a = addr;
110 if (AO_LISP_IS_CONST(a))
111 return AO_LISP_CONST | (a - AO_LISP_CONST_BASE) | type;
112 return (a - AO_LISP_POOL_BASE) | type;
115 struct ao_lisp_type {
116 int (*size)(void *addr);
117 void (*mark)(void *addr);
118 void (*move)(void *addr);
121 struct ao_lisp_cons {
126 struct ao_lisp_atom {
138 struct ao_lisp_frame {
142 struct ao_lisp_val vals[];
145 static inline struct ao_lisp_frame *
146 ao_lisp_poly_frame(ao_poly poly) {
147 return ao_lisp_ref(poly);
150 static inline ao_poly
151 ao_lisp_frame_poly(struct ao_lisp_frame *frame) {
152 return ao_lisp_poly(frame, AO_LISP_OTHER);
156 eval_sexpr, /* Evaluate an sexpr */
164 struct ao_lisp_stack {
165 uint8_t state; /* enum eval_state */
166 ao_poly prev; /* previous stack frame */
167 ao_poly sexprs; /* expressions to evaluate */
168 ao_poly values; /* values computed */
169 ao_poly values_tail; /* end of the values list for easy appending */
170 ao_poly frame; /* current lookup frame */
171 ao_poly list; /* most recent function call */
174 static inline struct ao_lisp_stack *
175 ao_lisp_poly_stack(ao_poly p)
177 return ao_lisp_ref(p);
180 static inline ao_poly
181 ao_lisp_stack_poly(struct ao_lisp_stack *stack)
183 return ao_lisp_poly(stack, AO_LISP_OTHER);
186 extern struct ao_lisp_stack *ao_lisp_stack;
187 extern ao_poly ao_lisp_v;
189 #define AO_LISP_FUNC_LAMBDA 0
190 #define AO_LISP_FUNC_NLAMBDA 1
191 #define AO_LISP_FUNC_MACRO 2
192 #define AO_LISP_FUNC_LEXPR 3
194 struct ao_lisp_builtin {
200 enum ao_lisp_builtin_id {
224 builtin_greater_equal,
230 typedef ao_poly (*ao_lisp_func_t)(struct ao_lisp_cons *cons);
232 extern const ao_lisp_func_t ao_lisp_builtins[];
234 static inline ao_lisp_func_t
235 ao_lisp_func(struct ao_lisp_builtin *b)
237 return ao_lisp_builtins[b->func];
240 struct ao_lisp_lambda {
247 static inline struct ao_lisp_lambda *
248 ao_lisp_poly_lambda(ao_poly poly)
250 return ao_lisp_ref(poly);
253 static inline ao_poly
254 ao_lisp_lambda_poly(struct ao_lisp_lambda *lambda)
256 return ao_lisp_poly(lambda, AO_LISP_OTHER);
260 ao_lisp_poly_other(ao_poly poly) {
261 return ao_lisp_ref(poly);
264 static inline uint8_t
265 ao_lisp_other_type(void *other) {
266 return *((uint8_t *) other);
269 static inline ao_poly
270 ao_lisp_other_poly(const void *other)
272 return ao_lisp_poly(other, AO_LISP_OTHER);
276 ao_lisp_mem_round(int size)
278 return (size + 3) & ~3;
281 #define AO_LISP_OTHER_POLY(other) ((ao_poly)(other) + AO_LISP_OTHER)
283 static inline int ao_lisp_poly_base_type(ao_poly poly) {
284 return poly & AO_LISP_TYPE_MASK;
287 static inline int ao_lisp_poly_type(ao_poly poly) {
288 int type = poly & AO_LISP_TYPE_MASK;
289 if (type == AO_LISP_OTHER)
290 return ao_lisp_other_type(ao_lisp_poly_other(poly));
294 static inline struct ao_lisp_cons *
295 ao_lisp_poly_cons(ao_poly poly)
297 return ao_lisp_ref(poly);
300 static inline ao_poly
301 ao_lisp_cons_poly(struct ao_lisp_cons *cons)
303 return ao_lisp_poly(cons, AO_LISP_CONS);
307 ao_lisp_poly_int(ao_poly poly)
309 return (int) ((ao_signed_poly) poly >> AO_LISP_TYPE_SHIFT);
312 static inline ao_poly
313 ao_lisp_int_poly(int i)
315 return ((ao_poly) i << 2) | AO_LISP_INT;
319 ao_lisp_poly_string(ao_poly poly)
321 return ao_lisp_ref(poly);
324 static inline ao_poly
325 ao_lisp_string_poly(char *s)
327 return ao_lisp_poly(s, AO_LISP_STRING);
330 static inline struct ao_lisp_atom *
331 ao_lisp_poly_atom(ao_poly poly)
333 return ao_lisp_ref(poly);
336 static inline ao_poly
337 ao_lisp_atom_poly(struct ao_lisp_atom *a)
339 return ao_lisp_poly(a, AO_LISP_OTHER);
342 static inline struct ao_lisp_builtin *
343 ao_lisp_poly_builtin(ao_poly poly)
345 return ao_lisp_ref(poly);
348 static inline ao_poly
349 ao_lisp_builtin_poly(struct ao_lisp_builtin *b)
351 return ao_lisp_poly(b, AO_LISP_OTHER);
354 /* memory functions */
355 /* returns 1 if the object was already marked */
357 ao_lisp_mark(const struct ao_lisp_type *type, void *addr);
359 /* returns 1 if the object was already marked */
361 ao_lisp_mark_memory(void *addr, int size);
364 ao_lisp_move_map(void *addr);
366 /* returns 1 if the object was already moved */
368 ao_lisp_move(const struct ao_lisp_type *type, void **ref);
370 /* returns 1 if the object was already moved */
372 ao_lisp_move_memory(void **ref, int size);
375 ao_lisp_alloc(int size);
378 ao_lisp_collect(void);
381 ao_lisp_root_add(const struct ao_lisp_type *type, void *addr);
384 ao_lisp_root_poly_add(ao_poly *p);
387 ao_lisp_root_clear(void *addr);
390 extern const struct ao_lisp_type ao_lisp_cons_type;
392 struct ao_lisp_cons *
393 ao_lisp_cons_cons(ao_poly car, struct ao_lisp_cons *cdr);
396 ao_lisp_cons_print(ao_poly);
399 ao_lisp_cons_patom(ao_poly);
402 extern const struct ao_lisp_type ao_lisp_string_type;
405 ao_lisp_string_new(int len);
408 ao_lisp_string_copy(char *a);
411 ao_lisp_string_cat(char *a, char *b);
414 ao_lisp_string_print(ao_poly s);
417 ao_lisp_string_patom(ao_poly s);
420 extern const struct ao_lisp_type ao_lisp_atom_type;
422 extern struct ao_lisp_atom *ao_lisp_atoms;
423 extern struct ao_lisp_frame *ao_lisp_frame_global;
424 extern struct ao_lisp_frame *ao_lisp_frame_current;
427 ao_lisp_atom_print(ao_poly a);
429 struct ao_lisp_atom *
430 ao_lisp_atom_intern(char *name);
433 ao_lisp_atom_get(ao_poly atom);
436 ao_lisp_atom_set(ao_poly atom, ao_poly val);
440 ao_lisp_int_print(ao_poly i);
444 ao_lisp_poly_print(ao_poly p);
447 ao_lisp_poly_patom(ao_poly p);
450 ao_lisp_poly_mark(ao_poly p, uint8_t note_cons);
452 /* returns 1 if the object has already been moved */
454 ao_lisp_poly_move(ao_poly *p, uint8_t note_cons);
459 ao_lisp_eval(ao_poly p);
462 ao_lisp_set_cond(struct ao_lisp_cons *cons);
466 ao_lisp_builtin_print(ao_poly b);
468 extern const struct ao_lisp_type ao_lisp_builtin_type;
470 /* Check argument count */
472 ao_lisp_check_argc(ao_poly name, struct ao_lisp_cons *cons, int min, int max);
474 /* Check argument type */
476 ao_lisp_check_argt(ao_poly name, struct ao_lisp_cons *cons, int argc, int type, int nil_ok);
478 /* Fetch an arg (nil if off the end) */
480 ao_lisp_arg(struct ao_lisp_cons *cons, int argc);
483 ao_lisp_args_name(uint8_t args);
491 ao_lisp_read_eval_print(void);
494 extern const struct ao_lisp_type ao_lisp_frame_type;
497 ao_lisp_frame_ref(struct ao_lisp_frame *frame, ao_poly atom);
499 struct ao_lisp_frame *
500 ao_lisp_frame_new(int num);
502 struct ao_lisp_frame *
503 ao_lisp_frame_add(struct ao_lisp_frame *frame, ao_poly atom, ao_poly val);
506 ao_lisp_frame_print(ao_poly p);
509 extern const struct ao_lisp_type ao_lisp_lambda_type;
511 struct ao_lisp_lambda *
512 ao_lisp_lambda_new(ao_poly cons);
515 ao_lisp_lambda_print(ao_poly lambda);
518 ao_lisp_lambda(struct ao_lisp_cons *cons);
521 ao_lisp_lexpr(struct ao_lisp_cons *cons);
524 ao_lisp_nlambda(struct ao_lisp_cons *cons);
527 ao_lisp_macro(struct ao_lisp_cons *cons);
530 ao_lisp_lambda_eval(struct ao_lisp_lambda *lambda,
531 struct ao_lisp_cons *cons);
536 ao_lisp_stack_print(void);
539 ao_lisp_error(int error, char *format, ...);
541 /* debugging macros */
545 int ao_lisp_stack_depth;
547 #define DBG_INDENT() do { int _s; for(_s = 0; _s < ao_lisp_stack_depth; _s++) printf(" "); } while(0)
548 #define DBG_IN() (++ao_lisp_stack_depth)
549 #define DBG_OUT() (--ao_lisp_stack_depth)
550 #define DBG_RESET() (ao_lisp_stack_depth = 0)
551 #define DBG(...) printf(__VA_ARGS__)
552 #define DBGI(...) do { DBG("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
553 #define DBG_CONS(a) ao_lisp_cons_print(ao_lisp_cons_poly(a))
554 #define DBG_POLY(a) ao_lisp_poly_print(a)
555 #define OFFSET(a) ((a) ? (int) ((uint8_t *) a - ao_lisp_pool) : -1)
556 #define DBG_STACK() ao_lisp_stack_print()
558 ao_lisp_frames_dump(void)
560 struct ao_lisp_stack *s;
561 DBGI(".. current frame: "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
562 for (s = ao_lisp_stack; s; s = ao_lisp_poly_stack(s->prev)) {
563 DBGI(".. stack frame: "); DBG_POLY(s->frame); DBG("\n");
566 #define DBG_FRAMES() ao_lisp_frames_dump()
581 #endif /* _AO_LISP_H_ */