-
Notifications
You must be signed in to change notification settings - Fork 0
/
decaf.cc
447 lines (409 loc) · 13.5 KB
/
decaf.cc
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
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cassert>
#include "semantics.h"
#include "decaf.tab.h"
using namespace std;
//Parsing and lexing functions parse calls lex
//Must call parse to get parse tree
extern int yylex(void);
extern int yyparse(void);
extern FILE * yyin;
extern const char * const* token_table;
extern Symtab * currentScope;
const int LINESINLIB = 22;
S_class * currentClass = NULL;
S_function * currentFunction = NULL;
int sequenceNumber = 1;
void pass1(ParseTree * tree);
void semantic_error(string err, int line)
{
cout << "Semantic error: " << err << " on line " << line - LINESINLIB << endl;
exit(1);
}
void error_if_defined_locally(ParseTree *tree)
{
// tree represents an identifier. Make sure it's not already defined.
assert(tree->type == TERMINAL and tree->token->type == IDENTIFIER);
string var_name = tree->token->text;
int lineno = tree->token->line;
if (currentScope->local_lookup(var_name)){
semantic_error(var_name + " already defined", lineno - LINESINLIB); //22 fake lines!
}
}
int countBrackets(ParseTree * bracketLst){
int count = 0;
if(bracketLst){
string brackets = bracketLst->token->text;
for (size_t i = 0; i < brackets.size(); i++){
if (brackets.substr(i, 1) == "[") count++;
}
}
return count;
}
int get_type_dimension(ParseTree * type, ParseTree * varDecId){
int dimension = 0;
if (type && (type->children.size() > 1)){
assert(type->description == "type");
dimension += countBrackets(type->children[1]);
}
if(varDecId && (varDecId->children.size() > 1)){
assert(varDecId->description == "varDecId");
dimension += countBrackets(varDecId->children[1]);
}
return dimension;
}
string get_type_name(ParseTree * type){
assert(type->description == "type");
return type->children[0]->token->text;
}
S_variable * makeVariable(ParseTree * modifiers, ParseTree * type, ParseTree * varDeclaratorId){
if (modifiers) assert(modifiers->description == "modifiers");
assert(type->description == "type");
assert(varDeclaratorId->description == "varDecId");
string varName = varDeclaratorId->children[0]->token->text;
// cout << "in makeVariable with " << varName << endl;
error_if_defined_locally(varDeclaratorId->children[0]);
string type_name = get_type_name(type);
S_variable *entry = new S_variable;
entry->ident = varName;
entry->type = new S_type;
entry->formal = false;
//Set up the type using type tree
entry->type->name = type_name;
entry->type->dimension = get_type_dimension(type, varDeclaratorId);
//Set up modifiers too@@@
if(modifiers){
//We are in field CANNOT BE STATIC and ONLY ONE ACCESS MOD
}
entry->parentClass = currentClass;
entry->sequenceNumber = sequenceNumber;
sequenceNumber++;
currentScope->insert(varName, entry);
cout << entry->type->name << " " << entry->ident << endl;
return entry;
}
S_function * makeMethod(ParseTree *tree){
assert(tree->description=="method" || tree->description=="ctor");
//DEAL WITH MODIFIERS
string ident = tree->children[2]->token->text;
error_if_defined_locally(tree->children[2]);
S_function *entry = new S_function;
entry->ident = ident;
entry->returnType = NULL; //Signal ctor
if (tree->description=="method"){
string returnTypeString = get_type_name(tree->children[1]);
S_type * returnEntry = new S_type;
returnEntry->name = returnTypeString;
returnEntry->dimension = get_type_dimension(tree->children[1], NULL);
entry->returnType = returnEntry;
}
openscope();
currentFunction = entry;
sequenceNumber = 0;
for(size_t i = 0; i < tree->children[3]->children.size(); i ++){
S_variable * formal = makeVariable(NULL, tree->children[3]->children[i]->children[0], tree->children[3]->children[i]->children[1]);
// cout << "formal ident: " << formal->ident << endl;
formal->formal = true;
entry->formals.push_back(formal);
}
pass1(tree->children[4]);
tree->symtab = closescope();
currentFunction = NULL;
sequenceNumber = 0;
currentScope->insert(ident, entry);
return entry;
}
S_class * makeClass(ParseTree * tree){
assert(tree->description == "class");
string ident = tree->children[0]->token->text;
error_if_defined_locally(tree->children[0]);
S_class * entry = new S_class;
entry->ident = ident;
currentClass = entry;
entry->parentClass = NULL;
if (tree->children.size() > 2){
// We explicitly extend a class!
string super = tree->children[2]->children[0]->token->text;
int line = tree->children[2]->children[0]->token->line;
semantics * tmp = currentScope->lookup(super);
if(!tmp) semantic_error("super class " + super + " not defined", line);
if(tmp->kind() != "S_class") semantic_error("super class " + super + " not a class", line);
entry->parentClass = (S_class *) tmp;
}
else {
if(ident != "Object"){
int line = tree->children[0]->token->line;
semantics * tmp = currentScope->lookup("Object");
if(!tmp) semantic_error("Couldn't find object -- logic error", line);
if(tmp->kind() != "S_class") semantic_error("Object not a class, logic error", line);
entry->parentClass = (S_class *) tmp;
}
}//WE EXTEND OBJECT LOOK IT UP
openscope();
pass1(tree->children[1]);
tree->symtab = closescope();
currentScope->insert(ident, entry);
currentClass = NULL;
return entry;
}
void pass1(ParseTree * tree)
{
if (!tree) return;
if (tree->type==TERMINAL) return;
if (tree->description=="statements") {
openscope();
for(size_t i = 0; i < tree->children.size(); i++){
pass1(tree->children[i]);
}
tree->symtab = closescope();
}
else if (tree->description=="method"){
S_function * entry = makeMethod(tree);
entry->ctor = false;
if(currentClass) currentClass->fields.push_back(entry);
}
else if (tree->description=="ctor"){
S_function * entry = makeMethod(tree);
entry->ctor = true;
if(currentClass) currentClass->fields.push_back(entry);
}
else if (tree->description=="field"){
//make fields
for (size_t i =0; i < tree->children[2]->children.size(); i++){
S_variable * entry = makeVariable(tree->children[0], tree->children[1], tree->children[2]->children[i]->children[0]);
if(currentClass){
entry->sequenceNumber = -1;
currentClass->fields.push_back(entry);
}
}
}
else if (tree->description=="declStmt"){
for (size_t i =0; i < tree->children[1]->children.size(); i++){
S_variable * entry = makeVariable(NULL, tree->children[0], tree->children[1]->children[i]->children[0]);
}
}
else if (tree->description=="class"){
S_class * entry = makeClass(tree);
}
else if (tree->type == NONTERMINAL){
for(size_t i = 0; i < tree->children.size(); i++){
pass1(tree->children[i]);
}
}
}
void typeCheck(ParseTree * t, Symtab * s);
semantics * virtualLookup(string ident, S_class * curClass){
if(!curClass) return NULL;
for(size_t i = 0; i < curClass->fields.size(); i++){
if(curClass->fields[i]->ident == ident) {
return curClass->fields[i];
}
}
return virtualLookup(ident, curClass->parentClass);
}
semantics * class_scope_lookup(string id, Symtab * scope, S_class * someClass){
//Look up in class structure or in scope
//Return the appropriate semantic object
semantics * fromClass = virtualLookup(id, someClass);
semantics * fromScope = scope->lookup(id);
if(fromScope) return fromScope;
if(fromClass) return fromClass;
return fromScope; //null
}
bool compatible(string subtype, string supertype, Symtab * tab){
if(subtype == supertype) return true;
if (subtype == "null"){
if(supertype == "null") return true;
semantics * supclass = tab->lookup(supertype);
if(!supclass) return false;
if(supclass->kind() == "S_class"){
return true;
}
return false;
}
else{
semantics * subclass = tab->lookup(subtype);
if(!subclass) return false;
if(subclass->kind() != "S_class") return false;
S_class * sub = (S_class *) subclass;
if(!sub->parentClass) return false;
return compatible(sub->parentClass->ident, supertype, tab);
}
}
void checkPrimary(ParseTree * tree, Symtab * tab){
if(tree->children[0]->type == NONTERMINAL){
//DO MORE HERE
typeCheck(tree->children[0], tab);
tree->s_type = tree->children[0]->s_type;
tree->dimension = tree->children[0]->dimension;
cout << "Primary Type" << tree->s_type <<endl;
}
else{//we have a variable identifier
//ident
assert(tree->children[0]->token->type == IDENTIFIER);
string ident = tree->children[0]->token->text;
int line = tree->children[0]->token->line;
semantics * var = class_scope_lookup(ident, tab, currentClass);
if (!var) semantic_error(ident + " not defined", line);
if (var->kind() != "S_variable") semantic_error(ident + " not a variable", line);
S_variable * temp = (S_variable *) var;
tree->s_type = temp->type->name;
tree->dimension = temp->type->dimension;
}
}
void checkLiteral(ParseTree * tree, Symtab * tab){
cout << "in check literal" << endl;
if (tree->children[0]->token->type == DECAF_NULL)
tree->s_type = "null";
else if (tree->children[0]->token->type == BOOLEAN_LITERAL)
tree->s_type = "bool";
else if (tree->children[0]->token->type == INTEGER){
cout << "it's an int" << endl;
tree->s_type = "int";
}
else if (tree->children[0]->token->type == CHARACTER)
tree->s_type = "char";
else if (tree->children[0]->token->type == STRING)
tree->s_type = "String";
tree->dimension=0;
}
void checkExpr(ParseTree * tree, Symtab * tab){
if(tree->children[0]->type == NONTERMINAL){
//We have a Primary. Deal with it later. Call typeCheck
typeCheck(tree->children[0], tab);
tree->s_type = tree->children[0]->s_type;
tree->dimension = tree->children[0]->dimension;
}
else{
string op = tree->children[0]->token->text;
int line = tree->children[0]->token->line;
for(size_t i = 0; i < tree->children.size(); i++){
typeCheck(tree->children[i], tab);
}
cout << op << endl;
if (op == "="){
if ((tree->children[1]->dimension) != (tree->children[2]->dimension)){
semantic_error("assignment different dimensions", line);
}
if (!compatible(tree->children[2]->s_type, tree->children[1]->s_type, tab)){
semantic_error("incompatible types for equality op", line);
}
tree->s_type = tree->children[1]->s_type;
tree->dimension = tree->children[1]->dimension;
}
else if (op == "!=" or op == "=="){
if ((tree->children[1]->dimension) != (tree->children[2]->dimension)){
semantic_error("equality op different dimensions", line);
}
if (!compatible(tree->children[1]->s_type, tree->children[2]->s_type, tab)){
semantic_error("incompatible types for equality op", line);
}
tree->s_type = "bool";
tree->dimension = 0;
}
else if (op == "||" or op == "&&"){
if ((tree->children[1]->s_type != "bool") or (tree->children[1]->dimension != 0)){
semantic_error("logical op expecting bool", line);
}
if ((tree->children[2]->s_type != "bool") or (tree->children[2]->dimension != 0)){
semantic_error("logical op expecting bool", line);
}
tree->s_type = "bool";
tree->dimension = 0;
}
else if (op == "<" or op == ">" or op == "<=" or op == ">="){
if ((tree->children[1]->s_type != "int") or (tree->children[1]->dimension != 0)){
semantic_error("comparison expecting int", line);
}
if ((tree->children.size() > 2) and ((tree->children[2]->s_type != "int") or (tree->children[2]->dimension != 0))){
semantic_error("comparison expecting int", line);
}
tree->s_type = "bool";
tree->dimension = 0;
}
else if (op == "*" or op == "/" or op == "%" or op == "+" or op == "-"){
if ((tree->children[1]->s_type != "int") or (tree->children[1]->dimension != 0)){
cout << tree->children[1]->s_type << "first" << endl;
semantic_error("arithmetic op expecting int", line);
}
if ((tree->children.size() > 2) and ((tree->children[2]->s_type != "int") or (tree->children[2]->dimension != 0))){
semantic_error("arithmetic op expecting int", line);
}
tree->s_type = "int";
tree->dimension = 0;
}
else if (op == "!"){
if ((tree->children[1]->s_type != "bool") or (tree->children[1]->dimension != 0)){
semantic_error("! expecting bool", line);
}
tree->s_type="bool";
tree->dimension = 0;
}
}
}
void typeCheck(ParseTree * tree, Symtab * tab){
if(!tree) return;
if(tree->symtab){
tab = tree->symtab;
}
if(tree->description == "expr"){
checkExpr(tree, tab);
}
else if(tree->description == "primary"){
checkPrimary(tree, tab);
}
else if(tree->description == "literal_expr"){
checkLiteral(tree, tab);
}
else{
for(size_t i = 0; i < tree->children.size(); i++){
typeCheck(tree->children[i], tab);
}
}
}
void testLex(){
int tok;
while ((tok=yylex()) != -1){
cout << token_table[tok - 258 + 3] << endl;
}
/* Read tokens until finished */
}
int main(int argc, char **argv){
/* Make sure there's a given file name */
if (argc != 2) {
cout << "USAGE: " << argv[0] << " FILE" << endl;
exit(1);
}
//CONCAT EXISTING FILES THEN IGNORE IO, OBJ, etc.
FILE * lib = fopen("lib.decaf", "r");
if (!lib) {
cout << "Can't open lib." << endl;
exit(1);
}
FILE * input = fopen(argv[1], "r");
if (!input) {
cout << argv[1] << ": No such file or file can't be opened for reading." << endl;
exit(1);
}
FILE * tmp = fopen("tmp", "w+");
char ch;
while( ( ch = fgetc(lib) ) != EOF )
fputc(ch,tmp);
while( ( ch = fgetc(input) ) != EOF )
fputc(ch,tmp);
fclose(lib);
fclose(input);
fclose(tmp);
yyin = fopen("tmp", "r");
//testLex();
yyparse();
openscope();
pass1(top);
traverseTree(top,0,0);
//Once in symtab, clear the preDefined objects
top->children.erase(top->children.begin(), top->children.begin()+3);
typeCheck(top, currentScope);
// cout << top->children.size() << endl;
}