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.
23 # define AO_LISP_CONS 0
24 # define AO_LISP_INT 1
25 # define AO_LISP_STRING 2
26 # define AO_LISP_OTHER 3
28 # define AO_LISP_ATOM 4
29 # define AO_LISP_BUILTIN 5
31 # define AO_LISP_NIL 0
33 #define AO_LISP_POOL 1024
34 #define AO_LISP_ROOT 16
36 static inline void *ao_lisp_set_ref(void *addr) {
37 return (void *) ((intptr_t)addr | 1);
40 static inline void *ao_lisp_clear_ref(void *addr) {
41 return (void *) ((intptr_t)addr & ~1);
44 extern uint8_t ao_lisp_pool[AO_LISP_POOL];
46 struct ao_lisp_mem_type {
47 void (*mark)(void *addr);
48 int (*size)(void *addr);
49 void (*move)(void *addr);
52 typedef intptr_t ao_lisp_poly;
56 struct ao_lisp_cons *cdr;
62 struct ao_lisp_atom *next;
66 #define AO_LISP_ATOM_CONST ((struct ao_lisp_atom *) (intptr_t) 1)
68 extern const struct ao_lisp_atom *ao_lisp_builtins[];
70 struct ao_lisp_builtin {
72 ao_lisp_poly (*func)(struct ao_lisp_cons *cons);
77 ao_lisp_poly_other(ao_lisp_poly poly) {
78 return (void *) (poly - AO_LISP_OTHER);
81 static const inline ao_lisp_poly
82 ao_lisp_other_poly(const void *other)
84 return (ao_lisp_poly) other + AO_LISP_OTHER;
87 #define AO_LISP_OTHER_POLY(other) ((ao_lisp_poly)(other) + AO_LISP_OTHER)
89 static inline int ao_lisp_poly_type(ao_lisp_poly poly) {
91 if (type == AO_LISP_OTHER)
92 return *((uint8_t *) ao_lisp_poly_other(poly));
96 static inline struct ao_lisp_cons *
97 ao_lisp_poly_cons(ao_lisp_poly poly)
99 return (struct ao_lisp_cons *) (poly - AO_LISP_CONS);
102 static inline ao_lisp_poly
103 ao_lisp_cons_poly(struct ao_lisp_cons *cons)
105 return (ao_lisp_poly) cons + AO_LISP_CONS;
109 ao_lisp_poly_int(ao_lisp_poly poly)
111 return (int) (poly >> 2);
114 static inline ao_lisp_poly
115 ao_lisp_int_poly(int i)
117 return ((ao_lisp_poly) i << 2) + AO_LISP_INT;
121 ao_lisp_poly_string(ao_lisp_poly poly)
123 return (char *) (poly - AO_LISP_STRING);
126 static inline ao_lisp_poly
127 ao_lisp_string_poly(char *s) {
128 return (ao_lisp_poly) s + AO_LISP_STRING;
131 static inline struct ao_lisp_atom *
132 ao_lisp_poly_atom(ao_lisp_poly poly)
134 return (struct ao_lisp_atom *) (poly - AO_LISP_OTHER);
137 static inline ao_lisp_poly
138 ao_lisp_atom_poly(struct ao_lisp_atom *a)
140 return (ao_lisp_poly) a + AO_LISP_OTHER;
143 static inline struct ao_lisp_builtin *
144 ao_lisp_poly_builtin(ao_lisp_poly poly)
146 return (struct ao_lisp_builtin *) (poly - AO_LISP_OTHER);
149 static inline ao_lisp_poly
150 ao_lisp_builtin_poly(struct ao_lisp_builtin *b)
152 return (ao_lisp_poly) b + AO_LISP_OTHER;
155 /* memory functions */
158 ao_lisp_mark(const struct ao_lisp_mem_type *type, void *addr);
160 /* returns 1 if the object was already marked */
162 ao_lisp_mark_memory(void *addr, int size);
165 ao_lisp_move(const struct ao_lisp_mem_type *type, void *addr);
167 /* returns NULL if the object was already moved */
169 ao_lisp_move_memory(void *addr, int size);
172 ao_lisp_alloc(int size);
175 ao_lisp_root_add(const struct ao_lisp_mem_type *type, void *addr);
178 ao_lisp_root_clear(void *addr);
181 extern const struct ao_lisp_mem_type ao_lisp_cons_type;
183 struct ao_lisp_cons *
184 ao_lisp_cons(ao_lisp_poly car, struct ao_lisp_cons *cdr);
187 ao_lisp_cons_print(struct ao_lisp_cons *cons);
190 extern const struct ao_lisp_mem_type ao_lisp_string_type;
193 ao_lisp_string_new(int len);
196 ao_lisp_string_copy(char *a);
199 ao_lisp_string_cat(char *a, char *b);
202 ao_lisp_string_print(char *s);
205 extern const struct ao_lisp_mem_type ao_lisp_atom_type;
208 ao_lisp_atom_init(void);
211 ao_lisp_atom_print(struct ao_lisp_atom *atom);
213 struct ao_lisp_atom *
214 ao_lisp_atom_intern(char *name);
218 ao_lisp_int_print(int i);
222 ao_lisp_poly_print(ao_lisp_poly p);
225 ao_lisp_poly_mark(ao_lisp_poly p);
228 ao_lisp_poly_move(ao_lisp_poly p);
232 ao_lisp_eval(ao_lisp_poly p);
236 ao_lisp_builtin_print(struct ao_lisp_builtin *b);
242 #endif /* _AO_LISP_H_ */