altos/lisp: Change GC move API
[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
19 static struct ao_lisp_builtin *
20 ao_lisp_make_builtin(enum ao_lisp_builtin_id func, int args) {
21         struct ao_lisp_builtin *b = ao_lisp_alloc(sizeof (struct ao_lisp_builtin));
22
23         b->type = AO_LISP_BUILTIN;
24         b->func = func;
25         b->args = args;
26         return b;
27 }
28
29 struct builtin_func {
30         char    *name;
31         int     args;
32         int     func;
33 };
34
35 struct builtin_func funcs[] = {
36         "car",          AO_LISP_LEXPR,  builtin_car,
37         "cdr",          AO_LISP_LEXPR,  builtin_cdr,
38         "cons",         AO_LISP_LEXPR,  builtin_cons,
39         "quote",        AO_LISP_NLAMBDA,builtin_quote,
40         "set",          AO_LISP_LEXPR,  builtin_set,
41         "setq",         AO_LISP_MACRO,  builtin_setq,
42         "cond",         AO_LISP_NLAMBDA,builtin_cond,
43         "print",        AO_LISP_LEXPR,  builtin_print,
44         "+",            AO_LISP_LEXPR,  builtin_plus,
45         "-",            AO_LISP_LEXPR,  builtin_minus,
46         "*",            AO_LISP_LEXPR,  builtin_times,
47         "/",            AO_LISP_LEXPR,  builtin_divide,
48         "%",            AO_LISP_LEXPR,  builtin_mod,
49         "=",            AO_LISP_LEXPR,  builtin_equal,
50         "<",            AO_LISP_LEXPR,  builtin_less,
51         ">",            AO_LISP_LEXPR,  builtin_greater,
52         "<=",           AO_LISP_LEXPR,  builtin_less_equal,
53         ">=",           AO_LISP_LEXPR,  builtin_greater_equal,
54 };
55
56 ao_poly
57 ao_lisp_set_cond(struct ao_lisp_cons *c)
58 {
59         (void) c;
60         return AO_LISP_NIL;
61 }
62
63 #define N_FUNC (sizeof funcs / sizeof funcs[0])
64
65 /* Syntactic atoms */
66 char *atoms[] = {
67         "lambda",
68         "nlambda",
69         "lexpr",
70         "macro"
71 };
72
73 #define N_ATOM (sizeof atoms / sizeof atoms[0])
74
75 struct ao_lisp_frame    *globals;
76
77 static int
78 is_atom(int offset)
79 {
80         struct ao_lisp_atom *a;
81
82         for (a = ao_lisp_atoms; a; a = ao_lisp_poly_atom(a->next))
83                 if (((uint8_t *) a->name - ao_lisp_const) == offset)
84                         return strlen(a->name);
85         return 0;
86 }
87
88 int
89 main(int argc, char **argv)
90 {
91         int     f, o, i;
92         ao_poly atom, val;
93         struct ao_lisp_atom     *a;
94         struct ao_lisp_builtin  *b;
95         int     in_atom;
96
97         printf("/*\n");
98         printf(" * Generated file, do not edit\n");
99         ao_lisp_root_add(&ao_lisp_frame_type, &globals);
100         globals = ao_lisp_frame_new(0);
101         for (f = 0; f < N_FUNC; f++) {
102                 b = ao_lisp_make_builtin(funcs[f].func, funcs[f].args);
103                 a = ao_lisp_atom_intern(funcs[f].name);
104                 globals = ao_lisp_frame_add(globals, ao_lisp_atom_poly(a), ao_lisp_builtin_poly(b));
105         }
106
107         /* atoms for syntax */
108         for (i = 0; i < N_ATOM; i++)
109                 (void) ao_lisp_atom_intern(atoms[i]);
110
111         /* boolean constants */
112         a = ao_lisp_atom_intern("nil");
113         globals = ao_lisp_frame_add(globals, ao_lisp_atom_poly(a), AO_LISP_NIL);
114         a = ao_lisp_atom_intern("t");
115         globals = ao_lisp_frame_add(globals, ao_lisp_atom_poly(a), ao_lisp_atom_poly(a));
116
117         for (;;) {
118                 atom = ao_lisp_read();
119                 if (!atom)
120                         break;
121                 val = ao_lisp_read();
122                 if (!val)
123                         break;
124                 if (ao_lisp_poly_type(atom) != AO_LISP_ATOM) {
125                         fprintf(stderr, "input must be atom val pairs\n");
126                         exit(1);
127                 }
128                 globals = ao_lisp_frame_add(globals, atom, val);
129         }
130
131         /* Reduce to referenced values */
132         ao_lisp_collect();
133         printf(" */\n");
134
135         printf("#define AO_LISP_POOL_CONST %d\n", ao_lisp_top);
136         printf("extern const uint8_t ao_lisp_const[AO_LISP_POOL_CONST] __attribute__((aligned(4)));\n");
137         printf("#define ao_builtin_atoms 0x%04x\n", ao_lisp_atom_poly(ao_lisp_atoms));
138         printf("#define ao_builtin_frame 0x%04x\n", ao_lisp_frame_poly(globals));
139
140         for (a = ao_lisp_atoms; a; a = ao_lisp_poly_atom(a->next)) {
141                 char    *n = a->name, c;
142                 printf ("#define _ao_lisp_atom_");
143                 while ((c = *n++)) {
144                         if (isalnum(c))
145                                 printf("%c", c);
146                         else
147                                 printf("%02x", c);
148                 }
149                 printf("  0x%04x\n", ao_lisp_atom_poly(a));
150         }
151         printf("#ifdef AO_LISP_CONST_BITS\n");
152         printf("const uint8_t ao_lisp_const[] = {");
153         for (o = 0; o < ao_lisp_top; o++) {
154                 uint8_t c;
155                 if ((o & 0xf) == 0)
156                         printf("\n\t");
157                 else
158                         printf(" ");
159                 c = ao_lisp_const[o];
160                 if (!in_atom)
161                         in_atom = is_atom(o);
162                 if (in_atom) {
163                         printf (" '%c',", c);
164                         in_atom--;
165                 } else {
166                         printf("0x%02x,", c);
167                 }
168         }
169         printf("\n};\n");
170         printf("#endif /* AO_LISP_CONST_BITS */\n");
171 }