-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalcInterpreter.c
416 lines (360 loc) · 14.7 KB
/
calcInterpreter.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
/* Bortoli Gianluca 159993 */
/*
* This file contains the interpreter, which is mainly the ex function
* It evauates every node, through the switch
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "myCalc.h"
#include "y.tab.h"
/*
* Execute the parse tree
* returns conNodeType to manage the given types, not only int
*/
conNodeType * ex(nodeType *p) {
if (p == 0){ //if node doesn't exist, exit
return 0;
}
switch(p->type) { //check wich type of node it is
// possible nodeDic, nodeCon, nodeId, nodeOpr (see myCalc.h enum)
case nodeDic: {
//check if var already declared earlier
if (getsym(p->dic.name)) {
fprintf(stdout, "%s is already declared\n", p->dic.name);
exit(1);
}
//if not decleared, put in symbol table its name and type
putsym(p->dic.name, p->dic.type);
return 0;
}
case nodeCon: {
// return directly the constant node of p
return &(p->con);
}
case nodeId: {
//check if an id with such name is already declared
symrec * s = getsym(p->id.name);
if(s == NULL){
fprintf(stdout, "%s variable does not exist\n", p->id.name);
exit(1);
}
conNodeType * r = malloc(sizeof(r));
r->type = s->type; //type is the one of the var in the sym table
switch(s->type){ //assign value depending on the type
case INTTYPE:
r->i = s->i;
break;
case REALTYPE:
r->r = s->r;
break;
case BOOLTYPE:
r->b = s->b;
break;
default:
yyerror("Type not recognized.");
}
return r;
}
case nodeOpr: {
switch(p->opr.oper) {
case WHILE: {
while(coercion(ex(p->opr.op[0]), BOOLTYPE)->b)
ex(p->opr.op[1]);
return 0;
}
case FOR: {
//assignment before for statement
ex(opr(EQ, 2, p->opr.op[0], p->opr.op[1]));
conNodeType * a;
//fake for with while
while(ex(opr(LT, 2, p->opr.op[0], p->opr.op[2]))->b) {
// body
ex(p->opr.op[3]);
// increment of one
a = ex(opr(PLUS, 2, p->opr.op[0], con(1,INTTYPE)));
ex(opr(EQ, 2, p->opr.op[0], con(getTyped(a), a->type)));
}
return 0;
}
case IF: {
if (coercion(ex(p->opr.op[0]), BOOLTYPE)->b){
ex(p->opr.op[1]);
}
else if (p->opr.nops > 2) {
ex(p->opr.op[2]);
}
return 0;
}
/*
* The print function is already polymorphic
* This is a sort of workaround in order to pass "early" tests.
* I implemented the java-style print before writing printInt e printReal
*/
case PRINTINT: {
conNodeType * print = ex(p->opr.op[0]);
if (print->type != INTTYPE) {
yyerror("The function printInt can print only integers.");
}
printf("%d\n", print->i);
return 0;
}
case PRINTREAL: {
conNodeType * print = ex(p->opr.op[0]);
if (print->type != REALTYPE) {
yyerror("The function printReal can print only float.");
}
//a label can only be part of a statement and a declaration is not a statement
// 46 is the maximum length of float in C
// I didn't figured out a smart(er) way to do this
char * tmp = (char *)malloc(46 + 1);
sprintf(tmp, "%f\n", print->r);
// substitute comma with dot, again
for(int i = 0 ; ; i++){
if(tmp[i] == '.'){
tmp[i] = ',';
break;
}
}
printf("%s\n", tmp);
free(tmp);
return 0;
}
case PRINT: {
// I already do type checking in the print function
// switch over types in order to print in the right way the value
conNodeType * print = ex(p->opr.op[0]);
switch(print->type){
case INTTYPE:
printf("%d\n", print->i);
break;
case REALTYPE:
// "a label can only be part of a statement and a declaration is not a statement"
;
// 46 is the maximum length of float in C
// I didn't figured out a smart(er) way to do this
char * tmp = (char *)malloc(46 + 1);
sprintf(tmp, "%f", print->r);
// substitute comma with dot, again
for(int i = 0 ; ; i++){
if(tmp[i] == '.'){
tmp[i] = ',';
break;
}
}
printf("%s\n", tmp);
free(tmp);
break;
case BOOLTYPE:
if (print->b)
printf("true\n");
else
printf("false\n");
break;
default:
yyerror("You tried to print an unauthorized type.");
}
return 0;
}
case SEMICOLON: {
ex(p->opr.op[0]);
return ex(p->opr.op[1]);
}
case EQ: {
symrec * s = getsym(p->opr.op[0]->id.name);
if(s == NULL){
fprintf(stdout, "There is not '%s' varibale in the symbol table\n", p->opr.op[0]->id.name);
exit(1);
}
// coercion over types on assignment before
conNodeType * val = coercion(ex(p->opr.op[1]), s->type);
if(s->type == val->type){
switch (val->type) {
case INTTYPE:
s->i = val->i;
break;
case REALTYPE:
s->r = val->r;
break;
case BOOLTYPE:
s->b = val->b;
break;
default:
yyerror("Not allowed type used.");
}
} else {
if(val->type == INTTYPE){
if(s->type == BOOLTYPE){
s->b = val->i != 0;
} else if(s->type == REALTYPE) {
s->r = val->i;
} else {
yyerror("Not allowed type used.");
}
}
yyerror("Not allowed type used.");
}
return val;
}
case UMINUS: {
conNodeType * a = ex(p->opr.op[0]);
//type checking with a switch is enough this time
switch(a->type) {
case INTTYPE:
return ex(con(-(a->i), INTTYPE));
case REALTYPE:
return ex(con(-(a->r), REALTYPE));
case BOOLTYPE:
default:
yyerror("Unable to execute operation");
}
}
/*
* For every operator, I do coercion between the "biggest type" possible between the two
* Execution on nodes is done only when proper constraint over type is matched
* This is done in: PLUS, MIN, MULT, DIV, LT, GT, GRE, LRE, NE, DBE, AND, OR and NOT
* In case a node of the proper type is matched, an error is raised
*/
case PLUS:{
conNodeType * a = ex(p->opr.op[0]);
conNodeType * b = ex(p->opr.op[1]);
varEnum d = biggestType(a->type,b->type);
coercion(a, d);
coercion(b, d);
if(a->type != BOOLTYPE){
return ex(con((getTyped(a) + getTyped(b)), a->type));
} else {
yyerror("Unable to execute operation");
}
}
case MIN:{
conNodeType * a = ex(p->opr.op[0]);
conNodeType * b = ex(p->opr.op[1]);
varEnum d = biggestType(a->type,b->type);
coercion(a, d);
coercion(b, d);
if(a->type != BOOLTYPE){
return ex(con((getTyped(a) - getTyped(b)), a->type));
} else {
yyerror("Unable to execute operation");
}
}
case MULT:{
conNodeType * a = ex(p->opr.op[0]);
conNodeType * b = ex(p->opr.op[1]);
varEnum d = biggestType(a->type,b->type);
coercion(a, d);
coercion(b, d);
if(a->type != BOOLTYPE){
return ex(con((getTyped(a) * getTyped(b)), a->type));
} else {
yyerror("Unable to execute operation");
}
}
case DIV:{
conNodeType * a = ex(p->opr.op[0]);
conNodeType * b = ex(p->opr.op[1]);
varEnum d = biggestType(a->type,b->type);
coercion(a, d);
coercion(b, d);
if(a->type != BOOLTYPE){
return ex(con((getTyped(a) / getTyped(b)), a->type));
} else {
yyerror("Unable to execute operation");
}
}
case LT:{
conNodeType * a = ex(p->opr.op[0]);
conNodeType * b = ex(p->opr.op[1]);
varEnum d = biggestType(a->type,b->type);
coercion(a, d);
coercion(b, d);
if(a->type != BOOLTYPE){
return ex(con((getTyped(a) < getTyped(b)), BOOLTYPE));
} else {
yyerror("Unable to execute operation");
}
}
case GT:{
conNodeType * a = ex(p->opr.op[0]);
conNodeType * b = ex(p->opr.op[1]);
varEnum d = biggestType(a->type,b->type);
coercion(a, d);
coercion(b, d);
if(a->type != BOOLTYPE){
return ex(con((getTyped(a) > getTyped(b)), BOOLTYPE));
} else {
yyerror("Unable to execute operation");
}
}
case GRE:{
conNodeType * a = ex(p->opr.op[0]);
conNodeType * b = ex(p->opr.op[1]);
varEnum d = biggestType(a->type,b->type);
coercion(a, d);
coercion(b, d);
if(a->type != BOOLTYPE){
return ex(con((getTyped(a) >= getTyped(b)), BOOLTYPE));
} else {
yyerror("Unable to execute operation");
}
}
case LRE:{
conNodeType * a = ex(p->opr.op[0]);
conNodeType * b = ex(p->opr.op[1]);
varEnum d = biggestType(a->type,b->type);
coercion(a, d);
coercion(b, d);
if(a->type != BOOLTYPE){
return ex(con((getTyped(a) <= getTyped(b)), BOOLTYPE));
} else {
yyerror("Unable to execute operation");
}
}
case NE:{
conNodeType * a = ex(p->opr.op[0]);
conNodeType * b = ex(p->opr.op[1]);
varEnum d = biggestType(a->type,b->type);
coercion(a, d);
coercion(b, d);
return ex(con((getTyped(a) != getTyped(b)), BOOLTYPE));
}
case DBE:{
conNodeType * a = ex(p->opr.op[0]);
conNodeType * b = ex(p->opr.op[1]);
varEnum d = biggestType(a->type,b->type);
coercion(a, d);
coercion(b, d);
return ex(con((getTyped(a) == getTyped(b)), BOOLTYPE));
}
case AND:{
conNodeType * a = ex(p->opr.op[0]);
conNodeType * b = ex(p->opr.op[1]);
coercion(a, BOOLTYPE);
coercion(b, BOOLTYPE);
return ex(con((getTyped(a) && getTyped(b)), BOOLTYPE));
}
case OR:{
conNodeType * a = ex(p->opr.op[0]);
conNodeType * b = ex(p->opr.op[1]);
coercion(a, BOOLTYPE);
coercion(b, BOOLTYPE);
return ex(con((getTyped(a) || getTyped(b)), BOOLTYPE));
}
case NOT:{
conNodeType * a = ex(p->opr.op[0]);
coercion(a, BOOLTYPE);
return ex(con(!getTyped(a), BOOLTYPE));
}
default:{
yyerror("Operator not recognized.");
}
}
break;
}
default:
yyerror("Node can not be matched");
}
yyerror("Error!");
exit(1);
}