6b6039795e7fe2fce6c456f865a9ff6cd288cd5f
[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         "print",        AO_LISP_LEXPR,  builtin_print,
43         "+",            AO_LISP_LEXPR,  builtin_plus,
44         "-",            AO_LISP_LEXPR,  builtin_minus,
45         "*",            AO_LISP_LEXPR,  builtin_times,
46         "/",            AO_LISP_LEXPR,  builtin_divide,
47         "%",            AO_LISP_LEXPR,  builtin_mod
48 };
49
50 #define N_FUNC (sizeof funcs / sizeof funcs[0])
51
52 struct ao_lisp_frame    *globals;
53
54 static int
55 is_atom(int offset)
56 {
57         struct ao_lisp_atom *a;
58
59         for (a = ao_lisp_atoms; a; a = ao_lisp_poly_atom(a->next))
60                 if (((uint8_t *) a->name - ao_lisp_const) == offset)
61                         return strlen(a->name);
62         return 0;
63 }
64
65 int
66 main(int argc, char **argv)
67 {
68         int     f, o;
69         ao_poly atom, val;
70         struct ao_lisp_atom     *a;
71         int     in_atom;
72
73         printf("/*\n");
74         printf(" * Generated file, do not edit\n");
75         ao_lisp_root_add(&ao_lisp_frame_type, &globals);
76         globals = ao_lisp_frame_new(0, 0);
77         for (f = 0; f < N_FUNC; f++) {
78                 struct ao_lisp_builtin  *b = ao_lisp_make_builtin(funcs[f].func, funcs[f].args);
79                 struct ao_lisp_atom     *a = ao_lisp_atom_intern(funcs[f].name);
80                 globals = ao_lisp_frame_add(globals, ao_lisp_atom_poly(a), ao_lisp_builtin_poly(b));
81         }
82
83         /* boolean constants */
84         a = ao_lisp_atom_intern("nil");
85         globals = ao_lisp_frame_add(globals, ao_lisp_atom_poly(a), AO_LISP_NIL);
86         a = ao_lisp_atom_intern("t");
87         globals = ao_lisp_frame_add(globals, ao_lisp_atom_poly(a), ao_lisp_atom_poly(a));
88
89         for (;;) {
90                 atom = ao_lisp_read();
91                 if (!atom)
92                         break;
93                 val = ao_lisp_read();
94                 if (!val)
95                         break;
96                 if (ao_lisp_poly_type(atom) != AO_LISP_ATOM) {
97                         fprintf(stderr, "input must be atom val pairs\n");
98                         exit(1);
99                 }
100                 globals = ao_lisp_frame_add(globals, atom, val);
101         }
102
103         /* Reduce to referenced values */
104         ao_lisp_collect();
105         printf(" */\n");
106
107         globals->readonly = 1;
108
109         printf("#define AO_LISP_POOL_CONST %d\n", ao_lisp_top);
110         printf("extern const uint8_t ao_lisp_const[AO_LISP_POOL_CONST] __attribute__((aligned(4)));\n");
111         printf("#define ao_builtin_atoms 0x%04x\n", ao_lisp_atom_poly(ao_lisp_atoms));
112         printf("#define ao_builtin_frame 0x%04x\n", ao_lisp_frame_poly(globals));
113
114         for (a = ao_lisp_atoms; a; a = ao_lisp_poly_atom(a->next)) {
115                 char    *n = a->name, c;
116                 printf ("#define _ao_lisp_atom_");
117                 while ((c = *n++)) {
118                         if (isalnum(c))
119                                 printf("%c", c);
120                         else
121                                 printf("%02x", c);
122                 }
123                 printf("  0x%04x\n", ao_lisp_atom_poly(a));
124         }
125         printf("#ifdef AO_LISP_CONST_BITS\n");
126         printf("const uint8_t ao_lisp_const[] = {");
127         for (o = 0; o < ao_lisp_top; o++) {
128                 uint8_t c;
129                 if ((o & 0xf) == 0)
130                         printf("\n\t");
131                 else
132                         printf(" ");
133                 c = ao_lisp_const[o];
134                 if (!in_atom)
135                         in_atom = is_atom(o);
136                 if (in_atom) {
137                         printf (" '%c',", c);
138                         in_atom--;
139                 } else {
140                         printf("0x%02x,", c);
141                 }
142         }
143         printf("\n};\n");
144         printf("#endif /* AO_LISP_CONST_BITS */\n");
145 }