1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
/*
* Copyright © 2016 Keith Packard <keithp@keithp.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/
#include "ao_scheme.h"
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <getopt.h>
static struct ao_scheme_builtin *
ao_scheme_make_builtin(enum ao_scheme_builtin_id func, int args) {
struct ao_scheme_builtin *b = ao_scheme_alloc(sizeof (struct ao_scheme_builtin));
b->type = AO_SCHEME_BUILTIN;
b->func = func;
b->args = args;
return b;
}
struct builtin_func {
char *name;
int args;
enum ao_scheme_builtin_id func;
};
#define AO_SCHEME_BUILTIN_CONSTS
#include "ao_scheme_builtin.h"
#define N_FUNC (sizeof funcs / sizeof funcs[0])
struct ao_scheme_frame *globals;
static int
is_atom(int offset)
{
struct ao_scheme_atom *a;
for (a = ao_scheme_atoms; a; a = ao_scheme_poly_atom(a->next))
if (((uint8_t *) a->name - ao_scheme_const) == offset)
return strlen(a->name);
return 0;
}
#define AO_FEC_CRC_INIT 0xffff
static inline uint16_t
ao_fec_crc_byte(uint8_t byte, uint16_t crc)
{
uint8_t bit;
for (bit = 0; bit < 8; bit++) {
if (((crc & 0x8000) >> 8) ^ (byte & 0x80))
crc = (crc << 1) ^ 0x8005;
else
crc = (crc << 1);
byte <<= 1;
}
return crc;
}
uint16_t
ao_fec_crc(const uint8_t *bytes, uint8_t len)
{
uint16_t crc = AO_FEC_CRC_INIT;
while (len--)
crc = ao_fec_crc_byte(*bytes++, crc);
return crc;
}
struct ao_scheme_macro_stack {
struct ao_scheme_macro_stack *next;
ao_poly p;
};
struct ao_scheme_macro_stack *macro_stack;
int
ao_scheme_macro_push(ao_poly p)
{
struct ao_scheme_macro_stack *m = macro_stack;
while (m) {
if (m->p == p)
return 1;
m = m->next;
}
m = malloc (sizeof (struct ao_scheme_macro_stack));
m->p = p;
m->next = macro_stack;
macro_stack = m;
return 0;
}
void
ao_scheme_macro_pop(void)
{
struct ao_scheme_macro_stack *m = macro_stack;
macro_stack = m->next;
free(m);
}
#define DBG_MACRO 0
#if DBG_MACRO
int macro_scan_depth;
void indent(void)
{
int i;
for (i = 0; i < macro_scan_depth; i++)
printf(" ");
}
#define MACRO_DEBUG(a) a
#else
#define MACRO_DEBUG(a)
#endif
ao_poly
ao_has_macro(ao_poly p);
ao_poly
ao_macro_test_get(ao_poly atom)
{
ao_poly *ref = ao_scheme_atom_ref(atom, NULL);
if (ref)
return *ref;
return AO_SCHEME_NIL;
}
ao_poly
ao_is_macro(ao_poly p)
{
struct ao_scheme_builtin *builtin;
struct ao_scheme_lambda *lambda;
ao_poly ret;
MACRO_DEBUG(indent(); printf ("is macro "); ao_scheme_poly_write(p); printf("\n"); ++macro_scan_depth);
switch (ao_scheme_poly_type(p)) {
case AO_SCHEME_ATOM:
if (ao_scheme_macro_push(p))
ret = AO_SCHEME_NIL;
else {
if (ao_is_macro(ao_macro_test_get(p)))
ret = p;
else
ret = AO_SCHEME_NIL;
ao_scheme_macro_pop();
}
break;
case AO_SCHEME_CONS:
ret = ao_has_macro(p);
break;
case AO_SCHEME_BUILTIN:
builtin = ao_scheme_poly_builtin(p);
if ((builtin->args & AO_SCHEME_FUNC_MASK) == AO_SCHEME_FUNC_MACRO)
ret = p;
else
ret = 0;
break;
case AO_SCHEME_LAMBDA:
lambda = ao_scheme_poly_lambda(p);
if (lambda->args == AO_SCHEME_FUNC_MACRO)
ret = p;
else
ret = ao_has_macro(lambda->code);
break;
default:
ret = AO_SCHEME_NIL;
break;
}
MACRO_DEBUG(--macro_scan_depth; indent(); printf ("... "); ao_scheme_poly_write(ret); printf("\n"));
return ret;
}
ao_poly
ao_has_macro(ao_poly p)
{
struct ao_scheme_cons *cons;
struct ao_scheme_lambda *lambda;
ao_poly m;
ao_poly list;
if (p == AO_SCHEME_NIL)
return AO_SCHEME_NIL;
MACRO_DEBUG(indent(); printf("has macro "); ao_scheme_poly_write(p); printf("\n"); ++macro_scan_depth);
switch (ao_scheme_poly_type(p)) {
case AO_SCHEME_LAMBDA:
lambda = ao_scheme_poly_lambda(p);
p = ao_has_macro(lambda->code);
break;
case AO_SCHEME_CONS:
cons = ao_scheme_poly_cons(p);
if ((p = ao_is_macro(cons->car)))
break;
list = cons->cdr;
p = AO_SCHEME_NIL;
while (list != AO_SCHEME_NIL && ao_scheme_poly_type(list) == AO_SCHEME_CONS) {
cons = ao_scheme_poly_cons(list);
m = ao_has_macro(cons->car);
if (m) {
p = m;
break;
}
list = cons->cdr;
}
break;
default:
p = AO_SCHEME_NIL;
break;
}
MACRO_DEBUG(--macro_scan_depth; indent(); printf("... "); ao_scheme_poly_write(p); printf("\n"));
return p;
}
int
ao_scheme_read_eval_abort(void)
{
ao_poly in, out = AO_SCHEME_NIL;
for(;;) {
in = ao_scheme_read();
if (in == _ao_scheme_atom_eof)
break;
out = ao_scheme_eval(in);
if (ao_scheme_exception)
return 0;
ao_scheme_poly_write(out);
putchar ('\n');
}
return 1;
}
static FILE *in;
static FILE *out;
int
ao_scheme_getc(void)
{
return getc(in);
}
static const struct option options[] = {
{ .name = "out", .has_arg = 1, .val = 'o' },
{ 0, 0, 0, 0 }
};
static void usage(char *program)
{
fprintf(stderr, "usage: %s [--out=<output>] [input]\n", program);
exit(1);
}
int
main(int argc, char **argv)
{
int f, o;
ao_poly val;
struct ao_scheme_atom *a;
struct ao_scheme_builtin *b;
int in_atom = 0;
char *out_name = NULL;
int c;
enum ao_scheme_builtin_id prev_func;
in = stdin;
out = stdout;
while ((c = getopt_long(argc, argv, "o:", options, NULL)) != -1) {
switch (c) {
case 'o':
out_name = optarg;
break;
default:
usage(argv[0]);
break;
}
}
ao_scheme_frame_init();
/* Boolean values #f and #t */
ao_scheme_bool_get(0);
ao_scheme_bool_get(1);
prev_func = _builtin_last;
for (f = 0; f < (int) N_FUNC; f++) {
if (funcs[f].func != prev_func)
b = ao_scheme_make_builtin(funcs[f].func, funcs[f].args);
a = ao_scheme_atom_intern(funcs[f].name);
ao_scheme_atom_def(ao_scheme_atom_poly(a),
ao_scheme_builtin_poly(b));
}
/* end of file value */
a = ao_scheme_atom_intern("eof");
ao_scheme_atom_def(ao_scheme_atom_poly(a),
ao_scheme_atom_poly(a));
/* 'else' */
a = ao_scheme_atom_intern("else");
if (argv[optind]){
in = fopen(argv[optind], "r");
if (!in) {
perror(argv[optind]);
exit(1);
}
}
if (!ao_scheme_read_eval_abort()) {
fprintf(stderr, "eval failed\n");
exit(1);
}
/* Reduce to referenced values */
ao_scheme_collect(AO_SCHEME_COLLECT_FULL);
for (f = 0; f < ao_scheme_frame_global->num; f++) {
struct ao_scheme_frame_vals *vals = ao_scheme_poly_frame_vals(ao_scheme_frame_global->vals);
val = ao_has_macro(vals->vals[f].val);
if (val != AO_SCHEME_NIL) {
printf("error: function %s contains unresolved macro: ",
ao_scheme_poly_atom(vals->vals[f].atom)->name);
ao_scheme_poly_write(val);
printf("\n");
exit(1);
}
}
if (out_name) {
out = fopen(out_name, "w");
if (!out) {
perror(out_name);
exit(1);
}
}
fprintf(out, "/* Generated file, do not edit */\n\n");
fprintf(out, "#define AO_SCHEME_POOL_CONST %d\n", ao_scheme_top);
fprintf(out, "extern const uint8_t ao_scheme_const[AO_SCHEME_POOL_CONST] __attribute__((aligned(4)));\n");
fprintf(out, "#define ao_builtin_atoms 0x%04x\n", ao_scheme_atom_poly(ao_scheme_atoms));
fprintf(out, "#define ao_builtin_frame 0x%04x\n", ao_scheme_frame_poly(ao_scheme_frame_global));
fprintf(out, "#define ao_scheme_const_checksum ((uint16_t) 0x%04x)\n", ao_fec_crc(ao_scheme_const, ao_scheme_top));
fprintf(out, "#define _ao_scheme_bool_false 0x%04x\n", ao_scheme_bool_poly(ao_scheme_false));
fprintf(out, "#define _ao_scheme_bool_true 0x%04x\n", ao_scheme_bool_poly(ao_scheme_true));
for (a = ao_scheme_atoms; a; a = ao_scheme_poly_atom(a->next)) {
char *n = a->name, c;
fprintf(out, "#define _ao_scheme_atom_");
while ((c = *n++)) {
if (isalnum(c))
fprintf(out, "%c", c);
else
fprintf(out, "%02x", c);
}
fprintf(out, " 0x%04x\n", ao_scheme_atom_poly(a));
}
fprintf(out, "#ifdef AO_SCHEME_CONST_BITS\n");
fprintf(out, "const uint8_t ao_scheme_const[AO_SCHEME_POOL_CONST] __attribute((aligned(4))) = {");
for (o = 0; o < ao_scheme_top; o++) {
uint8_t c;
if ((o & 0xf) == 0)
fprintf(out, "\n\t");
else
fprintf(out, " ");
c = ao_scheme_const[o];
if (!in_atom)
in_atom = is_atom(o);
if (in_atom) {
fprintf(out, " '%c',", c);
in_atom--;
} else {
fprintf(out, "0x%02x,", c);
}
}
fprintf(out, "\n};\n");
fprintf(out, "#endif /* AO_SCHEME_CONST_BITS */\n");
exit(0);
}
|