-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
375 lines (348 loc) · 10.6 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
#include "parser.h"
#include "stack.h"
#include "tokens.h"
#include <stdio.h>
#include <stdlib.h>
// sstack_t *parse_block(sstack_t *stack, int debug);
char *tokttstr(token_type_t t) {
switch (t) {
case FLOAT:
return "float";
case INT:
return "int";
default:
return NULL;
}
}
void nodestrtval(node_t *n) {
// printf("%p\n", n->val);
if (n->tok_class != VALUE)
return;
switch (n->tok_type) {
case INT:
n->val.i = strtol(n->str, NULL, 0);
return;
case FLOAT:
n->val.f = strtof(n->str, NULL);
return;
default:
return;
}
}
int parse_op(sstack_t *stack, sstack_t *op, int debug) {
if (stack->node->tok_class != OPERATION) {
printf("Parser Error: Unexpected `%s`\n", stack->node->str);
return -2;
}
if (stack->node->tok_type == ASG || stack->node->tok_type == INPUT) {
if (stack->node->next == NULL) {
printf("Parser Error: Unexpected end of file.\n");
return -2;
}
if (stack->node->next->tok_class != ID) {
printf("Parser Error: `%s` is nonassignable\n", stack->node->next->str);
return -2;
}
}
if (stack->node->tok_type == ASG) {
push_node(op, pop_node(stack));
token_type_t expd_type = stack->node->tok_type;
char *id_str = stack->node->str;
int line = stack->node->line;
push_node(op, pop_node(stack));
if (stack->node->tok_class == VALUE || stack->node->tok_class == ID) {
if (stack->node->tok_type != expd_type) {
printf(
"Parser Error: attempting to assign `%s` of type %s the value `%s` "
"of type %s on line %d\n",
op->node->str, tokttstr(op->node->tok_type), stack->node->str,
tokttstr(stack->node->tok_type), line);
return -2;
}
nodestrtval(stack->node);
push_node(op, pop_node(stack));
} else if (stack->node->tok_class == OPERATION) {
token_type_t res = parse_op(stack, op, debug);
if (res == -2)
return -2;
if (res != expd_type) {
printf("Parser Error: attempting to assign `%s` of type %s a return "
"value of type %s on line %d\n",
id_str, tokttstr(expd_type), tokttstr(res), line);
return -2;
}
} else {
printf("Parser Error: cannot assign `%s` to `%s` on line %d\n", stack->node->str,
id_str, line);
return -2;
}
return -1;
}
int val_count = 0;
int max_val_count;
token_type_t curr_type;
token_type_t arg_type;
int type_persists = 0;
char *op_str = stack->node->str;
char *op_str2;
switch (stack->node->tok_type) {
case DIV:
case EQ:
case GT:
case LT:
case NOT:
type_persists = 1;
break;
default:
break;
}
switch (stack->node->tok_type) {
case PRINT:
case INPUT:
curr_type = -1;
break;
case DIV:
curr_type = FLOAT;
break;
default:
curr_type = INT;
}
switch (stack->node->tok_type) {
case NOT:
case INPUT:
case PRINT:
max_val_count = 1;
break;
default:
max_val_count = 2;
}
if (debug)
printf("n: %s v_count: %i, max_v_count: %i\n", stack->node->str, val_count,
max_val_count);
push_node(op, pop_node(stack));
while (stack->node != NULL && val_count < max_val_count) {
if (debug)
printf("n: %s v_count: %i, max_v_count: %i\n", stack->node->str,
val_count, max_val_count);
if (stack->node->tok_class == VALUE || stack->node->tok_class == ID) {
arg_type = stack->node->tok_type;
nodestrtval(stack->node);
push_node(op, pop_node(stack));
val_count++;
} else if (stack->node->tok_class == OPERATION) {
op_str2 = stack->node->str;
arg_type = parse_op(stack, op, debug);
val_count++;
} else {
printf("Parser Error: unhandled token `%s`\n", stack->node->str);
return -2;
}
if (!type_persists && arg_type == FLOAT && curr_type == INT)
curr_type = FLOAT;
else if (arg_type == -1) {
printf("Parser Error: expected return value, but `%s` does not return "
"anything\n",
op_str2);
return -2;
} else if (arg_type == -2)
return -2;
}
if (val_count < max_val_count) {
printf("Parser Error: Unexpected end of file. `%s` expects %i arguments "
"but got only %i\n",
op_str, max_val_count, val_count);
return -2;
}
return curr_type;
}
sstack_t *parse_line(sstack_t *stack, int debug) {
sstack_t *op_line = init_stack();
node_t *n = stack->node;
switch (n->tok_type) {
case ASG:
case PRINT:
case INPUT: {
if (parse_op(stack, op_line, debug) != -1) {
fprintf(stderr, "Parser Error: parse_line: parse_op failed for ASG, "
"PRINT, or INPUT\n");
return NULL;
}
if (debug)
printf("leaving parse line op\n");
return op_line;
}
default:
break;
}
// TODO print error with more context for value, variable, or operation.
fprintf(stderr, "Parser Error: Unexpected `%s`\n", n->str);
if (n->tok_class == ID)
printf("Parser Tip: Forgot to prepend `asg`, `input` or `print`?\n");
else if (n->tok_class == OPERATION)
printf("Parser Tip: Forgot to prepend `print`, or assign the result to a "
"variable?\n");
return NULL;
}
sstack_t *parse_cond_exp(sstack_t *stack, int debug) {
sstack_t *cond_exp = init_stack();
node_t *cond_n = pop_node(stack);
if (stack->node == NULL) {
printf("Parser Error: Unexpected end of file. Expected conditional "
"expression for `%s.\n",
cond_n->str);
return NULL;
}
if (stack->node->tok_class == OPERATION)
parse_op(stack, cond_exp, debug);
else if (stack->node->tok_class == ID || stack->node->tok_class == VALUE) {
nodestrtval(stack->node);
push_node(cond_exp, pop_node(stack));
} else {
printf("Parser Error: Missing conditional expression for `%s`\n",
cond_n->str);
return NULL;
}
push_node(cond_exp, cond_n);
return cond_exp;
}
int parse_chunk(sstack_t *stack, sstack_t *parsed, int break_point, int debug) {
int success = 1;
while (stack->node != NULL && stack->node->tok_type != break_point) {
if (debug)
printf("pb: %s\n", stack->node->str);
switch (stack->node->tok_type) {
case WHILE: {
if (debug)
printf("Parser Notif. From parse_chunk: Entering parse_cond_exp "
"(while)\n");
if (!join_stacks(parsed, parse_cond_exp(stack, debug), debug)) {
printf("Parser Error: parse_cond_exp failed at WHILE case\n");
success = 0;
break;
}
if (stack->node == NULL) {
printf("Parser Error: Missing exec block for `while` statement at end of file\n");
success = 0;
break;
} else if (stack->node->tok_type != L_BRACE) {
printf("Parser Error: Missing exec block for `while` statement on line %d\n", stack->node->line);
success = 0;
break;
}
if (debug)
printf("Parser Notif. From parse_chunk: Entering while block\n");
append_node(parsed, pop_node(stack));
if (!parse_chunk(stack, parsed, R_BRACE, debug)) {
printf("Parser Error: parse_chunk: failed at while block\n");
success = 0;
break;
}
if (stack->node == NULL) {
printf("Parser Error: Unexpected end of file. `while` statement's "
"exec block not terminated with `}`\n");
success = 0;
break;
}
append_node(parsed, pop_node(stack));
break;
}
case IF: {
if (debug)
printf(
"Parser Notif. From parse_chunk: Entering parse_cond_exp (if)\n");
if (!join_stacks(parsed, parse_cond_exp(stack, debug), debug)) {
printf("Parser Error: parse_cond_exp failed at IF case\n");
success = 0;
break;
}
if (stack->node == NULL) {
printf("Parser Error: Missing exec block for `if` statement at end of file\n");
success = 0;
break;
} else if (stack->node->tok_type != L_BRACE) {
printf("Parser Error: Missing exec block for `if` statement on line %d\n", stack->node->line);
success = 0;
break;
}
if (debug)
printf("Parser Notif. From parse_chunk: Entering if block\n");
append_node(parsed, pop_node(stack));
if (!parse_chunk(stack, parsed, R_BRACE, debug)) {
printf("Parser Error: parse_chunk: failed at if block\n");
success = 0;
break;
}
if (debug)
printf("Parser Notif. From parse_chunk: Leaving if block\n");
if (stack->node == NULL) {
printf("Parser Error: Unexpected end of file. `if` statement's exec "
"block not terminated with `}`\n");
success = 0;
break;
}
append_node(parsed, pop_node(stack));
if (stack->node == NULL)
break;
if (stack->node->tok_type == ELSE) {
append_node(parsed, pop_node(stack));
if (stack->node == NULL) {
printf("Parser Error: Missing exec block or line for `else` at end of file\n");
success = 0;
break;
} else if (stack->node->tok_type != L_BRACE) {
printf("Parser Error: Missing exec block or line for `else` on line %d\n", stack->node->line);
success = 0;
break;
}
if (debug)
printf("Parser Notif. From parse_chunk: Entering else block\n");
append_node(parsed, pop_node(stack));
if (!parse_chunk(stack, parsed, R_BRACE, debug)) {
printf("Parser Error: parse_chunk: failed at else block\n");
success = 0;
break;
}
if (stack->node == NULL) {
printf(
"Parser Error: Unexpected end of file. `else` statement's exec "
"block not terminated with `}`\n");
success = 0;
break;
}
append_node(parsed, pop_node(stack));
}
break;
}
default: {
if (debug)
printf("Parser Notif. From parse_chunk: Entering parse_line\n");
if (!join_stacks(parsed, parse_line(stack, debug), debug)) {
printf("Parser Error: parse_chunk failed at default case\n");
success = 0;
break;
}
if (debug)
printf("Parser Notif. From parse_chunk: Leaving parse_line\n");
break;
}
}
if (success == 0) {
if (debug)
printf("Parser Msg: Exiting parser loop on encounter of error(s)\n");
break;
}
}
return success;
}
sstack_t *parse_tokens(sstack_t *stack, int debug) {
if (debug)
printf("Parser Notif. Debug mode on\n");
if (stack->node == NULL) {
printf("Parser Error: Main block is empty!\n");
return NULL;
}
sstack_t *parsed = init_stack();
if (parse_chunk(stack, parsed, -1, debug))
return parsed;
return NULL;
}