-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
424 lines (387 loc) · 13.1 KB
/
parse.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include "nuclei.h"
void parse_list(Token_list* list) {
Syntax_tree* tree = (Syntax_tree*)allocate_space(1, sizeof(Syntax_tree));
Token_node* current = list->start;
// printf("First token being considered is VVV\n"); // debugging
// print_token(current); // debugging
// printf("\n"); // debugging
bool parses_correctly = true;
tree->program = descend_recursively(¤t, &parses_correctly);
print_tree(tree); // debugging
if (parses_correctly) {
printf("Parsed OK\n");
free_tree(tree);
} else {
fprintf(stderr, "PARSING ERROR - syntactically invalid NUCLEI program\n");
free_tree(tree);
exit(EXIT_FAILURE);
}
}
Tree_node* descend_recursively(Token_node** current, bool* parses_correctly) {
Tree_node* program = make_node(PROG);
if (!next_token_is(current, 1, t_l_parenthesis)) {
return parser_fails(parses_correctly);
} else {
program->child1 = handle_INSTRCTS(current, parses_correctly);
}
return program;
}
Tree_node* handle_INSTRCTS(Token_node** current, bool* parses_correctly) {
if ((*current) == NULL) {
return parser_fails(parses_correctly);
}
// printf("In INSTRCTS. Considering token: "); // debugging
// print_token((*current)); // debugging
// printf("\n"); // debugging
if (next_token_is(current, 1, t_r_parenthesis)) {
return NULL;
} else {
Tree_node* instructions = make_node(INSTRCTS);
instructions->child1 = handle_INSTRCT(current, parses_correctly);
instructions->child2 = handle_INSTRCTS(current, parses_correctly);
return instructions;
}
}
Tree_node* handle_INSTRCT(Token_node** current, bool* parses_correctly) {
if (!next_token_is(current, 1, t_l_parenthesis)) {
return parser_fails(parses_correctly);
}
Tree_node* instruction = make_node(INSTRCT);
instruction->child1 = handle_FUNC(current, parses_correctly);
if (!next_token_is(current, 1, t_r_parenthesis)) {
return parser_fails(parses_correctly);
}
return instruction;
}
Tree_node* handle_FUNC(Token_node** current, bool* parses_correctly) {
Tree_node* function = make_node(FUNC);
if (is_RETFUNC(*current)) {
function->child1 = handle_RETFUNC(current, parses_correctly);
} else if (is_IOFUNC(*current)) {
function->child1 = handle_IOFUNC(current, parses_correctly);
} else if (is_IF(*current)) {
function->child1 = handle_IF(current, parses_correctly);
} else if (is_LOOP(*current)) {
function->child1 = handle_LOOP(current, parses_correctly);
} else {
return parser_fails(parses_correctly);
}
return function;
}
bool is_RETFUNC(Token_node* current) {
return (is_LISTFUNC(current) || is_INTFUNC(current) || is_BOOLFUNC(current));
}
Tree_node* handle_RETFUNC(Token_node** current, bool* parses_correctly) {
Tree_node* ret_function = make_node(RETFUNC);
if (is_LISTFUNC(*current)) {
ret_function->child1 = handle_LISTFUNC(current, parses_correctly);
} else if (is_INTFUNC(*current)) {
ret_function->child1 = handle_INTFUNC(current, parses_correctly);
} else if (is_BOOLFUNC(*current)) {
ret_function->child1 = handle_BOOLFUNC(current, parses_correctly);
} else {
return parser_fails(parses_correctly);
}
return ret_function;
}
bool is_IOFUNC(Token_node* current) {
return (current->value->type == t_set || current->value->type == t_print);
}
Tree_node* handle_IOFUNC(Token_node** current, bool* parses_correctly) {
Tree_node* io_function = make_node(IOFUNC);
if ((*current)->value->type == t_set) {
io_function->child1 = handle_SET(current, parses_correctly);
} else if ((*current)->value->type == t_print) {
io_function->child1 = handle_PRINT(current, parses_correctly);
} else {
return parser_fails(parses_correctly);
}
return io_function;
}
Tree_node* handle_SET(Token_node** current, bool* parses_correctly) {
Tree_node* set = make_node(SET);
if (!next_token_is(current, 1, t_set)) {
return parser_fails(parses_correctly);
} else {
set->child1 = handle_VAR(current, parses_correctly);
set->child2 = handle_LIST(current, parses_correctly);
return set;
}
}
bool is_IF(Token_node* current) {
// print_token(current); // debugging
// printf(" <- this is the current token\n"); // debugging
return (current->value->type == t_if);
}
Tree_node* handle_IF(Token_node** current, bool* parses_correctly) {
Tree_node* if_node = make_node(IF);
if (!next_token_is(current, 1, t_if)) {
return parser_fails(parses_correctly);
}
if (!next_token_is(current, 1, t_l_parenthesis)) {
return parser_fails(parses_correctly);
}
if_node->child1 = handle_BOOLFUNC(current, parses_correctly);
if (!next_token_is(current, 1, t_r_parenthesis)) {
return parser_fails(parses_correctly);
}
if (!next_token_is(current, 1, t_l_parenthesis)) {
return parser_fails(parses_correctly);
}
if_node->child2 = handle_INSTRCTS(current, parses_correctly);
if (!next_token_is(current, 1, t_l_parenthesis)) {
return parser_fails(parses_correctly);
}
if_node->child3 = handle_INSTRCTS(current, parses_correctly);
return if_node;
}
bool is_LOOP(Token_node* current) {
return (current->value->type == t_while);
}
Tree_node* handle_LOOP(Token_node** current, bool* parses_correctly) {
Tree_node* loop = make_node(LOOP);
if (!next_token_is(current, 1, t_while)) {
return parser_fails(parses_correctly);
}
if (!next_token_is(current, 1, t_l_parenthesis)) {
return parser_fails(parses_correctly);
}
loop->child1 = handle_BOOLFUNC(current, parses_correctly);
if (!next_token_is(current, 1, t_r_parenthesis)) {
return parser_fails(parses_correctly);
}
if (!next_token_is(current, 1, t_l_parenthesis)) {
return parser_fails(parses_correctly);
}
loop->child2 = handle_INSTRCTS(current, parses_correctly);
return loop;
}
bool is_LISTFUNC(Token_node* current) {
token_type type = current->value->type;
return (type == t_CAR || type == t_CDR || type == t_CONS);
}
Tree_node* handle_LISTFUNC(Token_node** current, bool* parses_correctly) {
Tree_node* list_function = make_node(LISTFUNC);
token_type type = (*current)->value->type;
if (!next_token_is(current, 3, t_CAR, t_CDR, t_CONS)) {
return parser_fails(parses_correctly);
} else {
list_function->func_type = type;
}
list_function->child1 = handle_LIST(current, parses_correctly);
if (type == t_CONS) {
list_function->child2 = handle_LIST(current, parses_correctly);
}
return list_function;
}
bool is_INTFUNC(Token_node* current) {
token_type type = current->value->type;
return (type == t_plus || type == t_length);
}
Tree_node* handle_INTFUNC(Token_node** current, bool* parses_correctly) {
Tree_node* int_function = make_node(INTFUNC);
token_type type = (*current)->value->type;
if (!next_token_is(current, 2, t_plus, t_length)) {
return parser_fails(parses_correctly);
} else {
int_function->func_type = type;
int_function->child1 = handle_LIST(current, parses_correctly);
}
if (type == t_plus) {
int_function->child2 = handle_LIST(current, parses_correctly);
}
return int_function;
}
bool is_BOOLFUNC(Token_node* current) {
token_type type = current->value->type;
return (type == t_less || type == t_greater || type == t_equal);
}
Tree_node* handle_BOOLFUNC(Token_node** current, bool* parses_correctly) {
Tree_node* bool_function = make_node(BOOLFUNC);
token_type type = (*current)->value->type;
if (!next_token_is(current, 3, t_less, t_greater, t_equal)) {
return parser_fails(parses_correctly);
} else {
bool_function->func_type = type;
bool_function->child1 = handle_LIST(current, parses_correctly);
bool_function->child2 = handle_LIST(current, parses_correctly);
}
return bool_function;
}
Tree_node* handle_LIST(Token_node** current, bool* parses_correctly) {
Tree_node* list = make_node(LIST);
token_type type = (*current)->value->type;
if (type == t_variable) {
list->child1 = handle_VAR(current, parses_correctly);
} else if (type == t_literal) {
list->child1 = handle_LITERAL(current, parses_correctly);
} else if (type == t_nil) {
list->child1 = handle_NIL(current, parses_correctly);
} else if (next_token_is(current, 1, t_l_parenthesis)) {
list->child1 = handle_RETFUNC(current, parses_correctly);
if (!next_token_is(current, 1, t_r_parenthesis)) {
return parser_fails(parses_correctly);
}
} else {
return parser_fails(parses_correctly);
}
return list;
}
Tree_node* handle_VAR(Token_node** current, bool* parses_correctly) {
Tree_node* var = make_node(VAR);
var->var_name = (*current)->value->var_name;
*current = (*current)->next;
return var;
}
Tree_node* handle_LITERAL(Token_node** current, bool* parses_correctly) {
if ((*current)->value->type == t_literal) {
Tree_node* literal = make_node(LITERAL);
literal->string_value = (*current)->value->lexeme;
*current = (*current)->next;
return literal;
} else {
return parser_fails(parses_correctly);
}
}
Tree_node* handle_NIL(Token_node** current, bool* parses_correctly) {
Tree_node* nil = make_node(NIL);
*current = (*current)->next;
return nil;
}
Tree_node* handle_PRINT(Token_node** current, bool* parses_correctly) {
Tree_node* print = make_node(PRINT);
if (!next_token_is(current, 1, t_print)) {
return parser_fails(parses_correctly);
}
if ((*current)->value->type == t_string) {
print->child1 = handle_STRING(current, parses_correctly);
} else if (is_LIST(*current)) {
print->child1 = handle_LIST(current, parses_correctly);
}
return print;
}
bool is_LIST(Token_node* current) {
token_type type = current->value->type;
return (type == t_variable || type == t_literal || type == t_nil || type == t_l_parenthesis);
}
Tree_node* handle_STRING(Token_node** current, bool* parses_correctly) {
if ((*current)->value->type == t_string) {
Tree_node* string = make_node(STRING);
string->string_value = (*current)->value->lexeme;
*current = (*current)->next;
return string;
} else {
return parser_fails(parses_correctly);
}
}
Tree_node* make_node(grammar_type type) {
Tree_node* new_node = (Tree_node*)allocate_space(1, sizeof(Tree_node));
new_node->type = type;
return new_node;
}
Tree_node* parser_fails(bool* parses_correctly) {
*parses_correctly = false;
return NULL;
}
bool next_token_is(Token_node** current, int num_possible_tokens, ...) {
if ((num_possible_tokens < 1) || !(*current)) {
return false;
}
va_list token_list;
va_start(token_list, num_possible_tokens);
for (int i = 0; i < num_possible_tokens; i++) {
token_type expected = va_arg(token_list, token_type);
if ((*current)->value->type == expected) {
*current = (*current)->next;
return true;
}
}
return false;
}
void print_tree(Syntax_tree* tree) {
if (tree->program) {
print_tree_node(tree->program);
} else {
printf("EMPTY TREE");
}
printf("\n");
}
void print_tree_node(Tree_node* node) {
char* node_type = get_node_type(node);
printf("(%s", node_type);
if (node->child1) {
print_tree_node(node->child1);
} else {
printf("()");
}
if (node->child2) {
print_tree_node(node->child2);
}
if (node->child3) {
print_tree_node(node->child3);
}
printf(")");
}
char* get_node_type(Tree_node* node) {
switch (node->type) {
case PROG:
return "PROG";
case INSTRCTS:
return "INSTRCTS";
case INSTRCT:
return "INSTRCT";
case FUNC:
return "FUNC";
case RETFUNC:
return "RETFUNC";
case LISTFUNC:
return "LISTFUNC";
case INTFUNC:
return "INTFUNC";
case BOOLFUNC:
return "BOOLFUNC";
case IOFUNC:
return "IOFUNC";
case SET:
return "SET";
case PRINT:
return "PRINT";
case IF:
return "IF";
case LOOP:
return "LOOP";
case LIST:
return "LIST";
case VAR:
return "VAR";
case STRING:
return "STRING";
case LITERAL:
return "LITERAL";
case NIL:
return "NIL";
default:
return "null";
}
}
void free_tree(Syntax_tree* tree) {
if (tree->program) {
free_node(tree->program);
}
free(tree);
}
void free_node(Tree_node* node) {
if (node->child1) {
free_node(node->child1);
node->child1 = NULL;
}
if (node->child2) {
free_node(node->child2);
node->child2 = NULL;
}
if (node->child3) {
free_node(node->child3);
node->child3 = NULL;
}
free(node);
}