-
Notifications
You must be signed in to change notification settings - Fork 0
/
ASTIfNode.cpp
170 lines (134 loc) · 3.7 KB
/
ASTIfNode.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
/*
* File: ASTIfNode.cpp
* Author: daniel
*
* Created on October 8, 2013, 1:35 AM
*/
#include "ASTIfNode.h"
#include "SemanticAnalyzer.h"
#include "Quadruple.h"
ASTIfNode::ASTIfNode() : ASTStatementNode(), exp(NULL), statement(NULL), elseStatement(NULL) {
}
ASTIfNode::ASTIfNode(const ASTIfNode& orig) : ASTStatementNode(orig), exp(orig.exp),
statement(orig.statement),elseStatement(orig.statement)
{
}
ASTIfNode& ASTIfNode::operator= (const ASTIfNode &rhs)
{
ASTStatementNode::operator=(rhs);
// do the copy
exp=rhs.exp;
statement = rhs.statement;
elseStatement = rhs.elseStatement;
// return the existing object
return *this;
}
ASTIfNode::~ASTIfNode() {
delete exp;
delete statement;
delete elseStatement;
}
string ASTIfNode::genQuadruples(){
Quadruple ifquad;
ifquad.operation ="iff";
ifquad.arg1 = exp->genQuadruples();
//if there is an else statements associated, follow this logic
/*if
* stuff
* end if label
* else label
* else stuff
* end label
*/
if(elseStatement!=NULL)
{
string elseLabel = getLabel();
string endLabel = getLabel();
ifquad.result=elseLabel;
vec.push_back(ifquad);
statement->genQuadruples();
vec.push_back(Quadruple("goto","","",endLabel));
vec.push_back(Quadruple("lab","","",elseLabel));
elseStatement->genQuadruples();
vec.push_back(Quadruple("lab","","",endLabel));
}
//if there is no else statement do it this way
/**if
statement stuff
end label*/
else
{
string endLabel=getLabel();
ifquad.result=endLabel;
vec.push_back(ifquad);
statement->genQuadruples();
vec.push_back(Quadruple("lab","","",endLabel));
}
if(this->next != NULL) {
this->next->genQuadruples();
}
return "";
}
void ASTIfNode::semAnalyze(){
if(init || !this->isGlobalDec){
this->scopeAnalyze();
if(init)
return;
}
this->exp->semAnalyze();
this->typeAnalyze();
this->statement->semAnalyze();
if(elseStatement != NULL)
this->elseStatement->semAnalyze();
if(this->next != NULL)
this->next->semAnalyze();
}
void ASTIfNode::scopeAnalyze(){
}
void ASTIfNode::typeAnalyze() {
if(exp == NULL) {
return;
}
if(exp->type != Scanner::BOOL) {
// Semantic error - expression must be of boolean type
sa->semanticError("Expression must be of boolean type", lineNumber);
}
}
bool ASTIfNode::returnAnalyze() {
bool retInBranches = true;
ASTStatementNode * curState = statement;
// Must have an else statement in order to return true
if(elseStatement != NULL) {
while(curState != NULL) {
retInBranches = retInBranches && curState->returnAnalyze();
curState = dynamic_cast<ASTStatementNode *>(curState->next);
}
curState = elseStatement;
while(curState != NULL) {
retInBranches = retInBranches && curState->returnAnalyze();
curState = dynamic_cast<ASTStatementNode *>(curState->next);
}
}
else {
retInBranches = false;
}
if(next != NULL) {
return retInBranches || dynamic_cast<ASTStatementNode *>(next)->returnAnalyze();
}
return retInBranches;
}
void ASTIfNode::printNode(int indent, ostream * output) {
this->output = output;
printIndented("if", indent);
printIndented("condition:", indent + 2);
exp->printNode(indent + 4, output);
printIndented("true branch:", indent + 2);
statement->printNode(indent + 4, output);
if(elseStatement != NULL) {
printIndented("false branch:", indent + 2);
elseStatement->printNode(indent + 4, output);
}
if(next != NULL) {
next->printNode(indent, output);
}
}