-
Notifications
You must be signed in to change notification settings - Fork 0
/
wlp4type.cc
232 lines (203 loc) · 7.8 KB
/
wlp4type.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
#include <iostream>
#include <sstream>
#include "structs.h"
#include "wlp4parse.h"
#include "wlp4type.h"
using namespace std;
TreeNode* createTree() {
TreeNode* tree;
ProcedureTable* procTable;
try {
tree = parseTokens();
procTable = new ProcedureTable();
TreeNode* procNode = tree->getChild("procedures");
collectProcedures(procTable, procNode);
delete(procTable);
return tree;
}
catch (runtime_error &e) {
delete(tree);
delete(procTable);
throw runtime_error(e.what());
}
}
void collectProcedures(ProcedureTable* &procTable, TreeNode* &tree) {
if(tree->children.size() == 1) {
TreeNode* mainNode = tree->getChild("main");
Procedure mainProc(mainNode);
procTable->addProcedure(mainProc);
annotateTypes(mainNode, mainProc, procTable);
if(mainNode->getChild("expr")->type != "int") {
throw runtime_error("wain must return int.");
}
checkStatements(mainNode);
}
else {
TreeNode* procNode = tree->getChild("procedure");
Procedure proc(procNode);
procTable->addProcedure(proc);
annotateTypes(procNode, proc, procTable);
if(procNode->getChild("expr")->type != "int") {
throw runtime_error("Procedure must return int.");
}
checkStatements(procNode);
TreeNode* procsNode = tree->getChild("procedures");
collectProcedures(procTable, procsNode);
}
}
void annotateTypes(TreeNode* &tree, Procedure &proc, ProcedureTable* &procTable) {
for(TreeNode* child : tree->children) {
annotateTypes(child, proc, procTable);
}
string ruleRhs = tree->rule.getRHSString();
if(tree->rule.lhs == "factor") {
if(ruleRhs == "NUM") {
tree->type = "int";
}
else if(ruleRhs == "NULL") {
tree->type = "int*";
}
else if(ruleRhs == "ID") {
tree->type = proc.localTable.getVariable(tree->getChild("ID")->token.lexeme).type;
}
else if(ruleRhs == "LPAREN expr RPAREN") {
tree->type = tree->getChild("expr")->type;
}
else if(ruleRhs == "AMP lvalue") {
if(tree->getChild("lvalue")->type != "int") {
throw runtime_error("Address can only be taken of an integer l-value.");
}
tree->type = "int*";
}
else if(ruleRhs == "STAR factor") {
if(tree->getChild("factor")->type != "int*") {
throw runtime_error("Cannot dereference a non-pointer.");
}
tree->type = "int";
}
else if(ruleRhs == "NEW INT LBRACK expr RBRACK") {
if(tree->getChild("expr")->type != "int") {
throw runtime_error("Argument of new int[] must be an integer.");
}
tree->type = "int*";
}
else if(ruleRhs == "ID LPAREN RPAREN") {
string idName = tree->getChild("ID")->token.lexeme;
if(proc.localTable.table.find(idName) != proc.localTable.table.end()) {
throw runtime_error("Variable " + idName + " is not a function.");
}
if(!procTable->getProcedure(idName).signature.empty()) {
throw runtime_error("Function " + idName + " expects arguments.");
}
tree->type = "int";
}
else if(ruleRhs == "ID LPAREN arglist RPAREN") {
string idName = tree->getChild("ID")->token.lexeme;
if(proc.localTable.table.find(idName) != proc.localTable.table.end()) {
throw runtime_error("Variable " + idName + " is not a function.");
}
vector<string> args;
TreeNode* argsNode = tree->getChild("arglist");
while(argsNode->children.size() == 3) {
args.push_back(argsNode->getChild("expr")->type);
argsNode = argsNode->getChild("arglist");
}
args.push_back(argsNode->getChild("expr")->type);
if(args.size() != procTable->getProcedure(idName).signature.size()) {
throw runtime_error("Function " + idName + " given wrong number of args.");
}
for(int i = 0; i < int(args.size()); i++) {
if(args.at(i) != procTable->getProcedure(idName).signature.at(i)) {
throw runtime_error("Function " + idName + " argument type mismatch.");
}
}
tree->type = "int";
}
}
else if(tree->rule.lhs == "expr") {
if(ruleRhs == "term") {
tree->type = tree->getChild("term")->type;
}
else if(ruleRhs == "expr PLUS term") {
if(tree->getChild("expr")->type == "int" && tree->getChild("term")->type == "int") {
tree->type = "int";
}
else if(tree->getChild("expr")->type == "int*" && tree->getChild("term")->type == "int") {
tree->type = "int*";
}
else if(tree->getChild("expr")->type == "int" && tree->getChild("term")->type == "int*") {
tree->type = "int*";
}
else {
throw runtime_error("Incompatible operands to +.");
}
}
else if(ruleRhs == "expr MINUS term") {
if(tree->getChild("expr")->type == "int" && tree->getChild("term")->type == "int") {
tree->type = "int";
}
else if(tree->getChild("expr")->type == "int*" && tree->getChild("term")->type == "int") {
tree->type = "int*";
}
else if(tree->getChild("expr")->type == "int*" && tree->getChild("term")->type == "int*") {
tree->type = "int";
}
else {
throw runtime_error("Incompatible operands to -.");
}
}
}
else if(tree->rule.lhs == "lvalue") {
if(ruleRhs == "ID") {
tree->type = proc.localTable.getVariable(tree->getChild("ID")->token.lexeme).type;
}
else if(ruleRhs == "LPAREN lvalue RPAREN") {
tree->type = tree->getChild("lvalue")->type;
}
else if(ruleRhs == "STAR factor") {
if(tree->getChild("factor")->type != "int*") {
throw runtime_error("Cannot dereference a non-pointer.");
}
tree->type = "int";
}
}
else if(tree->rule.lhs == "term") {
if(ruleRhs == "factor") {
tree->type = tree->getChild("factor")->type;
}
else {
if(!(tree->getChild("term")->type == "int" && tree->getChild("factor")->type == "int")) {
throw runtime_error("Operands of * / % must be ints.");
}
tree->type = "int";
}
}
}
void checkStatements(TreeNode* &tree) {
for(TreeNode* child : tree->children) {
checkStatements(child);
}
string ruleRhs = tree->rule.getRHSString();
if(tree->rule.lhs == "statement") {
if(ruleRhs == "lvalue BECOMES expr SEMI") {
if(tree->getChild("lvalue")->type != tree->getChild("expr")->type) {
throw runtime_error("Assignment between incompatible types.");
}
}
else if(ruleRhs == "PRINTLN LPAREN expr RPAREN SEMI") {
if(tree->getChild("expr")->type != "int") {
throw runtime_error("println() requires an int.");
}
}
else if(ruleRhs == "DELETE LBRACK RBRACK expr SEMI") {
if(tree->getChild("expr")->type != "int*") {
throw runtime_error("delete [] requires a int*.");
}
}
}
else if(tree->rule.lhs == "test") {
if(tree->getChild("expr", 1)->type != tree->getChild("expr", 2)->type) {
throw runtime_error("Cannot compare int* to int.");
}
}
}