-
Notifications
You must be signed in to change notification settings - Fork 0
/
ASTFunctionNode.cpp
178 lines (143 loc) · 3.83 KB
/
ASTFunctionNode.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
/*
* File: ASTFunctionNode.cpp
* Author: claire
*
* Created on October 6, 2013, 2:47 PM
*/
#include "ASTFunctionNode.h"
#include "ASTLiteralNode.h"
#include "ScopeTable.h"
ASTFunctionNode::ASTFunctionNode() : ASTDeclarationNode(),
param(NULL), compound(NULL) {
}
ASTFunctionNode::ASTFunctionNode(const ASTFunctionNode& orig):ASTDeclarationNode(orig),
param(orig.param),
compound(orig.compound)
{
}
ASTFunctionNode& ASTFunctionNode::operator= (const ASTFunctionNode &rhs)
{
ASTDeclarationNode::operator=(rhs);
// do the copy
param = rhs.param;
compound = rhs.compound;
// return the existing object
return *this;
}
ASTFunctionNode::~ASTFunctionNode() {
delete param;
delete compound;
}
string ASTFunctionNode::genQuadruples() {
int numLocals = 0;
ASTParamNode * curParam = param;
ASTDeclarationNode * decNode = compound->dec;
curLevel++;
curDisplacement = 2;
int i = -1;
// Calculate parameters level/displacement
while(curParam != NULL) {
curParam->level = curLevel;
curParam->displacement = i;
i--;
curParam = dynamic_cast<ASTParamNode *>(curParam->next);
}
// Calculate locals size
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 size
int ecsIndex = vec.size();
//generate the function quadruple
Quadruple funQuad;
funQuad.operation = "fun";
funQuad.arg1 = lookup->getIdentifierName(id);
funQuad.arg2 = ss.str();
vec.push_back(funQuad);
//generates the functions statement, by passing the comound node
//in order to avoid the ecs and lcs quadruples
compound->statement->genQuadruples();
// Update temp size
// -2 because of link and return address on stack
ss.str("");
ss << (curDisplacement - 2);
vec[ecsIndex].arg2 = ss.str();
curLevel--;
if(this->next != NULL) {
this->next->genQuadruples();
}
return "";
}
void ASTFunctionNode::semAnalyze(){
if(init || !this->isGlobalDec){
this->scopeAnalyze();
if(init)
return;
}
returnAnalyze();
if(getParamCount() > 0) {
this->param->semAnalyze();
}
this->compound->semAnalyze();
if(this->next != NULL)
this->next->semAnalyze();
//this->typeAnalyze();
}
void ASTFunctionNode::scopeAnalyze(){
sa->getST()->insertDeclaration(id, this);
}
void ASTFunctionNode::returnAnalyze() {
if(!this->compound->statement->returnAnalyze()) {
sa->semanticError("Missing return statement for function: " +
ASTNode::lookup->getIdentifierName(id), lineNumber);
}
}
void ASTFunctionNode::printNode(int indent, ostream * output) {
this->output = output;
printIndented("function", indent);
if(ASTNode::lookup != NULL) {
printIndented("id: " + ASTNode::lookup->getIdentifierName(id), indent + 2);
}
printIndented("type: " + Scanner::namesRev[declarationType], indent + 2);
if(param != NULL) {
printIndented("params:", indent + 2);
param->printNode(indent + 4, output);
}
printIndented("body:", indent + 2);
compound->printNode(indent + 4, output);
if(next != NULL) {
next->printNode(indent, output);
}
}
int ASTFunctionNode::getParamCount() {
int count = 0;
ASTParamNode * p = param;
// VOID param
if(p->declarationType == 0) {
return 0;
}
while(p != NULL) {
count++;
p = dynamic_cast<ASTParamNode *>(p->next);
}
return count;
}