-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.c
580 lines (434 loc) · 12.9 KB
/
interpreter.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <parser_helper.h>
#include "interpreter.h"
vardecl_t* getvar(char* name, scope_t* scope) {
if(scope == NULL)
return NULL;
vardecl_t* vd = scope->vars;
while(vd != NULL && strcmp(vd->identifier, name) != 0) {
vd = vd->next;
}
if(vd == NULL)
return getvar(name, scope->parent);
return vd;
}
vardecl_t* newvar(char* name, scope_t* scope) {
vardecl_t* vd;
if(scope->vars == NULL) {
scope->vars = malloc(sizeof(vardecl_t));
vd = scope->vars;
} else {
vd = scope->vars;
while(vd->next != NULL) {
vd = vd->next;
}
vd->next = malloc(sizeof(vardecl_t));
vd = vd->next;
}
vd->identifier = name;
vd->initialised = 0;
vd->containingScope = scope;
vd->next = NULL;
return vd;
}
result_t* interpreter_handledecl(ast_decl_t* t, scope_t* scope) {
result_t* res = interpreter_handlenewident(t->ident, scope);
if(res->type == RES_ERROR)
return res;
res->item.decl->mut = t->mut;
if(t->type == NULL) {
res->item.decl->type = VAR_UNKNOWN;
} else if(strcmp(t->type, "i32") == 0) {
res->item.decl->type = VAR_INT;
} else if(strcmp(t->type, "bool") == 0) {
res->item.decl->type = VAR_BOOL;
}
return res;
}
result_t* interpreter_handlenewident(ast_ident_t* t, scope_t* scope) {
result_t* res = malloc(sizeof(result_t));
vardecl_t* vd = getvar(t->ident, scope);
if(vd != NULL && vd->containingScope == scope) {
res->type = RES_ERROR;
char* a = "Error - Variable ";
char* b = " already exists\n";
res->item.error = malloc(strlen(a) + strlen(b) + strlen(t->ident) + 1);
sprintf(res->item.error, "%s%s%s", a, t->ident, b);
return res;
}
vd = newvar(t->ident, scope);
vd->type = VAR_UNKNOWN;
vd->next = NULL;
res->type = RES_DECL;
res->item.decl = vd;
return res;
}
result_t* interpreter_handleident(ast_ident_t* t, scope_t* scope) {
result_t* res = malloc(sizeof(result_t));
vardecl_t* vd;
vd = getvar(t->ident, scope);
if(vd == NULL) {
res->type = RES_ERROR;
char* s = "Error - Reference to undefined variable ";
res->item.error = malloc(strlen(s) + strlen(t->ident) + 2);
sprintf(res->item.error, "%s%s\n", s, t->ident);
return res;
}
if(vd->initialised == 0) {
res->type = RES_ERROR;
char* s = "Error - Reference to uninitialised variable ";
res->item.error = malloc(strlen(s) + strlen(t->ident) + 2);
sprintf(res->item.error, "%s%s\n", s, t->ident);
return res;
}
switch(vd->type) {
case VAR_UNKNOWN:
res->type = RES_ERROR;
char* s = "Error - Attempted to reference uninitialised variable ";
res->item.error = malloc(strlen(s) + strlen(t->ident) + 2);
sprintf(res->item.error, "%s%s\n", s, t->ident);
return res;
case VAR_INT:
res->type = RES_INT;
res->item.iVal = vd->item.iVal;
return res;
case VAR_BOOL:
res->type = RES_BOOL;
res->item.bVal = vd->item.bVal;
return res;
case VAR_NONE:
res->type = RES_NONE;
return res;
}
}
result_t* interpreter_handlebinop(ast_binop_t* t, scope_t* scope) {
result_t* res = malloc(sizeof(result_t));
res->type = RES_INT;
result_t* left = interpreter_handleexpr(t->left, scope);
result_t* right = interpreter_handleexpr(t->right, scope);
if(left->type == RES_ERROR) {
free(res);
free(right);
return left;
}
if(right->type == RES_ERROR) {
free(res);
free(left);
return right;
}
switch(t->type) {
case AST_BINOP_ADD:
res->item.iVal = left->item.iVal + right->item.iVal;
break;
case AST_BINOP_SUB:
res->item.iVal = left->item.iVal - right->item.iVal;
break;
case AST_BINOP_MUL:
res->item.iVal = left->item.iVal * right->item.iVal;
break;
case AST_BINOP_DIV:
res->item.iVal = left->item.iVal / right->item.iVal;
break;
case AST_BINOP_MOD:
res->item.iVal = left->item.iVal % right->item.iVal;
break;
case AST_BINOP_EQUAL:
res->type = RES_BOOL;
if(left->type != right->type) {
res->item.bVal = 0;
} else if(left->type == RES_INT) {
res->item.bVal = left->item.iVal == right->item.iVal ? 1 : 0;
} else if(left->type == RES_BOOL) {
res->item.bVal = left->item.bVal == right->item.bVal ? 1 : 0;
}
break;
case AST_BINOP_NOTEQUAL:
res->type = RES_BOOL;
if(left->type != right->type) {
res->item.bVal = 1;
} else if(left->type == RES_INT) {
res->item.bVal = left->item.iVal == right->item.iVal ? 0 : 1;
} else if(left->type == RES_BOOL) {
res->item.bVal = left->item.bVal == right->item.bVal ? 0 : 1;
}
break;
case AST_BINOP_LESSTHAN:
res->type = RES_BOOL;
if(left->type != right->type) {
res->item.bVal = 0;
} else if(left->type == RES_INT) {
res->item.bVal = left->item.iVal < right->item.iVal ? 1 : 0;
} else if(left->type == RES_BOOL) {
res->item.bVal = 0;
}
break;
case AST_BINOP_GREATERTHAN:
res->type = RES_BOOL;
if(left->type != right->type) {
res->item.bVal = 0;
} else if(left->type == RES_INT) {
res->item.bVal = left->item.iVal > right->item.iVal ? 1 : 0;
} else if(left->type == RES_BOOL) {
res->item.bVal = 0;
}
break;
case AST_BINOP_LESSOREQ:
res->type = RES_BOOL;
if(left->type != right->type) {
res->item.bVal = 0;
} else if(left->type == RES_INT) {
res->item.bVal = left->item.iVal <= right->item.iVal ? 1 : 0;
} else if(left->type == RES_BOOL) {
res->item.bVal = 0;
}
break;
case AST_BINOP_GREATEROREQ:
res->type = RES_BOOL;
if(left->type != right->type) {
res->item.bVal = 0;
} else if(left->type == RES_INT) {
res->item.bVal = left->item.iVal >= right->item.iVal ? 1 : 0;
} else if(left->type == RES_BOOL) {
res->item.bVal = 0;
}
break;
}
free(left);
free(right);
return res;
}
result_t* interpreter_handleexpr(ast_expr_t* t, scope_t* scope) {
result_t* res;
switch(t->type) {
case AST_EXPR_INT:
res = malloc(sizeof(result_t));
res->type = RES_INT;
res->item.iVal = t->item.val;
return res;
case AST_EXPR_BOOL:
res = malloc(sizeof(result_t));
res->type = RES_BOOL;
res->item.bVal = t->item.bVal;
return res;
case AST_EXPR_IDENT:
return interpreter_handleident(t->item.ident, scope);
case AST_EXPR_BINOP:
return interpreter_handlebinop(t->item.binop, scope);
case AST_EXPR_BLOCK:
return interpreter_handleblock(t->item.block, scope);
case AST_EXPR_IFBLK:
return interpreter_handlecond(t->item.ifblk, scope);
}
}
result_t* interpreter_handleassign(ast_assign_t* t, scope_t* scope) {
result_t* res;
vardecl_t* vd;
switch(t->type) {
case AST_ASSIGN_DECL:
res = interpreter_handledecl(t->item.decl, scope);
if(res->type == RES_ERROR)
return res;
vd = res->item.decl;
free(res);
break;
case AST_ASSIGN_IDENT:
vd = getvar(t->item.ident->ident, scope);
if(vd->mut == 0) {
res = malloc(sizeof(result_t));
res->type = RES_ERROR;
char* s = "Error - attempted to assign to immutable variable ";
res->item.error = malloc(strlen(s) + strlen(vd->identifier) + 2);
sprintf(res->item.error, "%s%s\n", s, vd->identifier);
return res;
}
break;
}
res = interpreter_handleexpr(t->value, scope);
if(res->type == RES_ERROR)
return res;
switch(res->type) {
case RES_INT:
if(vd->type == VAR_INT) {
vd->item.iVal = res->item.iVal;
vd->initialised = 1;
} else if(vd->type == VAR_UNKNOWN && t->type == AST_ASSIGN_DECL) {
vd->item.iVal = res->item.iVal;
vd->type = VAR_INT;
vd->initialised = 1;
} else {
free(res);
res = malloc(sizeof(result_t));
res->type = RES_ERROR;
char* s = "Error - type mismatch when assigning to ";
res->item.error = malloc(strlen(s) + strlen(vd->identifier) + 2);
sprintf(res->item.error, "%s%s\n", s, vd->identifier);
return res;
}
break;
case RES_BOOL:
if(vd->type == VAR_BOOL) {
vd->item.bVal = res->item.bVal;
vd->initialised = 1;
} else if(vd->type == VAR_UNKNOWN && t->type == AST_ASSIGN_DECL) {
vd->item.bVal = res->item.bVal;
vd->type = VAR_BOOL;
vd->initialised = 1;
} else {
free(res);
res = malloc(sizeof(result_t));
res->type = RES_ERROR;
char* s = "Error - type mismatch when assigning to ";
res->item.error = malloc(strlen(s) + strlen(vd->identifier) + 2);
sprintf(res->item.error, "%s%s\n", s, vd->identifier);
return res;
}
break;
case RES_NONE:
if(vd->type == VAR_NONE || (vd->type == VAR_UNKNOWN && t->type == AST_ASSIGN_DECL)) {
vd->type = VAR_NONE;
vd->initialised = 1;
} else {
free(res);
res = malloc(sizeof(result_t));
res->type = RES_ERROR;
char* s = "Error - type mismatch when assigning to ";
res->item.error = malloc(strlen(s) + strlen(vd->identifier) + 2);
sprintf(res->item.error, "%s%s\n", s, vd->identifier);
return res;
}
break;
}
free(res);
res = malloc(sizeof(result_t));
res->type = RES_NONE;
return res;
}
result_t* interpreter_handlecond(ast_cond_t* t, scope_t* scope) {
result_t* expr = interpreter_handleexpr(t->expr, scope);
if(expr->type == RES_BOOL && expr->item.bVal == 1) {
free(expr);
return interpreter_handleblock(t->block, scope);
} else if(expr->type != RES_BOOL) {
free(expr);
result_t* res = malloc(sizeof(result_t));
res->type = RES_ERROR;
res->item.error = strdup("Error - Attempted to branch on non-boolean value\n");
return res;
} else {
free(expr);
switch(t->type) {
case AST_COND_BARE:
break;
case AST_COND_ELSE:
return interpreter_handleblock(t->item.elseblk, scope);
break;
case AST_COND_ELIF:
return interpreter_handlecond(t->item.elseif, scope);
break;
}
}
result_t* res = malloc(sizeof(result_t));
res->type = RES_NONE;
return res;
}
result_t* interpreter_handlestmt(ast_stmt_t* t, scope_t* scope) {
switch(t->type) {
case AST_STMT_ASSIGN:
return interpreter_handleassign(t->item.assign, scope);
case AST_STMT_EXPR:
return interpreter_handleexpr(t->item.expr, scope);
case AST_STMT_DECL:
return interpreter_handledecl(t->item.decl, scope);
case AST_STMT_COND:
return interpreter_handlecond(t->item.cond, scope);
case AST_STMT_BLOCK:
return interpreter_handleblock(t->item.block, scope);
case AST_STMT_WHILE:
return interpreter_handlewhile(t->item.whileblock, scope);
case AST_STMT_CONT:
{
result_t* res = malloc(sizeof(result_t));
res->type = RES_CONT;
return res;
}
}
}
result_t* interpretloop(ast_stmt_t* t, scope_t* scope) {
while(t != NULL) {
result_t* res = interpreter_handlestmt(t, scope);
switch(res->type) {
case RES_INT:
printf("%d\n", res->item.iVal);
break;
case RES_BOOL:
printf("%s\n", res->item.bVal == 1 ? "true" : "false");
break;
case RES_ERROR:
printf("%s\n", res->item.error);
break;
case RES_CONT:
return res;
default:
break;
}
free(res);
t = t->next;
}
result_t* ret = malloc(sizeof(result_t));
ret->type = RES_NONE;
return ret;
}
result_t* interpreter_handlewhile(ast_while_t* loop, scope_t* scope) {
result_t* inloop = interpreter_handleexpr(loop->cond, scope);
if(inloop->type == RES_ERROR) {
return inloop;
} else if(inloop->type != RES_BOOL) {
free(inloop);
result_t* res = malloc(sizeof(result_t));
res->type = RES_ERROR;
res->item.error = strdup("Error - Attempted to execute while loop with non-boolean condition\n");
return res;
}
while(inloop->item.bVal == 1) {
result_t* tmp = interpreter_handleblock(loop->block, scope);
if(tmp->type == RES_ERROR) {
free(inloop);
return tmp;
}
free(inloop);
inloop = interpreter_handleexpr(loop->cond, scope);
if(inloop->type == RES_ERROR) {
return inloop;
} else if(inloop->type != RES_BOOL) {
free(inloop);
result_t* res = malloc(sizeof(result_t));
res->type = RES_ERROR;
res->item.error = strdup("Error - Attempted to execute while loop with non-boolean condition\n");
return res;
}
}
free(inloop);
inloop = malloc(sizeof(result_t));
inloop->type = RES_NONE;
return inloop;
}
result_t* interpreter_handleblock(ast_block_t* block, scope_t* scope) {
scope_t* newScope = malloc(sizeof(scope_t));
newScope->parent = scope;
newScope->vars = NULL;
if(block->first != NULL) {
result_t* res = interpretloop(block->first, newScope);
if(res->type = RES_CONT)
return res;
}
if(block->last != NULL) {
return interpreter_handleexpr(block->last, scope);
} else {
result_t* res = malloc(sizeof(result_t));
res->type = RES_NONE;
return res;
}
}