-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.c
385 lines (346 loc) · 9.43 KB
/
parser.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
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
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "parser.h"
#include "utils.h"
char *source = NULL;
int is_in_defun = 0; // bit of a HACKY way to parse defun's arguments
static inline int is_ws(char ch) {
return ch == ' ' ||
ch == '\t' ||
ch == '\n';
}
static inline int isdigit(char ch) {
return '0' <= ch && ch <= '9';
}
static int is_iden_ch(char ch) {
return (33 <= ch && ch <= 126) &&
ch != '(' && ch != ')' &&
ch != '[' && ch != ']' &&
ch != '{' && ch != '}' &&
ch != '\'' && ch != '"';
}
void parse_ws() {
while(parser_pos < strlen(source)) {
char ch = parser_getch();
if(ch == ';') {
parser_pos++;
while(parser_pos < strlen(source)) {
parser_pos++;
if(parser_getch() == '\n') { parser_pos++; break; }
}
ch = parser_getch();
}
if(is_ws(ch))
parser_pos++;
else
break;
}
}
value *parse_value(int is_args) {
deprintf("PARSE_VALUE\n");
if(parser_pos >= strlen(source)) {
error("Expected value, got end of file.\n");
return NULL;
}
char ch = parser_getch();
deprintf("%c\n", ch);
value *val = malloc(sizeof(value));
if (ch == '"' || ch == '\'') {
char startch = ch;
parser_pos++;
ch = parser_getch();
char *string = calloc(1,2);
int ssize = 0;
while(parser_pos < strlen(source) && parser_getch() != startch) {
string = realloc(string, ssize + 1);
string[ssize] = source[parser_pos++];
ssize++;
}
string = realloc(string, ssize + 1);
string[ssize] = '\0';
deprintf("STRING %s\n", string);
if(parser_getch() != startch) {
error("Expected end of string\n");
free(string);
free(val);
return NULL;
}
parser_pos++;
val->val.string = string;
val->type = VALUE_STR_TYPE;
} else if (isdigit(ch) || ch == '-') {
int neg = 1;
if(ch == '-') {
neg = -1;
parser_pos++;
ch = parser_getch();
}
char *string = calloc(1,2);
int ssize = 0;
while(parser_pos < strlen(source) && isdigit(parser_getch())) {
string = realloc(string, ssize + 1);
string[ssize] = source[parser_pos++];
ssize++;
}
string = realloc(string, ssize + 1);
string[ssize] = '\0';
val->val.nint = atoi(string) * neg;
deprintf("Number %i\n",val->val.nint);
val->type = VALUE_NINT_TYPE;
} else if(is_iden_ch(ch)) {
char *string = calloc(1,2);
int ssize = 0;
while(parser_pos < strlen(source) && is_iden_ch(parser_getch())) {
string = realloc(string, ssize + 1);
string[ssize] = source[parser_pos++];
ssize++;
}
string = realloc(string, ssize + 1);
string[ssize] = '\0';
val->val.string = string;
val->type = VALUE_IDENT_TYPE;
} else if (ch == '(') {
if(is_args) {
deprintf("VALARGS\n");
array *arr = parse_array('(', ')');
if(arr == NULL) { free(val); return NULL; }
val->val.array = arr;
val->type = VALUE_ARRAY_TYPE;
} else {
deprintf("VALFNCALL\n");
fn_call *call = parse_fn_call();
if(call == NULL) { free(val); return NULL; }
val->val.fn_call = call;
val->type = VALUE_CALL_TYPE;
}
} else if (ch == '{') {
deprintf("VALBLOCK\n");
block *block = parse_block();
if(block == NULL) { free(val); return NULL; }
val->val.block = block;
val->type = VALUE_BLOCK_TYPE;
} else if (ch == '[') {
deprintf("VALARRAY\n");
array *arr = parse_array('[', ']');
if(arr == NULL) { free(val); return NULL; }
val->val.array = arr;
val->type = VALUE_ARRAY_TYPE;
} else {
error("Expected value, got %c\n", ch);
free(val);
return NULL;
}
return val;
}
array *parse_array(char start, char end) {
if(parser_getch() != start) {
error("Expected array start %c\n", start);
return NULL;
}
parser_pos++;
array *arr = malloc(sizeof(array));
arr->first = NULL;
arr->last = NULL;
if(parser_getch() == end) {
parser_pos++;
return arr;
}
value *val = parse_value(0);
array_item *item = malloc(sizeof(array_item));
item->val = val;
item->prev = NULL;
item->next = NULL;
arr->first = item;
arr->last = item;
if(arr->first == NULL) {
free(arr);
return NULL;
}
while(parser_pos < strlen(source) && is_ws(parser_getch()) && parser_getch() != end) {
parse_ws();
val = parse_value(0);
if(val == NULL) {
free(arr);
return NULL;
}
array_item *item = malloc(sizeof(array_item));
item->val = val;
item->prev = arr->last;
item->next = NULL;
arr->last->next = item;
arr->last = item;
}
parser_pos++;
return arr;
}
fn_arg *parse_fn_arg(int is_args) {
deprintf("PARSE_FN_ARG\n");
fn_arg *arg = malloc(sizeof(fn_arg));
if(is_args) {
arg->val = parse_value(1);
} else {
arg->val = parse_value(0);
}
if(arg->val == NULL) {
free(arg);
return NULL;
}
arg->next = NULL;
return arg;
}
fn_args *parse_fn_args() {
deprintf("PARSE_FN_ARGS\n");
deprintf("IN DEFUN: %i\n", is_in_defun);
fn_args *args = malloc(sizeof(fn_args));
if(parser_getch() == ')') {
args->first = NULL;
args->last = NULL;
return args;
}
fn_arg *first = parse_fn_arg(0);
fn_arg *last = first;
int argn = 1;
while(is_ws(parser_getch()) && parser_getch() != ')') {
parse_ws();
fn_arg *arg;
if(is_in_defun && argn == 1) {
arg = parse_fn_arg(1);
is_in_defun = 0; // ensure it only parses second arg of defun
} else {
arg = parse_fn_arg(0);
}
if(arg == NULL) {
free(args);
return NULL;
}
last->next = arg;
last = arg;
argn++;
}
args->first = first;
args->last = last;
return args;
}
fn_call *parse_fn_call() {
deprintf("FN_CALL\n");
if(parser_getch() != '(') {
deprintf("%c\n", parser_getch());
error("Expected ( in function call start\n");
return NULL;
}
parser_pos++;
char ch = parser_getch();
char *name;
if(is_iden_ch(ch)) {
name = malloc(2);
name[0] = ch;
name[1] = 0;
parser_pos++;
} else {
error("Expected name in function call\n");
return NULL;
}
int isize = 1;
while(is_iden_ch(parser_getch())) {
name = realloc(name, isize + 1);
name[isize] = source[parser_pos++];
isize++;
}
name[isize] = 0;
deprintf("NAME: %s\n", name);
fn_call *call = malloc(sizeof(fn_call));
call->name = name;
call->args = NULL;
call->next = NULL;
if(parser_getch() == ')') {
parser_pos++;
call->args = malloc(sizeof(fn_args));
call->args->first = NULL;
call->args->last = NULL;
return call;
}
if(strcmp(name, "defun") == 0) {
is_in_defun = 1;
}
parse_ws();
call->args = parse_fn_args();
if(call->args == NULL) {
return call;
}
call->next = NULL;
parse_ws();
if(parser_getch() != ')') {
error("Expected ) in function call end\n");
deprintf("%c\n", parser_getch());
free(name);
fn_args_destroy(call->args);
free(call);
return NULL;
}
parser_pos++;
return call;
}
block *parse_block() {
deprintf("BLOCK\n");
if(parser_getch() != '{') {
error("Expected block\n");
return NULL;
}
parser_pos++;
block *block = malloc(sizeof(block));
parse_ws();
if(parser_getch() == '}') {
parser_pos++;
block->first = NULL;
block->last = NULL;
return block;
}
block->first = parse_fn_call();
block->last = NULL;
if(block->first == NULL) {
return block;
}
parse_ws();
if(parser_getch() == '}') {
parser_pos++;
return block;
}
fn_call *cur = block->first;
while(parser_getch() != '}') {
fn_call *next = parse_fn_call();
if(next == NULL) {
block_destroy(block);
return NULL;
}
cur->next = next;
cur = next;
parse_ws();
}
parser_pos++;
return block;
}
program *parse_program() {
deprintf("PROGRAM\n");
program *program = malloc(sizeof(program));
parse_ws();
program->first = parse_fn_call();
if(program->first == NULL) {
free(program);
return NULL;
}
program->last = NULL;
parse_ws();
fn_call *cur = program->first;
while(parser_pos < strlen(source)) {
fn_call *next = parse_fn_call();
if(next == NULL) {
program_destroy(program);
return NULL;
}
cur->next = next;
cur = next;
parse_ws();
}
return program;
}