altos/lisp: Change lisp objects to use ao_poly everywhere. Add 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
18 static struct ao_lisp_builtin *
19 ao_lisp_make_builtin(enum ao_lisp_builtin_id func, int args) {
20         struct ao_lisp_builtin *b = ao_lisp_alloc(sizeof (struct ao_lisp_builtin));
21
22         b->type = AO_LISP_BUILTIN;
23         b->func = func;
24         b->args = args;
25         return b;
26 }
27
28 struct builtin_func {
29         char    *name;
30         int     args;
31         int     func;
32 };
33
34 struct builtin_func funcs[] = {
35         "car",          AO_LISP_LEXPR,  builtin_car,
36         "cdr",          AO_LISP_LEXPR,  builtin_cdr,
37         "cons",         AO_LISP_LEXPR,  builtin_cons,
38         "quote",        AO_LISP_NLAMBDA,builtin_quote,
39         "print",        AO_LISP_LEXPR,  builtin_print,
40         "+",            AO_LISP_LEXPR,  builtin_plus,
41         "-",            AO_LISP_LEXPR,  builtin_minus,
42         "*",            AO_LISP_LEXPR,  builtin_times,
43         "/",            AO_LISP_LEXPR,  builtin_divide,
44         "%",            AO_LISP_LEXPR,  builtin_mod
45 };
46
47 #define N_FUNC (sizeof funcs / sizeof funcs[0])
48
49 int
50 main(int argc, char **argv)
51 {
52         int     f, o;
53         ao_poly atom, val;
54
55         for (f = 0; f < N_FUNC; f++) {
56                 struct ao_lisp_builtin  *b = ao_lisp_make_builtin(funcs[f].func, funcs[f].args);
57                 struct ao_lisp_atom     *a = ao_lisp_atom_intern(funcs[f].name);
58                 a->val = ao_lisp_builtin_poly(b);
59         }
60
61         for (;;) {
62                 atom = ao_lisp_read();
63                 if (!atom)
64                         break;
65                 val = ao_lisp_read();
66                 if (!val)
67                         break;
68                 if (ao_lisp_poly_type(atom) != AO_LISP_ATOM) {
69                         fprintf(stderr, "input must be atom val pairs\n");
70                         exit(1);
71                 }
72                 ao_lisp_poly_atom(atom)->val = val;
73         }
74
75         printf("/* constant objects, all referenced from atoms */\n\n");
76         printf("#define AO_LISP_POOL_CONST %d\n", ao_lisp_top);
77         printf("extern const uint8_t ao_lisp_const[AO_LISP_POOL_CONST] __attribute__((aligned(4)));\n");
78         printf("#define ao_builtin_atoms 0x%04x\n", ao_lisp_atom_poly(ao_lisp_atoms));
79         printf("#ifdef AO_LISP_CONST_BITS\n");
80         printf("const uint8_t ao_lisp_const[] = {");
81         for (o = 0; o < ao_lisp_top; o++) {
82                 if ((o & 0xf) == 0)
83                         printf("\n\t");
84                 else
85                         printf(" ");
86                 printf("0x%02x,", ao_lisp_const[o]);
87         }
88         printf("\n};\n");
89         printf("#endif /* AO_LISP_CONST_BITS */\n");
90 }