altos: Add lisp reader
[fw/altos] / src / lisp / ao_lisp.h
1 /*
2  * Copyright © 2016 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  */
14
15 #ifndef _AO_LISP_H_
16 #define _AO_LISP_H_
17
18 #include <stdint.h>
19 #include <string.h>
20 #include <stdio.h>
21
22
23 # define AO_LISP_CONS   0
24 # define AO_LISP_INT    1
25 # define AO_LISP_STRING 2
26 # define AO_LISP_OTHER  3
27
28 # define AO_LISP_ATOM           4
29 # define AO_LISP_BUILTIN        5
30
31 # define AO_LISP_NIL    0
32
33 #define AO_LISP_POOL    1024
34 #define AO_LISP_ROOT    16
35
36 static inline void *ao_lisp_set_ref(void *addr) {
37         return (void *) ((intptr_t)addr | 1);
38 }
39
40 static inline void *ao_lisp_clear_ref(void *addr) {
41         return (void *) ((intptr_t)addr & ~1);
42 }
43
44 extern uint8_t  ao_lisp_pool[AO_LISP_POOL];
45
46 struct ao_lisp_mem_type {
47         void    (*mark)(void *addr);
48         int     (*size)(void *addr);
49         void    (*move)(void *addr);
50 };
51
52 typedef intptr_t        ao_lisp_poly;
53
54 struct ao_lisp_cons {
55         ao_lisp_poly            car;
56         struct ao_lisp_cons     *cdr;
57 };
58
59 struct ao_lisp_atom {
60         uint8_t                 type;
61         ao_lisp_poly            val;
62         struct ao_lisp_atom     *next;
63         char                    name[];
64 };
65
66 #define AO_LISP_ATOM_CONST      ((struct ao_lisp_atom *) (intptr_t) 1)
67
68 extern const struct ao_lisp_atom *ao_lisp_builtins[];
69
70 struct ao_lisp_builtin {
71         uint8_t                 type;
72         ao_lisp_poly            (*func)(struct ao_lisp_cons *cons);
73         char                    name[];
74 };
75
76 static inline void *
77 ao_lisp_poly_other(ao_lisp_poly poly) {
78         return (void *) (poly - AO_LISP_OTHER);
79 }
80
81 static const inline ao_lisp_poly
82 ao_lisp_other_poly(const void *other)
83 {
84         return (ao_lisp_poly) other + AO_LISP_OTHER;
85 }
86
87 #define AO_LISP_OTHER_POLY(other) ((ao_lisp_poly)(other) + AO_LISP_OTHER)
88
89 static inline int ao_lisp_poly_type(ao_lisp_poly poly) {
90         int     type = poly & 3;
91         if (type == AO_LISP_OTHER)
92                 return *((uint8_t *) ao_lisp_poly_other(poly));
93         return type;
94 }
95
96 static inline struct ao_lisp_cons *
97 ao_lisp_poly_cons(ao_lisp_poly poly)
98 {
99         return (struct ao_lisp_cons *) (poly - AO_LISP_CONS);
100 }
101
102 static inline ao_lisp_poly
103 ao_lisp_cons_poly(struct ao_lisp_cons *cons)
104 {
105         return (ao_lisp_poly) cons + AO_LISP_CONS;
106 }
107
108 static inline int
109 ao_lisp_poly_int(ao_lisp_poly poly)
110 {
111         return (int) (poly >> 2);
112 }
113
114 static inline ao_lisp_poly
115 ao_lisp_int_poly(int i)
116 {
117         return ((ao_lisp_poly) i << 2) + AO_LISP_INT;
118 }
119
120 static inline char *
121 ao_lisp_poly_string(ao_lisp_poly poly)
122 {
123         return (char *) (poly - AO_LISP_STRING);
124 }
125
126 static inline ao_lisp_poly
127 ao_lisp_string_poly(char *s) {
128         return (ao_lisp_poly) s + AO_LISP_STRING;
129 }
130
131 static inline struct ao_lisp_atom *
132 ao_lisp_poly_atom(ao_lisp_poly poly)
133 {
134         return (struct ao_lisp_atom *) (poly - AO_LISP_OTHER);
135 }
136
137 static inline ao_lisp_poly
138 ao_lisp_atom_poly(struct ao_lisp_atom *a)
139 {
140         return (ao_lisp_poly) a + AO_LISP_OTHER;
141 }
142
143 static inline struct ao_lisp_builtin *
144 ao_lisp_poly_builtin(ao_lisp_poly poly)
145 {
146         return (struct ao_lisp_builtin *) (poly - AO_LISP_OTHER);
147 }
148
149 static inline ao_lisp_poly
150 ao_lisp_builtin_poly(struct ao_lisp_builtin *b)
151 {
152         return (ao_lisp_poly) b + AO_LISP_OTHER;
153 }
154
155 /* memory functions */
156
157 void
158 ao_lisp_mark(const struct ao_lisp_mem_type *type, void *addr);
159
160 /* returns 1 if the object was already marked */
161 int
162 ao_lisp_mark_memory(void *addr, int size);
163
164 void *
165 ao_lisp_move(const struct ao_lisp_mem_type *type, void *addr);
166
167 /* returns NULL if the object was already moved */
168 void *
169 ao_lisp_move_memory(void *addr, int size);
170
171 void *
172 ao_lisp_alloc(int size);
173
174 int
175 ao_lisp_root_add(const struct ao_lisp_mem_type *type, void *addr);
176
177 void
178 ao_lisp_root_clear(void *addr);
179
180 /* cons */
181 extern const struct ao_lisp_mem_type ao_lisp_cons_type;
182
183 struct ao_lisp_cons *
184 ao_lisp_cons(ao_lisp_poly car, struct ao_lisp_cons *cdr);
185
186 void
187 ao_lisp_cons_print(struct ao_lisp_cons *cons);
188
189 /* string */
190 extern const struct ao_lisp_mem_type ao_lisp_string_type;
191
192 char *
193 ao_lisp_string_new(int len);
194
195 char *
196 ao_lisp_string_copy(char *a);
197
198 char *
199 ao_lisp_string_cat(char *a, char *b);
200
201 void
202 ao_lisp_string_print(char *s);
203
204 /* atom */
205 extern const struct ao_lisp_mem_type ao_lisp_atom_type;
206
207 void
208 ao_lisp_atom_init(void);
209
210 void
211 ao_lisp_atom_print(struct ao_lisp_atom *atom);
212
213 struct ao_lisp_atom *
214 ao_lisp_atom_intern(char *name);
215
216 /* int */
217 void
218 ao_lisp_int_print(int i);
219
220 /* prim */
221 ao_lisp_poly
222 ao_lisp_poly_print(ao_lisp_poly p);
223
224 void
225 ao_lisp_poly_mark(ao_lisp_poly p);
226
227 ao_lisp_poly
228 ao_lisp_poly_move(ao_lisp_poly p);
229
230 /* eval */
231 ao_lisp_poly
232 ao_lisp_eval(ao_lisp_poly p);
233
234 /* builtin */
235 void
236 ao_lisp_builtin_print(struct ao_lisp_builtin *b);
237
238 /* read */
239 ao_lisp_poly
240 ao_lisp_read(void);
241
242 #endif /* _AO_LISP_H_ */