-
Notifications
You must be signed in to change notification settings - Fork 0
/
ASTAssignmentNode.cpp
160 lines (130 loc) · 3.73 KB
/
ASTAssignmentNode.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
/*
* File: ASTAssignmentNode.cpp
* Author: daniel
*
* Created on October 8, 2013, 1:19 AM
*/
#include "ASTAssignmentNode.h"
#include "ASTVariableDeclarationNode.h"
#include "ASTLiteralNode.h"
#include "ScopeTable.h"
#include "Quadruple.h"
ASTAssignmentNode::ASTAssignmentNode() : ASTStatementNode(), isArray(false),
id(0), left(NULL), exp(NULL), arrayExp(NULL)
{
}
ASTAssignmentNode::ASTAssignmentNode(const ASTAssignmentNode& orig) : ASTStatementNode(orig),
isArray(orig.isArray), id(orig.id), left(NULL),
exp(orig.exp), arrayExp(orig.arrayExp)
{
}
ASTAssignmentNode& ASTAssignmentNode::operator= (const ASTAssignmentNode &rhs)
{
ASTStatementNode::operator=(rhs);
// do the copy
isArray = rhs.isArray;
id = rhs.id;
left = rhs.left;
exp = rhs.exp;
arrayExp = rhs.arrayExp;
// return the existing object
return *this;
}
ASTAssignmentNode::~ASTAssignmentNode() {
delete exp;
delete arrayExp;
}
void ASTAssignmentNode::semAnalyze(){
if(init || !this->isGlobalDec){
this->scopeAnalyze();
if(init)
return;
}
if(this->isArray)
this->arrayExp->semAnalyze();
this->exp->semAnalyze();
this->typeAnalyze();
if(this->next != NULL)
this->next->semAnalyze();
}
void ASTAssignmentNode::scopeAnalyze(){
left = sa->getST()->getDeclaration(id, lineNumber);
}
string ASTAssignmentNode::genQuadruples(){
Quadruple quad;
stringstream ss;
ASTVariableDeclarationNode * leftDec = (ASTVariableDeclarationNode *)left;
ss << "(" << leftDec->level << "," << leftDec->displacement << ")";
//if its an array tae quadruple
if(isArray) {
quad.operation = "tae";
quad.arg1 = exp->genQuadruples();
quad.arg2 = arrayExp->genQuadruples();
//quad.result = left->lookup->getIdentifierName(id);
quad.result = ss.str();
vec.push_back(quad);
}
//other wise asignement quadruple
else {
quad.operation="asg";
//quad.result = left->lookup->getIdentifierName(id);
quad.result = ss.str();
// Special case, short-circuit assignment, don't bother storing temp var
string asgShort = exp->genQuadruples();
if(vec.back().operation == "rdi" ||
vec.back().operation == "rdb") {
//vec.back().result = left->lookup->getIdentifierName(id);
vec.back().result = ss.str();
}
// Some sort of expression, a temp var is necessary
else {
quad.arg1 = asgShort;
vec.push_back(quad);
}
}
if(this->next != NULL) {
this->next->genQuadruples();
}
return "";
}
void ASTAssignmentNode::typeAnalyze() {
if(left == NULL || exp == NULL) {
return;
}
// Already complained about an error
if(exp->type == -1) {
return;
}
if(exp->type == Scanner::VOID) {
return;
}
if(left->declarationType == Scanner::INT &&
(exp->type != Scanner::INT && exp->type != Scanner::NUM)) {
// Semantic error - assigning wrongly typed value
sa->semanticError("Assigning wrongly typed value", lineNumber);
}
else if(left->declarationType == Scanner::BOOL &&
(exp->type != Scanner::BOOL && exp->type != Scanner::BLIT)) {
// Semantic error - assigning wrongly typed value
sa->semanticError("Assigning wrongly typed value", lineNumber);
}
}
void ASTAssignmentNode::printNode(int indent, ostream * output) {
ostringstream oss;
this->output = output;
printIndented("assignment", indent);
if(ASTNode::lookup != NULL) {
printIndented("id: " + ASTNode::lookup->getIdentifierName(id), indent + 2);
}
oss << "arrayIndex? " << (isArray ? "YES" : "NO");
printIndented(oss.str(), indent + 2);
if(isArray) {
printIndented("index:", indent + 2);
arrayExp->printNode(indent + 4, output);
}
printIndented("value:", indent + 2);
exp->printNode(indent + 4, output);
if(next != NULL) {
next->printNode(indent, output);
}
}