-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.cpp
388 lines (329 loc) · 8.65 KB
/
parser.cpp
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
// Copyright (c) 2009, Nicholas "Indy" Ray. All rights reserved.
// See the LICENSE file for usage, modification, and distribution terms.
#include "parser.h"
#include "types.h"
#include "symbol.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
struct parser
{
struct symbol_table* symbols;
const char* curr;
int curr_line_num;
bool bErrorFree;
};
pointer parser_error(parser* parse, const char* Error, ...)
{
va_list ap;
va_start(ap, Error);
parse->bErrorFree = false;
printf("Error: Parsing Error line, %d: ", parse->curr_line_num);
vprintf(Error, ap);
putc('\n', stdout);
va_end(ap);
return NIL;
}
bool is_newline(char c)
{
return (c == '\r') || ( c == '\n');
}
bool is_whitespace(char c)
{
return (c == ' ') || (c == '\t') || is_newline(c);
}
bool is_delimiter(char c)
{
return is_whitespace(c) || (c == '(') || (c == ')') || (c == '\0');
}
bool is_symbol_char(char c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || strchr("!$%&*+-./:<=>?@^_~#", c);
}
bool is_number_char(char c)
{
return (c >= '0' && c <= '9') || strchr(".+-", c);
}
bool is_extended_hex_char(char c)
{
return (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
}
void eat_whitespace(parser* parse)
{
while(is_whitespace(*parse->curr))
{
if(*parse->curr == '\n')
parse->curr_line_num++;
parse->curr++;
}
}
pointer parse_expr(parser* parse);
pointer parse_string(parser* parse)
{
parse->curr++;
pointer string_buf;
{
const char* C = parse->curr;
size_t string_len = 0;
while(*C != '"')
{
if(*C == '\\')
C++;
C++;
string_len ++;
if(*C == '\0')
return parser_error(parse, "Unexpected EOF in string literal");
}
string_buf = alloc_string(string_len);
}
char* C = get_string_ref(string_buf);
while(*parse->curr != '"')
{
if(*parse->curr == '\\')
{
parse->curr++;
switch(*parse->curr)
{
case 'n':
*C++ = '\n';
break;
case 't':
*C++ = '\t';
break;
default:
*C++ = *parse->curr;
}
parse->curr++;
}
else
*C++ = *parse->curr++;
}
*C = '\0';
parse->curr++;
return string_buf;
}
pointer parse_symbol(parser* parse)
{
const char* C = parse->curr;
while(!is_delimiter(*parse->curr))
{
if(is_symbol_char(*parse->curr))
parse->curr++;
else
return parser_error(parse, "Unexpected char '%c' in symbol.", *parse->curr);
}
return create_symbol(parse->symbols, C, parse->curr-C);
}
pointer parse_number(parser* parse)
{
size_t len = 0;
bool isFloat = false;
bool isHex = false;
bool isBinary = false;
const char* Start = parse->curr;
while(!is_delimiter(*parse->curr))
{
if(*parse->curr == '.')
isFloat = true;
if(is_number_char(*parse->curr) ||
(isHex && is_extended_hex_char(*parse->curr)))
len++;
else if(len == 1 &&
*parse->curr == 'x' &&
*Start == '0')
{
len++;
isHex = true;
}
else if(len == 0 && *parse->curr == 'b')
isBinary = true;
else
return parser_error(parse, "Unexpected char '%c' in number literal.", *parse->curr);
parse->curr++;
}
{
int TotalIs = isHex + isBinary + isFloat;
if(TotalIs > true)
{
char* buffer = new char[len+1];
strncpy(buffer, Start, len);
buffer[len] = '\0';
parser_error(parse, "Unexpected number literal: %s.", buffer);
delete buffer;
return NIL;
}
}
if(isFloat)
{
char* buffer = new char[len+1];
strncpy(buffer, Start, len);
buffer[len] = '\0';
float ret = atof(buffer);
delete buffer;
return create_real(ret);
}
else
{
// Might be smart to use a buffer here, in case strtol doesn't see all delimiters as we do.
int ret;
if(isHex)
ret = strtol(Start + 2, NULL, 16);
else if(isBinary)
ret = strtol(Start + 1, NULL, 2);
else
ret = strtol(Start, NULL, 10);
return create_int(ret);
}
}
pointer parse_number_or_symbol(parser* parse)
{
// The scheme spec doesn't explicitlly call for symbols like ++, but we're going to allow them.
char next = (*(parse->curr+1));
if(next >= '0' && next <= '9')
return parse_number(parse);
else
return parse_symbol(parse);
}
pointer parse_number_or_pair(parser* parse)
{
char next = (*(parse->curr+1));
pointer ret_car;
pointer ret_cdr;
if(is_whitespace(next))
{
parse->curr++;
return parse_expr(parse);
}
else if(next >= '0' && next <= '9')
{
ret_car = parse_number(parse);
ret_cdr = parse_expr(parse);
return create_pair(ret_car, ret_cdr);
}
else
{
ret_car = parse_symbol(parse);
ret_cdr = parse_expr(parse);
return create_pair(ret_car, ret_cdr);
}
}
pointer parse_quote(parser* parse)
{
if(is_whitespace(*parse->curr))
return parser_error(parse, "unexpected whitespace after quote.");
switch(*parse->curr)
{
case '(':
return create_pair(create_symbol(parse->symbols, "QUOTE"), parse_expr(parse));
default:
if(is_symbol_char(*parse->curr) && !is_number_char(*parse->curr))
return create_pair(create_symbol(parse->symbols, "QUOTE"), parse_symbol(parse));
else
return parser_error(parse, "Unexpected token after quote.");
}
}
pointer parse_expr(parser* parse)
{
eat_whitespace(parse);
pointer ret_car;
pointer ret_cdr;
switch(*parse->curr)
{
case '(':
parse->curr++;
ret_car = parse_expr(parse);
ret_cdr = parse_expr(parse);
return create_pair(ret_car, ret_cdr);
case '"':
ret_car = parse_string(parse);
ret_cdr = parse_expr(parse);
return create_pair(ret_car, ret_cdr);
case '\'':
parse->curr++;
ret_car = parse_quote(parse);
ret_cdr = parse_expr(parse);
return create_pair(ret_car, ret_cdr);
case ')':
parse->curr++;
return NIL;
case '+': case '-': case 'b':
ret_car = parse_number_or_symbol(parse);
ret_cdr = parse_expr(parse);
return create_pair(ret_car, ret_cdr);
case '.':
return parse_number_or_pair(parse);
case '\\':
parse->curr++;
ret_car = create_char(*(parse->curr++));
ret_cdr = parse_expr(parse);
return create_pair(ret_car, ret_cdr);
case ';':
while(!is_newline(*parse->curr) && *parse->curr != '\0')
parse->curr++;
return parse_expr(parse);
case 0:
return NIL;
default:
if(is_number_char(*parse->curr))
{
ret_car = parse_number(parse);
ret_cdr = parse_expr(parse);
return create_pair(ret_car, ret_cdr);
}
else if(is_symbol_char(*parse->curr))
{
ret_car = parse_symbol(parse);
ret_cdr = parse_expr(parse);
return create_pair(ret_car, ret_cdr);
}
else
return parser_error(parse, "Unexpected char in expression.");
}
parse->curr++;
}
pointer parser_parse_expression(parser* parse, const char* string)
{
parse->curr = string;
parse->curr_line_num = 0;
parse->bErrorFree = true;
pointer ret = parse_expr(parse);
if(parse->bErrorFree)
return ret;
else
{
destroy_list(ret);
return NIL;
}
}
parser* init_parser(symbol_table* table)
{
parser* parse = new parser();
parse->symbols = table;
parse->curr = NULL;
return parse;
}
void destroy_parser(parser* parse)
{
delete parse;
}
pointer parse_file_to_tree(const char* file_location, symbol_table* table)
{
parser* parse = init_parser(table);
FILE* fin = fopen(file_location, "r");
if(!fin)
{
printf("Input File \"%s\" does not exist.\n", file_location);
return NIL;
}
fseek(fin, 0, SEEK_END);
size_t flen = ftell(fin);
rewind(fin);
char* buffer = new char[flen+1];
fread(buffer, 1, flen, fin);
buffer[flen] = '\0';
fclose(fin);
pointer ret = parser_parse_expression(parse, buffer);
destroy_parser(parse);
delete buffer;
return ret;
}