-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTyper.cpp
445 lines (371 loc) · 9.63 KB
/
Typer.cpp
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
#include "ParseTree.h"
#include "Type.h"
#include <iostream>
/*
Calculate types for each parse tree node, the corresponding productions are commented right above each Typer() method
*/
using namespace std;
TupleType* ParseTree::EMPTY_TYPE = new EmptyType();
//PROGRAM : EXTDEFS
void PROGRAM::Typer() {
extdefs->Typer();
}
//EXTDEFS : EXTDEF EXTDEFS
void EXTDEFS1::Typer() {
extdef->Typer();
extdefs->Typer();
}
//EXTDEFS :
void EXTDEFS2::Typer() {}
//EXTDEF : SPEC EXTVARS SEMI
void EXTDEF1::Typer() {
Type* specType = spec->Typer();
extvars->Typer(specType);
}
//TODO check type consistence
//EXTDEF : SPEC FUNC STMTBLOCK
void EXTDEF2::Typer() {
func->Typer(spec->Typer());
stmtblock->Typer();
}
//EXTVARS : DEC
void EXTVARS1::Typer(Type* type) {
dec->Typer(type);
}
//EXTVARS : DEC COMMA EXTVARS
void EXTVARS2::Typer(Type* type) {
dec->Typer(type);
extvars->Typer(type);
}
//EXTVARS :
void EXTVARS3::Typer(Type* type) {}
//SPEC : TYPE
Type* SPEC1::Typer() {
specType = type->Typer();
return specType;
}
//SPEC : STSPEC
Type* SPEC2::Typer() {
specType = stspec->Typer();
return specType;
}
//STSPEC : STRUCT OPTTAG LC DEFS RC
Type* STSPEC1::Typer() {
Structure* structureType = new Structure(opttag->GetSymbol()->name);
defs->Typer(structureType);
opttag->Typer(structureType);
return structureType;
}
//STSPEC : STRUCT ID
Type* STSPEC2::Typer() {
return ((MetaType*)name->Typer())->containType;
}
//OPTTAG : ID
void OPTTAG1::Typer(Type* type) {
type->typeName = name->symbol->name;
name->Typer(new MetaType(type));
}
//OPTTAG :
void OPTTAG2::Typer(Type* type) {
type->typeName = symbol->name;
symbol->type = new MetaType(type);
}
//VAR : ID
Type* VAR1::Typer(Type* type, Structure* structureType) {
name->Typer(type);
if(structureType != NULL) {
structureType->Collect(name->symbol->name, type);
return structureType;
}
return type;
}
//VAR : VAR LB INT RB
Type* VAR2::Typer(Type* type, Structure* structureType) {
num->Typer();
if(structureType != NULL) {
return var->Typer(new Array(type, num->num), structureType);
}
return var->Typer(new Array(type, num->num), NULL);
}
//FUNC : ID LP PARAS RP
Type* FUNC::Typer(Type* type) {
FuncType* funcType = new FuncType(paras->Typer(), type);
name->Typer(funcType);
return funcType;
}
//PARAS : PARA COMMA PARAS
Structure* PARAS1::Typer() {
Structure* parasTypes = paras->Typer();
parasTypes->Collect(para->GetName(), para->Typer());
return parasTypes;
}
//PARAS : PARA
Structure* PARAS2::Typer() {
Structure* structType = new Structure("");
structType->Collect(para->GetName(), para->Typer());
return structType;
}
//PARAS :
Structure* PARAS3::Typer() {
return new Structure("");
}
//PARA : SPEC VAR
Type* PARA::Typer() {
return var->Typer(spec->Typer(), NULL);
}
//STMTBLOCK : LC DEFS STMTS RC
Type* STMTBLOCK::Typer() {
defs->Typer();
return stmts->Typer();
}
//STMTS : STMT STMTS
Type* STMTS1::Typer() {
Type* stmtType = stmt->Typer();
Type* stmtsType = stmts->Typer();
//TODO check for inconsistent return types
if(stmtsType == EMPTY_TYPE) {
return stmtType;
}
else {
return stmtsType;
}
}
//STMTS :
Type* STMTS2::Typer() {
return EMPTY_TYPE;
}
//STMT : EXP SEMI
Type* STMT1::Typer() {
exp->Typer();
return EMPTY_TYPE;
}
//STMT : STMTBLOCK
Type* STMT2::Typer() {
return stmtblock->Typer();
}
//STMT : RETURN EXP SEMI
Type* STMT3::Typer() {
return exp->Typer();
}
//STMT : IF LP EXP RP STMT ESTMT
Type* STMT4::Typer() {
exp->Typer();
Type* thenType = stmt->Typer();
Type* elseType = estmt->Typer();
//TODO check for inconsistent return types;
if(thenType == EMPTY_TYPE) {
return elseType;
}
else {
return thenType;
}
}
//STMT : FOR LP FOREXP SEMI FOREXP SEMI FOREXP SEMI RP STMT
Type* STMT5::Typer() {
forexp1->Typer();
forexp2->Typer();
forexp3->Typer();
return stmt->Typer();
}
//STMT : CONT SEMI
Type* STMT6::Typer() {
return EMPTY_TYPE;
}
//STMT : BREAK SEMI
Type* STMT7::Typer() {
return EMPTY_TYPE;
}
//ESTMT : ELSE STMT
Type* ESTMT1::Typer() {
return stmt->Typer();
}
//ESTMT :
Type* ESTMT2::Typer() {
return EMPTY_TYPE;
}
//DEFS : DEF DEFS
void DEFS1::Typer(Type* type) {
def->Typer(type);
defs->Typer(type);
}
//DEFS :
void DEFS2::Typer(Type* type) {}
//DEF : SPEC DECS SEMI
void DEF::Typer(Type* type) {
Type* specType = spec->Typer();
if(type != EMPTY_TYPE) {
decs->Typer(specType, (Structure*)type);
}
decs->Typer(specType, NULL);
}
//DECS : DEC DECS
void DECS1::Typer(Type* type, Structure* structureType) {
dec->Typer(type, structureType);
decs->Typer(type, structureType);
}
//DECS : DEC
void DECS2::Typer(Type* type, Structure* structureType) {
dec->Typer(type, structureType);
}
//DEC : VAR
void DEC1::Typer(Type* type, Structure* structureType) {
var->Typer(type, structureType);
}
//DEC : VAR ASSIGNOP INIT
void DEC2::Typer(Type* type, Structure* structureType) {
var->Typer(type, structureType);
init->Typer();
}
//INIT : EXP
Structure* INIT1::Typer() {
Structure* structType = new Structure("");
structType->Collect("", exp->Typer());
return structType;
}
//INIT : LC ARGS RC
Structure* INIT2::Typer() {
return args->Typer();
}
//FOREXP : EXP
Type* FOREXP1::Typer() {
return exp->Typer();
}
//FOREXP :
Type* FOREXP2::Typer() {
return EMPTY_TYPE;
}
//EXP : EXP BINARYOP EXP
Type* EXP1::Typer() {
Type* type1 = exp1->Typer();
Type* type2 = exp2->Typer();
if(type1->typeName != "i32" || type2->typeName != "i32") {
cout << "Line" << lineNumber << " Try to do arithmetic operation with type " <<
type1->typeName << " and " << type2->typeName;
}
return type2;
}
//EXP : UNARYOP EXP
Type* EXP2::Typer() {
Type* type = exp->Typer();
if(type->typeName != "i32") {
cout << "Line " << lineNumber << " Try to do arithmetic operation on type " << type->typeName << endl;
}
return type;
}
//EXP : '-' EXP
Type* EXP3::Typer() {
Type* type = exp->Typer();
if(type->typeName != "i32") {
cout << "Line " << lineNumber << " Try to do arithmetic operation on type " << type->typeName << endl;
}
return type;
}
//EXP : LP EXP RP
Type* EXP4::Typer() {
return exp->Typer();
}
//EXP : ID LP ARGS RP
Type* EXP5::Typer() {
//Type check for function call is left to code generation phase
return ((FuncType*)name->Typer())->returnType;
}
//EXP : ID ARRS
Type* EXP6::Typer() {
Type* idType = name->Typer();
if(!idType->isArray && !arrs->isEmpty()) {
cout << "Line " << lineNumber << " Try to index a non-array variable "
<< name->symbol->name << endl;
}
return arrs->Typer(idType);
}
//EXP : EXP DOT ID
Type* EXP7::Typer() {
Type* type = exp->Typer();
if(!type->isStruct) {
cout << "Line " << this->lineNumber << " Try to access element of a non-struct" << "." << endl;
}
Structure* structType = (Structure*)type;
Type* elementType = structType->getElementType(name->symbol->name);
if(elementType->typeName == "empty") {
cout << "Line " << lineNumber << " Struct " << structType->typeName << " dosen't have a field called " << name->symbol->name << "." << endl;
}
return elementType;
}
//EXP : INT
Type* EXP8::Typer() {
return num->Typer();
}
//EXP : ID ARRS ASSIGNOP EXP
Type* EXP9::Typer() {
Type* idType = name->Typer();
if(!idType->isArray && !arrs->isEmpty()) {
cout << "Line " << lineNumber << " Try to index a non-array variable "
<< name->symbol->name << endl;
}
Type* arrayType = arrs->Typer(idType);
Type* expType = exp->Typer();
if(arrayType->typeName != expType->typeName) {
cout << "Line " << lineNumber << " Try to assign " << idType->typeName << " with " << expType->typeName << "." << endl;
}
return arrayType;
}
//EXP : EXP DOT ID ASSIGNOP EXP
Type* EXP10::Typer() {
Type* expType = exp1->Typer();
if(!expType->isStruct) {
cout << expType->typeName << endl;
cout << "Line " << lineNumber << " Try to access element of a non-struct" << "." << endl;
}
Structure* structType = (Structure*)expType;
Type* idType = structType->getElementType(name->symbol->name);
if(idType->typeName == "empty") {
cout << "Line " << lineNumber << " Struct " << structType->typeName << " dosen't have a field called " << name->symbol->name << "." << endl;
}
Type* exp2Type = exp2->Typer();
if(exp2Type->typeName != idType->typeName) {
cout << "Line " << lineNumber << " Try to assign " << idType->typeName << " with " << exp2Type->typeName << "." << endl;
}
return idType;
}
//TODO check type for indexes
//ARRS : LB EXP RB ARRS
Type* ARRS1::Typer(Type* type) {
exp->Typer();
return arrs->Typer(((Array*)type)->elementType);
}
//ARRS :
Type* ARRS2::Typer(Type* type) {
return type;
}
//ARGS : EXP COMMA ARGS
Structure* ARGS1::Typer() {
Structure* argsTypes = args->Typer();
argsTypes->Collect("", exp->Typer());
return argsTypes;
}
//ARGS : EXP
Structure* ARGS2::Typer() {
Structure* structType = new Structure("");
structType->Collect("", exp->Typer());
return structType;
}
//ARGS :
Structure* ARGS3::Typer() {
return new Structure("");
}
Type* ID::Typer(Type* type) {
//cout << symbol->name << " " << type->typeName << endl;
if(type == EMPTY_TYPE) {
return symbol->type;
}
else {
symbol->type = type;
}
return type;
}
Type* TYPE::Typer() {
return new Int();
}
Type* NUM::Typer() {
return new Int();
}