-
Notifications
You must be signed in to change notification settings - Fork 0
/
sanity.c
456 lines (420 loc) · 15.6 KB
/
sanity.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
/*
* Project: IFJ16, a programming language interpreter
* FIT VUT Brno
* Authors: xzaryb00 - Zarybnický Jakub
* xtamas01 - Tamaškovič Marek
* xvasko12 - Vaško Martin
* xvasko14 - Vaško Michal
* xzales12 - Záleský Jiří
*/
#include "sanity.h"
static SymbolTable *symTable;
static SymbolTable *localTable;
static bool hasReturnCommand;
static char *className;
void runSemanticAnalysis(SymbolTable *s) {
symTable = s;
Node *root = symTable->root;
/* iterates global table with Root Nodes and function which should compare operators, types, arguments, classes , methods etc... */
checkMainRunPresence();
table_iterate_fn(root, checkReturnPresence);
table_iterate_fn(root, checkTopLevel);
table_iterate_fn(root, checkOperatorAssignmentType);
symTable = NULL;
}
void freeSemantic() {
if (localTable != NULL) {
freeSymbolTable(localTable);
}
if (className != NULL) {
free_c(className);
}
}
//----- Check return type -----
void checkReturnPresenceC(Function *f, Command *c) {
(void) f;
if (c->type == C_RETURN) {
hasReturnCommand = true;
}
}
void checkReturnPresence(Function *f) {
hasReturnCommand = false;
traverseCommands(f, checkReturnPresenceC, NULL);
if (f->returnType != T_VOID && !hasReturnCommand) {
freeSemantic();
fprintf(stderr, "In function %s:\n", f->name);
MERROR(ERR_RUNTIME_UNINITIALIZED, "No 'return' in a non-void function.");
}
}
void checkMainRunPresence() {
Node *tmp = table_lookup(symTable, "Main");
if (tmp == NULL || tmp->type != N_CLASS) {
freeSemantic();
MERROR(ERR_SEM_UNDEFINED, "Missing class Main.");
}
tmp = table_lookup(symTable, "Main.run");
if (tmp == NULL || tmp->type != N_FUNCTION) {
freeSemantic();
MERROR(ERR_SEM_UNDEFINED, "Missing run method in Main.");
}
if (tmp->data.function->returnType != T_VOID){
freeSemantic();
MERROR(ERR_SEM_UNDEFINED, "Run is not void function.");
}
}
void checkOperatorAssignmentTypeC(Function *f, Command *c) {
if (c == NULL || f == NULL)
return;
Node *n;
ValueType ltype, rtype;
switch (c->type) {
case C_DECLARE:
table_insert_dummy(localTable, c->data.declare);
break;
case C_DEFINE:
ltype = c->data.define.declaration.type;
rtype = getExpressionType(c->data.define.expr, hasString(c->data.define.expr));
if (!isAssignCompatible(ltype, rtype)) {
freeSemantic();
if (c->data.define.expr->type == E_FUNCALL && rtype != ltype && rtype == T_VOID){
FERROR(ERR_RUNTIME_UNINITIALIZED, "Cannot assign a(n) %s to %s.\n",
showValueType(rtype), showValueType(ltype));
}
FERROR(ERR_SEM_TYPECHECK, "Cannot assign a(n) %s to %s.\n",
showValueType(rtype), showValueType(ltype));
}
table_insert_dummy(localTable, c->data.define.declaration);
break;
case C_ASSIGN:
/* EXAMPLE: a = 5, first lookup Node if exists then do value type compare, and chceck assign to a function */
n = table_lookup_either(symTable, localTable, className,
c->data.assign.name);
if (n == NULL) {
freeSemantic();
fprintf(stderr, "In function %s:\n", f->name);
FERROR(ERR_SEM_UNDEFINED,
"Trying to assign to an undefined variable '%s'",
c->data.assign.name);
}
if (n->type != N_VALUE) {
freeSemantic();
fprintf(stderr, "In function %s:\n", f->name);
MERROR(ERR_SEM_UNDEFINED, "Can't assign to a function");
}
ltype = n->data.value->type;
rtype = getExpressionType(c->data.assign.expr, hasString(c->data.assign.expr));
if (!isAssignCompatible(ltype, rtype)) {
freeSemantic();
FERROR(ERR_SEM_TYPECHECK, "Cannot assign a(n) %s to %s.\n",
showValueType(rtype), showValueType(ltype));
}
break;
case C_IF:
rtype = getExpressionType(c->data.ifC.cond, hasString(c->data.ifC.cond));
if (!isAssignCompatible(T_BOOLEAN, rtype)) {
freeSemantic();
MERROR(ERR_SEM_TYPECHECK,
"'if' condition requires a boolean expression\n");
}
break;
case C_WHILE:
rtype = getExpressionType(c->data.whileC.cond, hasString(c->data.whileC.cond));
if (!isAssignCompatible(T_BOOLEAN, rtype)) {
freeSymbolTable(localTable);
MERROR(ERR_SEM_TYPECHECK,
"'while' condition requires a boolean expression\n");
}
break;
case C_DO_WHILE:
rtype = getExpressionType(c->data.doWhileC.cond, hasString(c->data.doWhileC.cond));
if (!isAssignCompatible(T_BOOLEAN, rtype)) {
freeSemantic();
MERROR(ERR_SEM_TYPECHECK,
"'do-while' condition requires a boolean expression\n");
}
break;
case C_FOR:
if (c->data.forC.initial != NULL) {
ltype = c->data.forC.var.type;
rtype = getExpressionType(c->data.forC.initial, hasString(c->data.forC.initial));
if (!isAssignCompatible(ltype, rtype)) {
freeSemantic();
FERROR(ERR_SEM_TYPECHECK,
"Can't assign %s to %s in 'for' initialization",
showValueType(ltype), showValueType(rtype));
}
}
table_insert_dummy(localTable, c->data.forC.var);
/* compare expression type of for condition if both types are ocmpatible */
rtype = getExpressionType(c->data.forC.cond, hasString(c->data.forC.cond));
if (!isAssignCompatible(T_BOOLEAN, rtype)) {
freeSemantic();
MERROR(ERR_SEM_TYPECHECK,
"'for' condition requires a boolean expression\n");
}
break;
case C_EXPRESSION:
getExpressionType(c->data.expr, hasString(c->data.expr)); //check for undefineds etc.
break;
case C_RETURN:
if (f->returnType == T_VOID) {
if (c->data.expr != NULL) {
freeSemantic();
fprintf(stderr, "In function %s:\n", f->name);
MERROR(ERR_SEM_TYPECHECK,
"Returning a value from a void function");
}
} else {
if (c->data.expr == NULL) {
freeSemantic();
fprintf(stderr, "In function %s:\n", f->name);
MERROR(ERR_SEM_TYPECHECK,
"Not returning a value from a non-void function");
}
ltype = f->returnType;
rtype = getExpressionType(c->data.expr, hasString(c->data.expr));
if (!isAssignCompatible(ltype, rtype)) {
freeSemantic();
FERROR(ERR_SEM_TYPECHECK,
"Can't return a %s from a function returning %s\n",
showValueType(rtype), showValueType(ltype));
}
}
break;
case C_BLOCK:
case C_CONTINUE:
case C_BREAK:
break;
}
}
void checkOperatorAssignmentTypeF(Command *c) {
freeNode(table_remove(localTable, c->data.forC.var.name));
}
void checkOperatorAssignmentType(Function *f) {
className = getClassName(f->name);
localTable = createSymbolTable();
Declaration *arg = f->argHead;
while (arg != NULL) {
table_insert_dummy(localTable, *arg);
arg = arg->next;
}
traverseCommands(f, checkOperatorAssignmentTypeC,
checkOperatorAssignmentTypeF);
freeSemantic();
}
void checkTopLevelInner(Function *f, Command *c, bool cycle) {
while (c != NULL) {
switch (c->type) {
case C_DECLARE:
freeSemantic();
fprintf(stderr, "In function %s:\n", f->name);
MERROR(ERR_SYNTAX, "C_DECLARE located in a nested block.");
break;
case C_DEFINE:
freeSemantic();
fprintf(stderr, "In function %s:\n", f->name);
MERROR(ERR_INTERNAL, "C_DEFINE located in a nested block.");
break;
case C_CONTINUE:
/* same as break, cannot continue without loop */
if (cycle == false) {
freeSemantic();
fprintf(stderr, "In function %s:\n", f->name);
MERROR(ERR_INTERNAL, "C_CONTINUE located outside of a cycle.");
}
break;
case C_BREAK:
/* same as continue, cannot break without loop */
if (cycle == false) {
freeSemantic();
fprintf(stderr, "In function %s:\n", f->name);
MERROR(ERR_INTERNAL, "C_BREAK located outside of a cycle.");
}
break;
case C_IF:
checkTopLevelInner(f, c->data.ifC.thenBlock.head, cycle);
checkTopLevelInner(f, c->data.ifC.elseBlock.head, cycle);
break;
case C_WHILE:
checkTopLevelInner(f, c->data.whileC.bodyBlock.head, true);
break;
case C_DO_WHILE:
checkTopLevelInner(f, c->data.doWhileC.bodyBlock.head, true);
break;
case C_FOR:
checkTopLevelInner(f, c->data.forC.iter, false);
checkTopLevelInner(f, c->data.forC.bodyBlock.head, true);
break;
case C_BLOCK:
checkTopLevelInner(f, c->data.block.head, cycle);
break;
case C_ASSIGN:
case C_EXPRESSION:
case C_RETURN:
break;
}
c = c->next;
}
}
// Application at traversing of symbolTable
void checkTopLevel(Function *f) {
for (Command *c = f->body.head; c != NULL; c = c->next) {
switch (c->type) {
case C_CONTINUE:
freeSemantic();
fprintf(stderr, "In function %s:\n", f->name);
MERROR(ERR_INTERNAL, "C_CONTINUE located outside of a cycle.");
break;
case C_BREAK:
freeSemantic();
fprintf(stderr, "In function %s:\n", f->name);
MERROR(ERR_INTERNAL, "C_DEFINE located outside of a cycle.");
break;
case C_IF:
checkTopLevelInner(f, c->data.ifC.thenBlock.head, false);
checkTopLevelInner(f, c->data.ifC.elseBlock.head, false);
break;
case C_WHILE:
checkTopLevelInner(f, c->data.whileC.bodyBlock.head, true);
break;
case C_DO_WHILE:
checkTopLevelInner(f, c->data.doWhileC.bodyBlock.head, true);
break;
case C_FOR:
checkTopLevelInner(f, c->data.forC.iter, false);
checkTopLevelInner(f, c->data.forC.bodyBlock.head, true);
break;
case C_BLOCK:
checkTopLevelInner(f, c->data.block.head, false);
break;
default:
break;
}
}
}
bool hasString(Expression *e) {
Node *n;
switch (e->type) {
case E_VALUE:
return e->data.value->type == T_STRING;
case E_FUNCALL:
n = table_lookup_either(symTable, NULL, className, e->data.funcall.name);
if (n == NULL || n->type != N_FUNCTION) {
return false;
}
return n->data.function->returnType == T_STRING;
case E_REFERENCE:
n = table_lookup_either(symTable, localTable, className, e->data.reference);
if (n == NULL || n->type != N_VALUE) {
return false;
}
return n->data.value->type == T_STRING;
case E_BINARY:
return hasString(e->data.binary.left) || hasString(e->data.binary.right);
case E_UNARY:
return hasString(e->data.unary.e);
}
return false;
}
ValueType getExpressionType(Expression *e, bool hasString_) {
if (e == NULL) {
freeSemantic();
MERROR(ERR_INTERNAL, "NULL expression");
}
Node *n;
Expression *arg;
ValueType t;
switch (e->type) {
case E_VALUE:
return e->data.value->type;
case E_FUNCALL:
/* EXAMPLE - foo(a,b,c); check wether function exists(symTable[global table] is enough to check, no local functions allowed)*/
n = table_lookup_either(symTable, NULL, className, e->data.funcall.name);
if (n == NULL) {
freeSemantic();
FERROR(ERR_SEM_UNDEFINED,
"Trying to call a non-existent function %s",
e->data.reference);
}
/* check type of Node if it is function, then its arguements */
if (n->type != N_FUNCTION) {
freeSemantic();
FERROR(ERR_SEM_TYPECHECK, "Trying to call a variable %s",
e->data.funcall.name);
}
if (e->data.funcall.argCount != n->data.function->argCount) {
freeSemantic();
FERROR(ERR_SEM_TYPECHECK,
"Calling %s with %d arguments, expecting %d",
e->data.funcall.name, e->data.funcall.argCount,
n->data.function->argCount);
}
arg = e->data.funcall.argHead;
for (int argNum = 0; arg != NULL; argNum++, arg = arg->next) {
Declaration *d = n->data.function->argHead;
for (int i = n->data.function->argCount - 1; i --> argNum;)
d = d->next;
ValueType rtype = getExpressionType(arg, hasString(arg));
if (!isAssignCompatible(d->type, rtype) && strcmp("ifj16.print", e->data.funcall.name) != 0) {
fprintf(stderr,
"Cannot convert %s to %s while calling function %s\n",
showValueType(rtype), showValueType(d->type),
n->data.function->name);
freeSemantic();
ERROR(ERR_SEM_TYPECHECK);
}
}
return n->data.function->returnType;
case E_REFERENCE:
n = table_lookup_either(symTable, localTable, className,
e->data.reference);
if (n == NULL) {
freeSemantic();
FERROR(ERR_SEM_UNDEFINED,
"Trying to reference a non-existent variable %s",
e->data.reference);
}
if (n->type != N_VALUE) {
freeSemantic();
MERROR(ERR_SEM_TYPECHECK, "Trying to reference a function");
}
return n->data.value->type;
case E_BINARY:
t = getBinExpType(e->data.binary.op,
getExpressionType(e->data.binary.left, hasString_),
getExpressionType(e->data.binary.right, hasString_));
if (t == T_VOID && !hasString_) {
FERROR(ERR_SEM_TYPECHECK, "Wrong operator types for operation %s: %s, %s.\n",
showBinaryOperation(e->data.binary.op),
showValueType(getExpressionType(e->data.binary.left, hasString_)),
showValueType(getExpressionType(e->data.binary.right, hasString_)));
}
return t;
case E_UNARY:
t = getExpressionType(e->data.unary.e, hasString_);
switch (e->data.unary.op) {
case U_ID:
return t;
case U_PREINC:
case U_POSTINC:
case U_PREDEC:
case U_POSTDEC:
case U_NEG:
if (t == T_INTEGER || t == T_DOUBLE) {
return t;
}
break;
case U_NOT:
if (t == T_BOOLEAN) {
return t;
}
break;
}
freeSemantic();
fprintf(stderr,"Wrong operand type for operation %s: %s.\n",
showUnaryOperation(e->data.unary.op), showValueType(t));
ERROR(ERR_SEM_TYPECHECK);
}
//...and appease the compiler
return T_VOID;
}