altos: Add lambda support to lisp
[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 };
50
51 ao_poly
52 ao_lisp_set_cond(struct ao_lisp_cons *c)
53 {
54         (void) c;
55         return AO_LISP_NIL;
56 }
57
58 #define N_FUNC (sizeof funcs / sizeof funcs[0])
59
60 /* Syntactic atoms */
61 char *atoms[] = {
62         "lambda",
63         "nlambda",
64         "lexpr",
65         "macro"
66 };
67
68 #define N_ATOM (sizeof atoms / sizeof atoms[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 atom, 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         ao_lisp_root_add(&ao_lisp_frame_type, &globals);
95         globals = ao_lisp_frame_new(0, 0);
96         for (f = 0; f < N_FUNC; f++) {
97                 b = ao_lisp_make_builtin(funcs[f].func, funcs[f].args);
98                 a = ao_lisp_atom_intern(funcs[f].name);
99                 globals = ao_lisp_frame_add(globals, ao_lisp_atom_poly(a), ao_lisp_builtin_poly(b));
100         }
101
102         /* atoms for syntax */
103         for (i = 0; i < N_ATOM; i++)
104                 (void) ao_lisp_atom_intern(atoms[i]);
105
106         /* boolean constants */
107         a = ao_lisp_atom_intern("nil");
108         globals = ao_lisp_frame_add(globals, ao_lisp_atom_poly(a), AO_LISP_NIL);
109         a = ao_lisp_atom_intern("t");
110         globals = ao_lisp_frame_add(globals, ao_lisp_atom_poly(a), ao_lisp_atom_poly(a));
111
112         for (;;) {
113                 atom = ao_lisp_read();
114                 if (!atom)
115                         break;
116                 val = ao_lisp_read();
117                 if (!val)
118                         break;
119                 if (ao_lisp_poly_type(atom) != AO_LISP_ATOM) {
120                         fprintf(stderr, "input must be atom val pairs\n");
121                         exit(1);
122                 }
123                 globals = ao_lisp_frame_add(globals, atom, val);
124         }
125
126         /* Reduce to referenced values */
127         ao_lisp_collect();
128         printf(" */\n");
129
130         globals->readonly = 1;
131
132         printf("#define AO_LISP_POOL_CONST %d\n", ao_lisp_top);
133         printf("extern const uint8_t ao_lisp_const[AO_LISP_POOL_CONST] __attribute__((aligned(4)));\n");
134         printf("#define ao_builtin_atoms 0x%04x\n", ao_lisp_atom_poly(ao_lisp_atoms));
135         printf("#define ao_builtin_frame 0x%04x\n", ao_lisp_frame_poly(globals));
136
137         for (a = ao_lisp_atoms; a; a = ao_lisp_poly_atom(a->next)) {
138                 char    *n = a->name, c;
139                 printf ("#define _ao_lisp_atom_");
140                 while ((c = *n++)) {
141                         if (isalnum(c))
142                                 printf("%c", c);
143                         else
144                                 printf("%02x", c);
145                 }
146                 printf("  0x%04x\n", ao_lisp_atom_poly(a));
147         }
148         printf("#ifdef AO_LISP_CONST_BITS\n");
149         printf("const uint8_t ao_lisp_const[] = {");
150         for (o = 0; o < ao_lisp_top; o++) {
151                 uint8_t c;
152                 if ((o & 0xf) == 0)
153                         printf("\n\t");
154                 else
155                         printf(" ");
156                 c = ao_lisp_const[o];
157                 if (!in_atom)
158                         in_atom = is_atom(o);
159                 if (in_atom) {
160                         printf (" '%c',", c);
161                         in_atom--;
162                 } else {
163                         printf("0x%02x,", c);
164                 }
165         }
166         printf("\n};\n");
167         printf("#endif /* AO_LISP_CONST_BITS */\n");
168 }