-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
290 lines (245 loc) · 6.95 KB
/
parser.c
File metadata and controls
290 lines (245 loc) · 6.95 KB
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
#include <setjmp.h>
#include <stdio.h>
#include <string.h>
#include "mouselisp.h"
inline static int is_whitespace(unsigned char c) {
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
}
inline static int is_end_value(unsigned char c) {
return is_whitespace(c) || c == '(' || c == ')' || c == ';';
}
inline static int is_sign(unsigned char c) {
return (c == '+') || (c == '-');
}
inline static int is_name(unsigned char c) {
return (c == '!') || (c == '%') || (c == '&') || (c == '*') || (c == '/') ||
(c == ':') || (c == '<') || (c == '=') || (c == '>') || (c == '?') ||
(c == '^') || (c == '_') || (c == '~') || (c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z');
}
inline static int is_number(unsigned char c) { return c >= '0' && c <= '9'; }
inline static int is_name_rest(unsigned char c) {
return is_sign(c) || is_name(c) || is_number(c);
}
ml_parser ml_parser_new_str(const char *str) {
return (ml_parser){.file = ml_file_new_str(str)};
}
ml_parser ml_parser_new_file(FILE *file) {
return (ml_parser){.file = ml_file_new_file(file)};
}
ml_object *ml_parser_parse_literal_number(ml_parser *p, ml_machine *m) {
enum { STATE_START, STATE_ZERO, STATE_INTERGER } st = STATE_START;
ml_read_char c;
int sign = 1;
int ret = 0;
for (;;) {
c = ml_file_read(&p->file);
switch (st) {
case STATE_START:
if (c.eof)
ml_throw(m, ml_syntax_error(m, the_nil));
if (c.c == '0') {
st = STATE_ZERO;
break;
}
if (c.c >= '1' && c.c <= '9') {
ret += c.c - '0';
st = STATE_INTERGER;
break;
}
if (c.c == '+') {
st = STATE_INTERGER;
break;
}
if (c.c == '-') {
sign = -1;
st = STATE_INTERGER;
break;
}
ml_throw(m, ml_syntax_error(m, the_nil));
case STATE_ZERO:
if (c.eof || is_end_value(c.c)) {
if (!c.eof)
ml_file_unread(&p->file, c.c);
return ml_object_new_number(ret);
}
ml_throw(m, ml_syntax_error(m, the_nil));
case STATE_INTERGER:
if (c.eof || is_end_value(c.c)) {
if (!c.eof)
ml_file_unread(&p->file, c.c);
return ml_object_new_number(ret);
}
if (c.c >= '0' && c.c <= '9') {
ret = chk_addi(m, chk_muli(m, 10, ret), sign * (c.c - '0'));
break;
}
ml_throw(m, ml_syntax_error(m, the_nil));
}
}
}
ml_object *ml_parser_parse_expr(ml_parser *p, ml_machine *m);
ml_object *ml_parser_parse_list(ml_parser *p, ml_machine *m) {
enum { STATE_START, STATE_LIST } st = STATE_START;
ml_read_char c;
ml_object *expr;
ml_object *root = the_nil;
ml_object *tail = root;
for (;;) {
c = ml_file_read(&p->file);
switch (st) {
case STATE_START:
if (c.eof)
ml_throw(m, ml_syntax_error(m, the_nil));
if (c.c == '(') {
st = STATE_LIST;
break;
}
ml_throw(m, ml_syntax_error(m, the_nil));
case STATE_LIST:
if (c.eof)
ml_throw(m, ml_syntax_error(m, the_nil));
if (is_whitespace(c.c))
continue;
if (c.c == ';') {
while (!c.eof && c.c != '\n')
c = ml_file_read(&p->file);
continue;
}
if (c.c == ')') {
if (root != the_nil)
return root;
ml_throw(m, ml_syntax_error(m, the_nil));
}
ml_file_unread(&p->file, c.c);
expr = ml_parser_parse_expr(p, m);
if (root == the_nil) {
root = tail = ml_object_new_cons(expr, the_nil);
} else {
tail->cons.cdr = ml_object_new_cons(expr, the_nil);
tail = tail->cons.cdr;
}
break;
}
}
}
ml_object *ml_parser_parse_expr(ml_parser *p, ml_machine *m) {
enum { STATE_START, STATE_LITERAL_NUMBER, STATE_NAME } st = STATE_START;
ml_read_char c;
char first;
ml_string strbuf = ml_string_new_str("");
for (;;) {
c = ml_file_read(&p->file);
switch (st) {
case STATE_START:
if (c.eof)
ml_throw(m, ml_syntax_error(m, the_nil));
if (is_whitespace(c.c))
break;
if (c.c == ';') {
while (!c.eof && c.c != '\n')
c = ml_file_read(&p->file);
break;
}
if (c.c == '(') {
ml_file_unread(&p->file, c.c);
return ml_parser_parse_list(p, m);
}
if (is_number(c.c) || is_sign(c.c)) {
first = c.c;
st = STATE_LITERAL_NUMBER;
break;
}
if (is_name(c.c)) {
ml_string_concat_char(&strbuf, c.c);
st = STATE_NAME;
break;
}
ml_throw(m, ml_syntax_error(m, the_nil));
case STATE_LITERAL_NUMBER:
if (c.eof || is_end_value(c.c)) {
if (!c.eof)
ml_file_unread(&p->file, c.c);
if (is_sign(first)) {
ml_string_concat_char(&strbuf, first);
goto name_end;
} else {
ml_file_unread(&p->file, first);
return ml_parser_parse_literal_number(p, m);
}
}
if (is_number(c.c)) {
ml_file_unread(&p->file, c.c);
ml_file_unread(&p->file, first);
return ml_parser_parse_literal_number(p, m);
}
if (is_name(c.c) || is_sign(c.c)) {
if (is_sign(first)) {
ml_string_concat_char(&strbuf, first);
ml_string_concat_char(&strbuf, c.c);
st = STATE_NAME;
break;
}
}
ml_throw(m, ml_syntax_error(m, the_nil));
case STATE_NAME:
if (c.eof || is_end_value(c.c)) {
if (!c.eof)
ml_file_unread(&p->file, c.c);
goto name_end;
}
if (is_name_rest(c.c)) {
ml_string_concat_char(&strbuf, c.c);
break;
}
ml_throw(m, ml_syntax_error(m, the_nil));
}
}
name_end:
if (strcmp(strbuf.str, "nil") == 0)
return the_nil;
else if (strcmp(strbuf.str, "true") == 0)
return ml_object_new_bool(1);
else if (strcmp(strbuf.str, "false") == 0)
return ml_object_new_bool(0);
else
return ml_object_new_name(strbuf.str);
}
ml_object *ml_parser_parse(ml_parser *p, ml_machine *m) {
ml_read_char c;
ml_object *expr;
ml_object *root = the_nil;
ml_object *tail = root;
for (;;) {
c = ml_file_read(&p->file);
if (c.eof)
return root;
if (is_whitespace(c.c))
continue;
if (c.c == ';') {
while (!c.eof && c.c != '\n')
c = ml_file_read(&p->file);
continue;
}
ml_file_unread(&p->file, c.c);
expr = ml_parser_parse_expr(p, m);
if (root == the_nil) {
root = tail = ml_object_new_cons(expr, the_nil);
} else {
tail->cons.cdr = ml_object_new_cons(expr, the_nil);
tail = tail->cons.cdr;
}
}
}
ml_object *ml_parser_xparse2(const char *filename, const char *line,
ml_parser *p, ml_machine *m) {
/* top level error handling */
jmp_buf curr_jmpbuf;
m->last_exc_handler = &curr_jmpbuf;
m->exc = the_nil;
if (setjmp(curr_jmpbuf) == 0)
return ml_parser_parse(p, m);
else
fatal("%s: %s: error: %s", filename, line,
m->exc->cons.cdr->cons.car->str.str);
}