altos/lisp: use regular read-eval-print loop for make const
[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         "eval",         AO_LISP_FUNC_LAMBDA,    builtin_eval,
37         "read",         AO_LISP_FUNC_LAMBDA,    builtin_read,
38         "lambda",       AO_LISP_FUNC_NLAMBDA,   builtin_lambda,
39         "lexpr",        AO_LISP_FUNC_NLAMBDA,   builtin_lexpr,
40         "nlambda",      AO_LISP_FUNC_NLAMBDA,   builtin_nlambda,
41         "macro",        AO_LISP_FUNC_NLAMBDA,   builtin_macro,
42         "car",          AO_LISP_FUNC_LAMBDA,    builtin_car,
43         "cdr",          AO_LISP_FUNC_LAMBDA,    builtin_cdr,
44         "cons",         AO_LISP_FUNC_LAMBDA,    builtin_cons,
45         "last",         AO_LISP_FUNC_LAMBDA,    builtin_last,
46         "quote",        AO_LISP_FUNC_NLAMBDA,   builtin_quote,
47         "set",          AO_LISP_FUNC_LAMBDA,    builtin_set,
48         "setq",         AO_LISP_FUNC_MACRO,     builtin_setq,
49         "cond",         AO_LISP_FUNC_NLAMBDA,   builtin_cond,
50         "progn",        AO_LISP_FUNC_NLAMBDA,   builtin_progn,
51         "while",        AO_LISP_FUNC_NLAMBDA,   builtin_while,
52         "print",        AO_LISP_FUNC_LEXPR,     builtin_print,
53         "patom",        AO_LISP_FUNC_LEXPR,     builtin_patom,
54         "+",            AO_LISP_FUNC_LEXPR,     builtin_plus,
55         "-",            AO_LISP_FUNC_LEXPR,     builtin_minus,
56         "*",            AO_LISP_FUNC_LEXPR,     builtin_times,
57         "/",            AO_LISP_FUNC_LEXPR,     builtin_divide,
58         "%",            AO_LISP_FUNC_LEXPR,     builtin_mod,
59         "=",            AO_LISP_FUNC_LEXPR,     builtin_equal,
60         "<",            AO_LISP_FUNC_LEXPR,     builtin_less,
61         ">",            AO_LISP_FUNC_LEXPR,     builtin_greater,
62         "<=",           AO_LISP_FUNC_LEXPR,     builtin_less_equal,
63         ">=",           AO_LISP_FUNC_LEXPR,     builtin_greater_equal,
64         "delay",        AO_LISP_FUNC_LAMBDA,    builtin_delay,
65         "led",          AO_LISP_FUNC_LEXPR,     builtin_led,
66 };
67
68 #define N_FUNC (sizeof funcs / sizeof funcs[0])
69
70 struct ao_lisp_frame    *globals;
71
72 static int
73 is_atom(int offset)
74 {
75         struct ao_lisp_atom *a;
76
77         for (a = ao_lisp_atoms; a; a = ao_lisp_poly_atom(a->next))
78                 if (((uint8_t *) a->name - ao_lisp_const) == offset)
79                         return strlen(a->name);
80         return 0;
81 }
82
83 int
84 main(int argc, char **argv)
85 {
86         int     f, o, i;
87         ao_poly sexpr, val;
88         struct ao_lisp_atom     *a;
89         struct ao_lisp_builtin  *b;
90         int     in_atom;
91
92         printf("/*\n");
93         printf(" * Generated file, do not edit\n");
94         for (f = 0; f < N_FUNC; f++) {
95                 b = ao_lisp_make_builtin(funcs[f].func, funcs[f].args);
96                 a = ao_lisp_atom_intern(funcs[f].name);
97                 ao_lisp_atom_set(ao_lisp_atom_poly(a),
98                                  ao_lisp_builtin_poly(b));
99         }
100
101         /* boolean constants */
102         ao_lisp_atom_set(ao_lisp_atom_poly(ao_lisp_atom_intern("nil")),
103                          AO_LISP_NIL);
104         a = ao_lisp_atom_intern("t");
105         ao_lisp_atom_set(ao_lisp_atom_poly(a),
106                          ao_lisp_atom_poly(a));
107
108         /* end of file value */
109         a = ao_lisp_atom_intern("eof");
110         ao_lisp_atom_set(ao_lisp_atom_poly(a),
111                          ao_lisp_atom_poly(a));
112
113         ao_lisp_read_eval_print();
114
115         /* Reduce to referenced values */
116         ao_lisp_collect();
117         printf(" */\n");
118
119         printf("#define AO_LISP_POOL_CONST %d\n", ao_lisp_top);
120         printf("extern const uint8_t ao_lisp_const[AO_LISP_POOL_CONST] __attribute__((aligned(4)));\n");
121         printf("#define ao_builtin_atoms 0x%04x\n", ao_lisp_atom_poly(ao_lisp_atoms));
122         printf("#define ao_builtin_frame 0x%04x\n", ao_lisp_frame_poly(ao_lisp_frame_global));
123
124         for (a = ao_lisp_atoms; a; a = ao_lisp_poly_atom(a->next)) {
125                 char    *n = a->name, c;
126                 printf ("#define _ao_lisp_atom_");
127                 while ((c = *n++)) {
128                         if (isalnum(c))
129                                 printf("%c", c);
130                         else
131                                 printf("%02x", c);
132                 }
133                 printf("  0x%04x\n", ao_lisp_atom_poly(a));
134         }
135         printf("#ifdef AO_LISP_CONST_BITS\n");
136         printf("const uint8_t ao_lisp_const[] = {");
137         for (o = 0; o < ao_lisp_top; o++) {
138                 uint8_t c;
139                 if ((o & 0xf) == 0)
140                         printf("\n\t");
141                 else
142                         printf(" ");
143                 c = ao_lisp_const[o];
144                 if (!in_atom)
145                         in_atom = is_atom(o);
146                 if (in_atom) {
147                         printf (" '%c',", c);
148                         in_atom--;
149                 } else {
150                         printf("0x%02x,", c);
151                 }
152         }
153         printf("\n};\n");
154         printf("#endif /* AO_LISP_CONST_BITS */\n");
155 }