-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASTBranchNode.cpp
205 lines (157 loc) · 4.45 KB
/
ASTBranchNode.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
/*
* File: ASTBranchNode.cpp
* Author: claire
*
* Created on October 9, 2013, 12:09 AM
*/
#include "ASTBranchNode.h"
#include "SemanticAnalyzer.h"
#include "Quadruple.h"
ASTBranchNode::ASTBranchNode(): ASTStatementNode(), expression(NULL), firstCase(NULL),
defaultReached(false), caseList(0)
{
}
ASTBranchNode::ASTBranchNode(const ASTBranchNode& orig):ASTStatementNode(orig),
expression(orig.expression), firstCase(orig.firstCase), defaultReached(orig.defaultReached),
caseList(orig.caseList)
{
}
ASTBranchNode& ASTBranchNode::operator= (const ASTBranchNode &rhs)
{
ASTStatementNode::operator=(rhs);
// do the copy
expression = rhs.expression;
firstCase = rhs.firstCase;
defaultReached = rhs.defaultReached;
caseList = rhs.caseList;
// return the existing object
return *this;
}
ASTBranchNode::~ASTBranchNode() {
delete expression;
delete firstCase;
}
string ASTBranchNode::genQuadruples() {
/* temp vars
* iff on case's val, skip to next case iff
* recurse into case
* goto end
* next case iff
*/
string expTemp = expression->genQuadruples();
ASTCaseNode * cNode = firstCase;
ASTCaseNode * defNode = NULL;
string endLabel = getLabel();
// Looping over the cases
while(cNode != NULL) {
// Will put the default case last, without conditions
if(cNode->type == Scanner::DEFAULT) {
defNode = cNode;
cNode = dynamic_cast<ASTCaseNode *>(cNode->next);
continue;
}
stringstream ss;
ss << cNode->num;
// Case condition
Quadruple eqQuad;
eqQuad.operation = "eq";
eqQuad.arg1 = expTemp;
eqQuad.arg2 = ss.str();
eqQuad.result = getTemp();
vec.push_back(eqQuad);
// Case if false jump
Quadruple ifQuad;
ifQuad.operation = "iff";
ifQuad.arg1 = eqQuad.result;
ifQuad.result = getLabel();
vec.push_back(ifQuad);
cNode->genQuadruples();
// Skip remainder of cases
vec.push_back(Quadruple("goto", "", "", endLabel));
// Start of next case
vec.push_back(Quadruple("lab", "", "", ifQuad.result));
cNode = dynamic_cast<ASTCaseNode *>(cNode->next);
}
// Now do the default's code
if(defNode != NULL) {
defNode->genQuadruples();
}
// End of branch
vec.push_back(Quadruple("lab", "", "", endLabel));
if(this->next != NULL) {
this->next->genQuadruples();
}
return "";
}
void ASTBranchNode ::semAnalyze(){
if(init || !this->isGlobalDec){
this->scopeAnalyze();
if(init)
return;
}
this->expression->semAnalyze();
this-> firstCase->semAnalyze();
this->caseAnalyze();
this->typeAnalyze();
if(this->next != NULL)
this->next->semAnalyze();
}
void ASTBranchNode :: caseAnalyze(){
ASTCaseNode * current = firstCase;
do{
if(current->type == Scanner::DEFAULT){
if(defaultReached)
sa->semanticError("Multiple default statements found",lineNumber);
defaultReached = true;
}
else{
if(contains(current->num))
sa->semanticError("Case value repeated",lineNumber);
caseList.push_back(current->num);
}
current = dynamic_cast<ASTCaseNode *>(current->next);
}while(current!= NULL);
}
void ASTBranchNode:: scopeAnalyze(){
//nothing?
}
void ASTBranchNode::typeAnalyze() {
if(expression == NULL) {
return;
}
if(expression->type != Scanner::INT) {
// Semantic error - integral expression expected
sa->semanticError("Integral expression expected", lineNumber);
}
}
bool ASTBranchNode::returnAnalyze() {
bool retInCases = true;
ASTCaseNode * curCase = firstCase;
while(curCase != NULL) {
retInCases = retInCases && curCase->returnAnalyze();
curCase = dynamic_cast<ASTCaseNode *>(curCase->next);
}
if(next != NULL) {
return retInCases || dynamic_cast<ASTStatementNode *>(next)->returnAnalyze();
}
return retInCases;
}
void ASTBranchNode::printNode(int indent, ostream * output) {
this->output = output;
printIndented("branch", indent);
printIndented("exp:", indent + 2);
expression->printNode(indent + 4, output);
printIndented("cases:", indent + 2);
firstCase->printNode(indent + 4, output);
if(next != NULL) {
next->printNode(indent, output);
}
}
//helper functions, checks caseList to see if contains x
bool ASTBranchNode :: contains(int x){
for(int i = 0; i<caseList.size(); i++){
if(caseList.at(i)==x)
return true;
}
return false;
}