forked from ThrostGunnulf/iJavaC
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsemantic.c
More file actions
397 lines (343 loc) · 10.5 KB
/
semantic.c
File metadata and controls
397 lines (343 loc) · 10.5 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "astNodes.h"
#include "show.h"
#include "symbols.h"
#include "semantic.h"
#include "exitClean.h"
char* currentMethod;
MethodTable* currentLocalTable;
extern ClassTable* symbolsTable;
char* opsOrig[] = {"+", "-", "*", "/", "%", "<", ">", "<=", ">=", "!=", "==", "!", ".length", "&&", "||"};
////
// Semantic error checking functions.
void checkMethodDecl(MethodDecl*);
void checkStmtList(StmtList*);
void checkStmt(Stmt*);
Type checkExpr(Expr*);
int isValidIntLit(char*);
////
// Error functions.
void errorInvalidLiteral(char*);
void errorIncompatibleTypeInStatement(char*, Type, Type);
void errorIncompatibleTypeInStatement2(char*, Type, Type, Type);
void errorOperatorCannotBeApplied(char*, Type);
void errorOperatorCannotBeApplied2(char*, Type, Type);
void errorIncompatibleTypeInAssign(char*, Type, Type);
void errorIncompatibleTypeInAssignArray(char*, Type, Type);
void errorCannotFindSymbol(char*);
void errorIncompatibleArgument(int, char*, Type, Type);
void checkSemantics(Class* myProgram)
{
DeclList* aux = myProgram->declList;
for(; aux != NULL; aux = aux->next)
if(aux->type == METHODDECL)
{
currentMethod = aux->methodDecl->id;
currentLocalTable = getLocalTable(currentMethod);
checkStmtList(aux->methodDecl->stmtList);
}
}
void checkStmtList(StmtList* stmtList)
{
StmtList* aux = stmtList;
for(; aux != NULL; aux = aux->next)
checkStmt(aux->stmt);
}
void checkStmt(Stmt* stmt)
{
if(!stmt)
return;
Type t1, t2, t3;
if(stmt->type == CSTAT)
checkStmtList(stmt->stmtList);
else if(stmt->type == IFELSE)
{
t1 = checkExpr(stmt->expr1);
if(t1 != BOOL_T) //ERROR
errorIncompatibleTypeInStatement("if", t1, BOOL_T);
checkStmt(stmt->stmt1);
checkStmt(stmt->stmt2);
}
else if(stmt->type == WHILE_T)
{
t1 = checkExpr(stmt->expr1);
if(t1 != BOOL_T) //ERROR
errorIncompatibleTypeInStatement("while", t1, BOOL_T);
checkStmt(stmt->stmt1);
}
else if(stmt->type == RETURN_T)
{
t1 = getMethodFromGlobal(currentMethod);
if(stmt->expr1)
t2 = checkExpr(stmt->expr1);
else
t2 = VOID_T;
if(t1 != t2) //ERROR
errorIncompatibleTypeInStatement("return", t2, t1);
}
else if(stmt->type == PRINT_T)
{
t1 = checkExpr(stmt->expr1);
if(t1 != BOOL_T && t1 != INT_T) //ERROR
errorIncompatibleTypeInStatement2("System.out.println", t1, BOOL_T, INT_T);
}
else if(stmt->type == STORE)
{
t1 = getSymbolFromLocalOrGlobal(stmt->id);
t2 = -1;
if(t1 == t2)
errorCannotFindSymbol(stmt->id);
t2 = checkExpr(stmt->expr1);
if(t1 != t2)
errorIncompatibleTypeInAssign(stmt->id, t2, t1);
}
else if(stmt->type == STOREARRAY)
{
t1 = getSymbolFromLocalOrGlobal(stmt->id);
t2 = -1;
if(t1 == t2)
errorCannotFindSymbol(stmt->id);
t2 = checkExpr(stmt->expr1);
if((t1 != INTARRAY && t1 != BOOLARRAY) || t2 != INT_T) //ERROR
errorOperatorCannotBeApplied2("[", t1, t2);
if(t1 == INTARRAY)
t3 = INT_T;
else
t3 = BOOL_T;
t2 = checkExpr(stmt->expr2);
if(t3 != t2) //ERROR
errorIncompatibleTypeInAssignArray(stmt->id, t2, t3);
}
}
Type checkExpr(Expr* expr)
{
Type t1, t2;
if(expr->type == BINOP)
{
switch(expr->op)
{
case PLUS:
case MINUS:
case MUL:
case DIV:
case REM:
t1 = checkExpr(expr->expr1);
t2 = checkExpr(expr->expr2);
if(t1 != INT_T || t2 != INT_T) //ERROR
errorOperatorCannotBeApplied2(opsOrig[expr->op], t1, t2);
return INT_T;
case LESSER:
case GREATER:
case LEQ:
case GEQ:
t1 = checkExpr(expr->expr1);
t2 = checkExpr(expr->expr2);
if(t1 != INT_T || t2 != INT_T) //ERROR
errorOperatorCannotBeApplied2(opsOrig[expr->op], t1, t2);
return BOOL_T;
case DIF:
case EQ:
t1 = checkExpr(expr->expr1);
t2 = checkExpr(expr->expr2);
if(t1 != t2) //ERROR
errorOperatorCannotBeApplied2(opsOrig[expr->op], t1, t2);
return BOOL_T;
case AND_T:
case OR_T:
t1 = checkExpr(expr->expr1);
t2 = checkExpr(expr->expr2);
if(t1 != BOOL_T || t2 != BOOL_T) //ERROR
errorOperatorCannotBeApplied2(opsOrig[expr->op], t1, t2);
return BOOL_T;
}
}
else if(expr->type == UNOP)
{
if(expr->op == PLUS || expr->op == MINUS)
{
t1 = checkExpr(expr->expr1);
if(t1 != INT_T) //ERROR
errorOperatorCannotBeApplied(opsOrig[expr->op], t1);
return INT_T;
}
else if(expr->op == NOT)
{
t1 = checkExpr(expr->expr1);
if(t1 != BOOL_T) //ERROR
errorOperatorCannotBeApplied(opsOrig[expr->op], t1);
return BOOL_T;
}
else if(expr->op == DOTLENGTH_T)
{
t1 = checkExpr(expr->expr1);
if(t1 != INTARRAY && t1 != BOOLARRAY && t1 != STRINGARRAY) //ERROR
errorOperatorCannotBeApplied(opsOrig[expr->op], t1);
return INT_T;
}
}
else if(expr->type == ID_T)
{
Type r, notFound = -1;
r = getSymbolFromLocalOrGlobal(expr->idOrLit);
if(r == notFound)
errorCannotFindSymbol(expr->idOrLit);
return r;
}
else if(expr->type == INTLIT_T)
{
if(!isValidIntLit(expr->idOrLit)) //ERROR
errorInvalidLiteral(expr->idOrLit);
return INT_T;
}
else if(expr->type == BOOLLIT_T)
return BOOL_T;
else if(expr->type == CALL)
{
MethodTable* t = getLocalTable(expr->idOrLit);
if(t == NULL)
errorCannotFindSymbol(expr->idOrLit);
int argNum = 0;
ArgsList* auxArgs = expr->argsList;
MethodTableEntry* auxParams = t->entries;
while(auxArgs != NULL || auxParams != NULL)
{
t1 = VOID_T;
if(auxArgs != NULL)
{
t1 = checkExpr(auxArgs->expr);
auxArgs = auxArgs->next;
}
t2 = VOID_T;
if(auxParams != NULL)
{
t2 = auxParams->type;
auxParams = auxParams->next;
if(auxParams && !auxParams->isParam)
auxParams = NULL;
}
if(t1 != t2) //ERROR
errorIncompatibleArgument(argNum, expr->idOrLit, t1, t2);
argNum++;
}
return getMethodFromGlobal(expr->idOrLit);
}
else if(expr->type == PARSEINT_T)
{
t1 = getSymbolFromLocalOrGlobal(expr->idOrLit);
t2 = -1;
if(t1 == t2)
errorCannotFindSymbol(expr->idOrLit);
t2 = checkExpr(expr->expr1);
if(t1 != STRINGARRAY || t2 != INT_T) //ERROR
errorOperatorCannotBeApplied2("Integer.parseInt", t1, t2);
return INT_T;
}
else if(expr->type == INDEX)
{
t1 = checkExpr(expr->expr1);
t2 = checkExpr(expr->expr2);
if((t1 != INTARRAY && t1 != BOOLARRAY) || t2 != INT_T) //ERROR
errorOperatorCannotBeApplied2("[", t1, t2);
if(t1 == INTARRAY)
return INT_T;
else
return BOOL_T;
}
else if(expr->type == NEWINTARR)
{
t1 = checkExpr(expr->expr1);
if(t1 != INT_T)
errorOperatorCannotBeApplied("new int", t1);
return INTARRAY;
}
else if(expr->type == NEWBOOLARR)
{
t1 = checkExpr(expr->expr1);
if(t1 != INT_T)
errorOperatorCannotBeApplied("new boolean", t1);
return BOOLARRAY;
}
}
int isValidIntLit(char* intlit)
{
int len = strlen(intlit);
if(intlit[0] != '0')
return 1;
if(len >= 1 && intlit[0] == '0' && intlit[1] == 'x')
return 1;
int i;
for(i=1; i < len; i++)
if(intlit[i] == '9' || intlit[i] == '8')
return 0;
return 1;
}
////
// Error functions.
void errorInvalidLiteral(char* lit)
{
printf("Invalid literal %s\n", lit);
exitClean(0);
}
void errorIncompatibleTypeInStatement(char* stmt, Type t, Type r)
{
char typeT[TYPE_SIZE], typeR[TYPE_SIZE];
typeToStringST(t, typeT);
typeToStringST(r, typeR);
printf("Incompatible type in %s statement (got %s, required %s)\n", stmt, typeT, typeR);
exitClean(0);
}
void errorIncompatibleTypeInStatement2(char* stmt, Type g, Type r1, Type r2)
{
char typeG[TYPE_SIZE], typeR1[TYPE_SIZE], typeR2[TYPE_SIZE];
typeToStringST(g, typeG);
typeToStringST(r1, typeR1);
typeToStringST(r2, typeR2);
printf("Incompatible type in %s statement (got %s, required %s or %s)\n", stmt, typeG, typeR1, typeR2);
exitClean(0);
}
void errorOperatorCannotBeApplied(char* op, Type t)
{
char type[TYPE_SIZE];
typeToStringST(t, type);
printf("Operator %s cannot be applied to type %s\n", op, type);
exitClean(0);
}
void errorOperatorCannotBeApplied2(char* op, Type t1, Type t2)
{
char type1[TYPE_SIZE], type2[TYPE_SIZE];
typeToStringST(t1, type1);
typeToStringST(t2, type2);
printf("Operator %s cannot be applied to types %s, %s\n", op, type1, type2);
exitClean(0);
}
void errorIncompatibleTypeInAssign(char* id, Type got, Type req)
{
char typeGot[TYPE_SIZE], typeReq[TYPE_SIZE];
typeToStringST(got, typeGot);
typeToStringST(req, typeReq);
printf("Incompatible type in assignment to %s (got %s, required %s)\n", id, typeGot, typeReq);
exitClean(0);
}
void errorIncompatibleTypeInAssignArray(char* id, Type got, Type req)
{
char typeGot[TYPE_SIZE], typeReq[TYPE_SIZE];
typeToStringST(got, typeGot);
typeToStringST(req, typeReq);
printf("Incompatible type in assignment to %s[] (got %s, required %s)\n", id, typeGot, typeReq);
exitClean(0);
}
void errorIncompatibleArgument(int argNum, char* id, Type g, Type r)
{
char typeG[TYPE_SIZE], typeR[TYPE_SIZE];
typeToStringST(g, typeG);
typeToStringST(r, typeR);
printf("Incompatible type of argument %d in call to method %s (got %s, required %s)\n", argNum, id, typeG, typeR);
exitClean(0);
}
void errorCannotFindSymbol(char* id)
{
printf("Cannot find symbol %s\n", id);
exitClean(0);
}