-
Notifications
You must be signed in to change notification settings - Fork 0
/
ASTCompoundNode.cpp
154 lines (121 loc) · 3.29 KB
/
ASTCompoundNode.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
/*
* File: ASTCompoundNode.cpp
* Author: claire
*
* Created on October 6, 2013, 2:42 PM
*/
#include "ASTCompoundNode.h"
#include "ASTVariableDeclarationNode.h"
#include "ASTLiteralNode.h"
#include "ScopeTable.h"
ASTCompoundNode::ASTCompoundNode() : ASTStatementNode(),
prevDisplacement(0),
dec(NULL), statement(NULL)
{
}
ASTCompoundNode::ASTCompoundNode(const ASTCompoundNode& orig):ASTStatementNode(orig),
prevDisplacement(orig.prevDisplacement),
dec(orig.dec), statement(orig.statement)
{
}
ASTCompoundNode& ASTCompoundNode::operator= (const ASTCompoundNode &rhs)
{
ASTStatementNode::operator=(rhs);
prevDisplacement = rhs.prevDisplacement;
dec = rhs.dec;
statement = rhs.statement;
// return the existing object
return *this;
}
ASTCompoundNode::~ASTCompoundNode() {
delete dec;
delete statement;
}
string ASTCompoundNode::genQuadruples(){
int numLocals = 0;
ASTDeclarationNode * decNode = dec;
curLevel++;
prevDisplacement = curDisplacement;
curDisplacement = 1;
// Calculate locals size
//gets number of local variables for the compound
while(decNode != NULL) {
ASTVariableDeclarationNode * decAsVar =
dynamic_cast<ASTVariableDeclarationNode *>(decNode);
if(decAsVar != NULL) {
if(!decAsVar->isArray) {
numLocals++;
decAsVar->level = curLevel;
decAsVar->displacement = curDisplacement;
curDisplacement++;
}
else {
ASTLiteralNode * sizeVal = decAsVar->arrayExp->calc();
numLocals += sizeVal->value;
decAsVar->level = curLevel;
decAsVar->displacement = curDisplacement;
curDisplacement += sizeVal->value;
}
}
decNode = dynamic_cast<ASTDeclarationNode *>(decNode->next);
}
stringstream ss;
ss << numLocals;
// Will return to update temp size
int ecsIndex = vec.size();
//creates the quadruples for entering the compound
//its statements, and leaving the compound
vec.push_back(Quadruple("ecs",ss.str(),"",""));
statement->genQuadruples();
vec.push_back(Quadruple("lcs","","",""));
// Update temp size
// -1 because of link on stack
ss.str("");
ss << (curDisplacement - 1);
vec[ecsIndex].arg1 = ss.str();
curDisplacement = prevDisplacement;
curLevel--;
if(this->next != NULL) {
this->next->genQuadruples();
}
return "";
}
void ASTCompoundNode::semAnalyze(){
if(init || !this->isGlobalDec){
this->scopeAnalyze();
if(init)
return;
}
sa->getST()->enterBlock();
if(this->dec != NULL)
this->dec->semAnalyze();
this->statement->semAnalyze();
sa->getST()->exitBlock();
if(this->next != NULL)
this->next->semAnalyze();
//this->typeAnalyze();
}
void ASTCompoundNode::scopeAnalyze(){
//nothing
}
bool ASTCompoundNode::returnAnalyze() {
bool children = this->statement->returnAnalyze();
if(next != NULL) {
return children || dynamic_cast<ASTStatementNode *>(next)->returnAnalyze();
}
return children;
}
void ASTCompoundNode::printNode(int indent, ostream * output) {
this->output = output;
if(dec != NULL) {
printIndented("declarations", indent);
dec->printNode(indent + 2, output);
}
if(statement != NULL) {
printIndented("statements", indent);
statement->printNode(indent + 2, output);
}
if(next != NULL) {
next->printNode(indent, output);
}
}