-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.c
317 lines (297 loc) · 9.93 KB
/
reader.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "reader.h"
#include "runtime.h"
#include "evaluator.h"
#include "eval2.h"
int debug_lexer = 0;
int debug_reader = 0;
extern int yylex();
extern FILE* yyin;
union yystype yylval;
tagged_stype* reader_stack;
tagged_stype* rs_ptr;
void push_val(tagged_stype val)
{
*rs_ptr++ = val;
}
tagged_stype* pop_val()
{
if (rs_ptr > reader_stack) {
return --rs_ptr;
}
return NULL;
}
static const char* macro_name(int c)
{
switch (c) {
case '\'': return "quote";
case '`': return "quasiquote";
case ',': return "unquote";
case COMMA_AT: return "unquote-splicing";
}
return NULL;
}
void push_lispval(LispVal* lv)
{
const char* mn_inst = NULL;
while (rs_ptr > reader_stack && (mn_inst = macro_name(rs_ptr[-1].tag))) {
lv = lisp_cons(lisp_atom(sym(mn_inst)), lisp_cons(lv, lisp_nil()));
pop_val();
mn_inst = NULL;
}
push_val((tagged_stype){
.tag = LISPVAL,
.sval.value = lv
});
}
static LispVal* reader_read()
{
int num_parens = 0;
int lexval;
while ((lexval = yylex()) != 0) {
if (debug_lexer) {
fprintf(stderr, "LEXVAL = \"%c\" 0x%x, %d\n", lexval, lexval, lexval);
}
lexswitch:
switch (lexval) {
case ERROR:
{
if (debug_lexer) {
fprintf(stderr, "ERROR(%c)\n", yylval.err_char);
}
// reset stack
rs_ptr = reader_stack;
return NULL;
}
case NUM:
{
push_lispval(lisp_num(atoi(symtext(yylval.id))));
break;
}
case VAR:
{
push_lispval(lisp_atom(yylval.id));
break;
}
case '(':
{
push_val((tagged_stype){ .tag = '(' });
num_parens++;
break;
}
case ')':
{
if (num_parens == 0) {
fprintf(stderr, "syntax error\n");
break;
}
//mark_safepoint(); // communicate with the collector
// collapse stack into val
LispVal* thelist = lisp_nil();
tagged_stype* top;
while ((top = pop_val())) {
if (top->tag == '(')
break;
// build the list up from it's tail
if (top->tag == LISPVAL) {
thelist = lisp_cons(top->sval.value, thelist);
} else if (top->tag == '.') {
if (thelist->tag == LCONS && thelist->tail->tag == LNIL) {
// (...<rest> . <r1>)
thelist = thelist->head;
} else {
fprintf(stderr, "syntax error: . placement\n");
// don't actually do anything
}
} else {
// TODO: what happens here!
}
}
// push the constructed list back on
push_lispval(thelist);
num_parens--;
//mark_safepoint(); // communicate with the collector
break;
}
case '#':
{
// TODO !! perhaps we could deal with some
// of this in the lexer instead...?
lexval = yylex();
switch (lexval) {
case '(':
// TODO: vector
break;
case '\\':
// TODO: character
lexval = yylex();
switch (lexval) {
case '.':
case '\'':
case '`':
case ',':
case ' ':
case '\t':
case '\f':
case '\v':
case '(':
case ')':
push_lispval(lisp_char(lexval));
break;
case COMMA_AT:
// TODO
break;
case VAR:
// Check for character names
// like #\space
if (sym_equal(sym("space"), yylval.id)) {
push_lispval(lisp_char(' '));
} else if (sym_equal(sym("newline"), yylval.id)) {
push_lispval(lisp_char('\n'));
} else if (sym_equal(sym("tab"), yylval.id)) {
push_lispval(lisp_char('\t'));
} else if (strlen(symtext(yylval.id)) == 1) {
push_lispval(lisp_char(
symtext(yylval.id)[0]));
} else {
// unknown character name
fprintf(stderr,
"unknown character name: %s\n",
symtext(yylval.id));
rs_ptr = reader_stack;
return NULL;
}
break;
case 0:
return NULL; // EOF
case NUM:
// TODO: 0-9 should be fine
default:
// ignore?
continue;
}
break;
case VAR:
if (sym_equal(sym("t"), yylval.id)) {
push_lispval(lisp_bool(1));
} else if (sym_equal(sym("f"), yylval.id)) {
push_lispval(lisp_bool(0));
} else {
// combine the # with the symbol?
// error?
fprintf(stderr, "unknown reader macro #%s\n",
symtext(yylval.id));
rs_ptr = reader_stack;
return NULL;
}
break;
case 0:
return NULL; // EOF
default:
push_lispval(lisp_atom(sym("#")));
goto lexswitch;
}
break;
}
case '.':
case '\'':
case '`':
case ',':
case COMMA_AT:
push_val((tagged_stype){ .tag = lexval });
continue;
case ' ':
case '\t':
case '\f':
case '\v':
continue;
default:
fprintf(stderr, "%c\n", lexval);
break;
}
if (num_parens == 0) {
// return!
tagged_stype* top = pop_val();
if (top && top->tag == LISPVAL) {
return top->sval.value;
} else {
fprintf(stderr, "bad something...");
return NULL;
}
}
}
return NULL; // End of file
}
void set_stack_high(void** stack_high);
void set_stack_low(void** stack_low);
extern int verbose_gc;
extern int debug_evaluator;
extern int debug_eval2;
int main(int argc, char** argv)
{
int use_eval2 = 0;
for (int i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
if (strcmp(argv[i], "-v") == 0) {
if (verbose_gc) {
debug_evaluator = 1;
debug_eval2 = 1;
} else {
verbose_gc = 1;
debug_reader = 1;
}
} else if (strcmp(argv[i], "-vv") == 0) {
verbose_gc = 1;
debug_reader = 1;
debug_evaluator = 1;
debug_eval2 = 1;
} else if (strcmp(argv[i], "-2") == 0) {
use_eval2 = 1;
} else {
fprintf(stderr, "unknown flag: -%c", argv[i][1]);
}
} else {
yyin = fopen(argv[i], "r");
if (!yyin) {
perror(argv[i]);
exit(EXIT_FAILURE);
}
}
}
initialize_heap(512 * 1024);
if (use_eval2) {
initialize_evaluator2();
} else {
initialize_evaluator();
}
// initialise the reader stack
reader_stack = calloc(1024, sizeof *reader_stack);
if (!reader_stack) { perror("out of memory"); abort(); }
rs_ptr = reader_stack;
/*
* A neat, asm-free way I came up with of safely grabbing some stack
* bounds.
* set_stack_high and set_stack_low are in a separate module so that
* the optimizer can't (assume no LTO) get rid of the stack assignment
*/
void* dummy = 0; // use ptr word to get word alignment
set_stack_high(&dummy);
for (;;) {
LispVal* value = reader_read();
if (!value && feof(yyin))
break;
if (!value)
continue;
//print_lispval(stdout, value);
//printf("\n");
LispVal* evaluated = (use_eval2) ? eval2(value) : eval(value);
print_lispval(stdout, evaluated);
printf("\n");
if (debug_reader) {
print_heap_state(); // Just to get a print of GC stats
}
}
set_stack_high(&dummy);
}