-
Notifications
You must be signed in to change notification settings - Fork 0
/
ASTVariableNode.cpp
140 lines (108 loc) · 3.02 KB
/
ASTVariableNode.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
/*
* File: ASTVariableNode.cpp
* Author: daniel
*
* Created on October 8, 2013, 8:51 AM
*/
#include "ASTVariableNode.h"
#include "ASTLiteralNode.h"
#include "ScopeTable.h"
ASTVariableNode::ASTVariableNode() : ASTExpressionNode(), id(0),
varDec(NULL), isArray(false),
arrayExp(NULL)
{
}
ASTVariableNode::ASTVariableNode(const ASTVariableNode& orig) : ASTExpressionNode(orig),
id(orig.id), varDec(NULL),
isArray(orig.isArray), arrayExp(orig.arrayExp)
{
}
ASTVariableNode& ASTVariableNode::operator= (const ASTVariableNode &rhs)
{
ASTExpressionNode::operator=(rhs);
// do the copy
id = rhs.id;
varDec = rhs.varDec;
isArray = rhs.isArray;
arrayExp = rhs.arrayExp;
// return the existing object
return *this;
}
ASTVariableNode::~ASTVariableNode() {
delete arrayExp;
}
string ASTVariableNode::genQuadruples() {
// Get the identifying address (level,displacement)
stringstream ss;
ss << "(" << varDec->level << "," << varDec->displacement << ")";
// Non-array is simply this address
if(!isArray) {
//return lookup->getIdentifierName(id);
return ss.str();
}
// With array, need to actually produce a temp with the array access result
else {
string arrayTemp = arrayExp->genQuadruples();
string arrRes = getTemp();
//vec.push_back(Quadruple("fae",lookup->getIdentifierName(id),arrayTemp,arrRes));
vec.push_back(Quadruple("fae",ss.str(),arrayTemp,arrRes));
return arrRes;
}
}
void ASTVariableNode::semAnalyze(){
if(init || !this->isGlobalDec){
this->scopeAnalyze();
if(init)
return;
}
if(this->isArray)
this->arrayExp->semAnalyze();
this->typeAnalyze();
if(this->next != NULL)
this->next->semAnalyze();
}
void ASTVariableNode::semAnalyze(bool restrictIdents){
if(restrictIdents) {
sa->semanticError("Array size must be static", lineNumber);
restrictIdents = false;
}
if(init || !this->isGlobalDec){
this->scopeAnalyze();
if(init)
return;
}
this->typeAnalyze();
if(this->isArray)
this->arrayExp->semAnalyze(restrictIdents);
if(this->next != NULL)
this->next->semAnalyze();
}
void ASTVariableNode::scopeAnalyze(){
varDec = (ASTVariableDeclarationNode *)(sa->getST()->getDeclaration(id, lineNumber));
}
void ASTVariableNode::typeAnalyze() {
if(varDec == NULL) {
//throw "NULL in varDec";
type = -1;
return;
}
type = varDec->declarationType;
}
void ASTVariableNode::printNode(int indent, ostream * output) {
ostringstream oss;
this->output = output;
printIndented("var", indent);
if(ASTNode::lookup != NULL) {
printIndented("id: " + ASTNode::lookup->getIdentifierName(id), indent + 2);
}
printIndented("type: " + Scanner::namesRev[type], indent + 2);
oss << "array? " << (isArray ? "YES" : "NO");
printIndented(oss.str(), indent + 2);
if(isArray) {
printIndented("index:", indent + 2);
arrayExp->printNode(indent + 4, output);
}
if(next != NULL) {
next->printNode(indent, output);
}
}