altos/lisp: split set/def. Add def support to lambdas
[fw/altos] / src / lisp / ao_lisp_make_const.c
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 #include "ao_lisp.h"
16 #include <stdlib.h>
17 #include <ctype.h>
18 #include <unistd.h>
19 #include <getopt.h>
20
21 static struct ao_lisp_builtin *
22 ao_lisp_make_builtin(enum ao_lisp_builtin_id func, int args) {
23         struct ao_lisp_builtin *b = ao_lisp_alloc(sizeof (struct ao_lisp_builtin));
24
25         b->type = AO_LISP_BUILTIN;
26         b->func = func;
27         b->args = args;
28         return b;
29 }
30
31 struct builtin_func {
32         char    *name;
33         int     args;
34         enum ao_lisp_builtin_id func;
35 };
36
37 #define AO_LISP_BUILTIN_CONSTS
38 #include "ao_lisp_builtin.h"
39
40 #define N_FUNC (sizeof funcs / sizeof funcs[0])
41
42 struct ao_lisp_frame    *globals;
43
44 static int
45 is_atom(int offset)
46 {
47         struct ao_lisp_atom *a;
48
49         for (a = ao_lisp_atoms; a; a = ao_lisp_poly_atom(a->next))
50                 if (((uint8_t *) a->name - ao_lisp_const) == offset)
51                         return strlen(a->name);
52         return 0;
53 }
54
55 #define AO_FEC_CRC_INIT 0xffff
56
57 static inline uint16_t
58 ao_fec_crc_byte(uint8_t byte, uint16_t crc)
59 {
60         uint8_t bit;
61
62         for (bit = 0; bit < 8; bit++) {
63                 if (((crc & 0x8000) >> 8) ^ (byte & 0x80))
64                         crc = (crc << 1) ^ 0x8005;
65                 else
66                         crc = (crc << 1);
67                 byte <<= 1;
68         }
69         return crc;
70 }
71
72 uint16_t
73 ao_fec_crc(const uint8_t *bytes, uint8_t len)
74 {
75         uint16_t        crc = AO_FEC_CRC_INIT;
76
77         while (len--)
78                 crc = ao_fec_crc_byte(*bytes++, crc);
79         return crc;
80 }
81
82 struct ao_lisp_macro_stack {
83         struct ao_lisp_macro_stack *next;
84         ao_poly p;
85 };
86
87 struct ao_lisp_macro_stack *macro_stack;
88
89 int
90 ao_lisp_macro_push(ao_poly p)
91 {
92         struct ao_lisp_macro_stack *m = macro_stack;
93
94         while (m) {
95                 if (m->p == p)
96                         return 1;
97                 m = m->next;
98         }
99         m = malloc (sizeof (struct ao_lisp_macro_stack));
100         m->p = p;
101         m->next = macro_stack;
102         macro_stack = m;
103         return 0;
104 }
105
106 void
107 ao_lisp_macro_pop(void)
108 {
109         struct ao_lisp_macro_stack *m = macro_stack;
110
111         macro_stack = m->next;
112         free(m);
113 }
114
115 #define DBG_MACRO 0
116 #if DBG_MACRO
117 int macro_scan_depth;
118
119 void indent(void)
120 {
121         int i;
122         for (i = 0; i < macro_scan_depth; i++)
123                 printf("  ");
124 }
125 #define MACRO_DEBUG(a)  a
126 #else
127 #define MACRO_DEBUG(a)
128 #endif
129
130 ao_poly
131 ao_has_macro(ao_poly p);
132
133 ao_poly
134 ao_macro_test_get(ao_poly atom)
135 {
136         ao_poly *ref = ao_lisp_atom_ref(atom);
137         if (ref)
138                 return *ref;
139         return AO_LISP_NIL;
140 }
141
142 ao_poly
143 ao_is_macro(ao_poly p)
144 {
145         struct ao_lisp_builtin  *builtin;
146         struct ao_lisp_lambda   *lambda;
147         ao_poly ret;
148
149         MACRO_DEBUG(indent(); printf ("is macro "); ao_lisp_poly_write(p); printf("\n"); ++macro_scan_depth);
150         switch (ao_lisp_poly_type(p)) {
151         case AO_LISP_ATOM:
152                 if (ao_lisp_macro_push(p))
153                         ret = AO_LISP_NIL;
154                 else {
155                         if (ao_is_macro(ao_macro_test_get(p)))
156                                 ret = p;
157                         else
158                                 ret = AO_LISP_NIL;
159                         ao_lisp_macro_pop();
160                 }
161                 break;
162         case AO_LISP_CONS:
163                 ret = ao_has_macro(p);
164                 break;
165         case AO_LISP_BUILTIN:
166                 builtin = ao_lisp_poly_builtin(p);
167                 if ((builtin->args & AO_LISP_FUNC_MASK) == AO_LISP_FUNC_MACRO)
168                         ret = p;
169                 else
170                         ret = 0;
171                 break;
172
173         case AO_LISP_LAMBDA:
174                 lambda = ao_lisp_poly_lambda(p);
175                 if (lambda->args == AO_LISP_FUNC_MACRO)
176                         ret = p;
177                 else
178                         ret = ao_has_macro(lambda->code);
179                 break;
180         default:
181                 ret = AO_LISP_NIL;
182                 break;
183         }
184         MACRO_DEBUG(--macro_scan_depth; indent(); printf ("... "); ao_lisp_poly_write(ret); printf("\n"));
185         return ret;
186 }
187
188 ao_poly
189 ao_has_macro(ao_poly p)
190 {
191         struct ao_lisp_cons     *cons;
192         struct ao_lisp_lambda   *lambda;
193         ao_poly                 m;
194
195         if (p == AO_LISP_NIL)
196                 return AO_LISP_NIL;
197
198         MACRO_DEBUG(indent(); printf("has macro "); ao_lisp_poly_write(p); printf("\n"); ++macro_scan_depth);
199         switch (ao_lisp_poly_type(p)) {
200         case AO_LISP_LAMBDA:
201                 lambda = ao_lisp_poly_lambda(p);
202                 p = ao_has_macro(lambda->code);
203                 break;
204         case AO_LISP_CONS:
205                 cons = ao_lisp_poly_cons(p);
206                 if ((p = ao_is_macro(cons->car)))
207                         break;
208
209                 cons = ao_lisp_poly_cons(cons->cdr);
210                 p = AO_LISP_NIL;
211                 while (cons) {
212                         m = ao_has_macro(cons->car);
213                         if (m) {
214                                 p = m;
215                                 break;
216                         }
217                         cons = ao_lisp_poly_cons(cons->cdr);
218                 }
219                 break;
220
221         default:
222                 p = AO_LISP_NIL;
223                 break;
224         }
225         MACRO_DEBUG(--macro_scan_depth; indent(); printf("... "); ao_lisp_poly_write(p); printf("\n"));
226         return p;
227 }
228
229 int
230 ao_lisp_read_eval_abort(void)
231 {
232         ao_poly in, out = AO_LISP_NIL;
233         for(;;) {
234                 in = ao_lisp_read();
235                 if (in == _ao_lisp_atom_eof)
236                         break;
237                 out = ao_lisp_eval(in);
238                 if (ao_lisp_exception)
239                         return 0;
240                 ao_lisp_poly_write(out);
241                 putchar ('\n');
242         }
243         return 1;
244 }
245
246 static FILE     *in;
247 static FILE     *out;
248
249 int
250 ao_lisp_getc(void)
251 {
252         return getc(in);
253 }
254
255 static const struct option options[] = {
256         { .name = "out", .has_arg = 1, .val = 'o' },
257         { 0, 0, 0, 0 }
258 };
259
260 static void usage(char *program)
261 {
262         fprintf(stderr, "usage: %s [--out=<output>] [input]\n", program);
263         exit(1);
264 }
265
266 int
267 main(int argc, char **argv)
268 {
269         int     f, o;
270         ao_poly val;
271         struct ao_lisp_atom     *a;
272         struct ao_lisp_builtin  *b;
273         int     in_atom = 0;
274         char    *out_name = NULL;
275         int     c;
276         enum ao_lisp_builtin_id prev_func;
277
278         in = stdin;
279         out = stdout;
280
281         while ((c = getopt_long(argc, argv, "o:", options, NULL)) != -1) {
282                 switch (c) {
283                 case 'o':
284                         out_name = optarg;
285                         break;
286                 default:
287                         usage(argv[0]);
288                         break;
289                 }
290         }
291
292         ao_lisp_frame_init();
293
294         /* Boolean values #f and #t */
295         ao_lisp_bool_get(0);
296         ao_lisp_bool_get(1);
297
298         prev_func = _builtin_last;
299         for (f = 0; f < (int) N_FUNC; f++) {
300                 if (funcs[f].func != prev_func)
301                         b = ao_lisp_make_builtin(funcs[f].func, funcs[f].args);
302                 a = ao_lisp_atom_intern(funcs[f].name);
303                 ao_lisp_atom_def(ao_lisp_atom_poly(a),
304                                  ao_lisp_builtin_poly(b));
305         }
306
307         /* end of file value */
308         a = ao_lisp_atom_intern("eof");
309         ao_lisp_atom_def(ao_lisp_atom_poly(a),
310                          ao_lisp_atom_poly(a));
311
312         /* 'else' */
313         a = ao_lisp_atom_intern("else");
314
315         if (argv[optind]){
316                 in = fopen(argv[optind], "r");
317                 if (!in) {
318                         perror(argv[optind]);
319                         exit(1);
320                 }
321         }
322         if (!ao_lisp_read_eval_abort()) {
323                 fprintf(stderr, "eval failed\n");
324                 exit(1);
325         }
326
327         /* Reduce to referenced values */
328         ao_lisp_collect(AO_LISP_COLLECT_FULL);
329
330         for (f = 0; f < ao_lisp_frame_global->num; f++) {
331                 struct ao_lisp_frame_vals       *vals = ao_lisp_poly_frame_vals(ao_lisp_frame_global->vals);
332                 val = ao_has_macro(vals->vals[f].val);
333                 if (val != AO_LISP_NIL) {
334                         printf("error: function %s contains unresolved macro: ",
335                                ao_lisp_poly_atom(vals->vals[f].atom)->name);
336                         ao_lisp_poly_write(val);
337                         printf("\n");
338                         exit(1);
339                 }
340         }
341
342         if (out_name) {
343                 out = fopen(out_name, "w");
344                 if (!out) {
345                         perror(out_name);
346                         exit(1);
347                 }
348         }
349
350         fprintf(out, "/* Generated file, do not edit */\n\n");
351
352         fprintf(out, "#define AO_LISP_POOL_CONST %d\n", ao_lisp_top);
353         fprintf(out, "extern const uint8_t ao_lisp_const[AO_LISP_POOL_CONST] __attribute__((aligned(4)));\n");
354         fprintf(out, "#define ao_builtin_atoms 0x%04x\n", ao_lisp_atom_poly(ao_lisp_atoms));
355         fprintf(out, "#define ao_builtin_frame 0x%04x\n", ao_lisp_frame_poly(ao_lisp_frame_global));
356         fprintf(out, "#define ao_lisp_const_checksum ((uint16_t) 0x%04x)\n", ao_fec_crc(ao_lisp_const, ao_lisp_top));
357
358         fprintf(out, "#define _ao_lisp_bool_false 0x%04x\n", ao_lisp_bool_poly(ao_lisp_false));
359         fprintf(out, "#define _ao_lisp_bool_true 0x%04x\n", ao_lisp_bool_poly(ao_lisp_true));
360
361         for (a = ao_lisp_atoms; a; a = ao_lisp_poly_atom(a->next)) {
362                 char    *n = a->name, c;
363                 fprintf(out, "#define _ao_lisp_atom_");
364                 while ((c = *n++)) {
365                         if (isalnum(c))
366                                 fprintf(out, "%c", c);
367                         else
368                                 fprintf(out, "%02x", c);
369                 }
370                 fprintf(out, "  0x%04x\n", ao_lisp_atom_poly(a));
371         }
372         fprintf(out, "#ifdef AO_LISP_CONST_BITS\n");
373         fprintf(out, "const uint8_t ao_lisp_const[AO_LISP_POOL_CONST] __attribute((aligned(4))) = {");
374         for (o = 0; o < ao_lisp_top; o++) {
375                 uint8_t c;
376                 if ((o & 0xf) == 0)
377                         fprintf(out, "\n\t");
378                 else
379                         fprintf(out, " ");
380                 c = ao_lisp_const[o];
381                 if (!in_atom)
382                         in_atom = is_atom(o);
383                 if (in_atom) {
384                         fprintf(out, " '%c',", c);
385                         in_atom--;
386                 } else {
387                         fprintf(out, "0x%02x,", c);
388                 }
389         }
390         fprintf(out, "\n};\n");
391         fprintf(out, "#endif /* AO_LISP_CONST_BITS */\n");
392         exit(0);
393 }