-
Notifications
You must be signed in to change notification settings - Fork 0
/
ASTVariableDeclarationNode.cpp
94 lines (73 loc) · 2.24 KB
/
ASTVariableDeclarationNode.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
/*
* File: ASTVariableNode.cpp
* Author: claire
*
* Created on October 6, 2013, 2:46 PM
*/
#include "ASTVariableDeclarationNode.h"
#include "ScopeTable.h"
#include "ASTLiteralNode.h"
ASTVariableDeclarationNode::ASTVariableDeclarationNode() : ASTDeclarationNode(),
level(0), displacement(0),
isArray(false), arrayExp(NULL)
{
}
ASTVariableDeclarationNode::ASTVariableDeclarationNode(const ASTVariableDeclarationNode& orig) :ASTDeclarationNode(orig),
level(orig.level), displacement(orig.displacement),
isArray(orig.isArray),arrayExp(orig.arrayExp)
{
}
ASTVariableDeclarationNode& ASTVariableDeclarationNode::operator= (const ASTVariableDeclarationNode &rhs)
{
ASTDeclarationNode::operator=(rhs);
// do the copy
level = rhs.level;
displacement = rhs.displacement;
isArray = rhs.isArray;
arrayExp = rhs.arrayExp;
// return the existing object
return *this;
}
ASTVariableDeclarationNode::~ASTVariableDeclarationNode() {
delete arrayExp;
}
void ASTVariableDeclarationNode::semAnalyze(){
if(init || !isGlobalDec){
this->scopeAnalyze();
if(init) return;
}
if(isArray) {
// Analyze with restriction that vars not allowed
arrayExp->semAnalyze(true);
// Check that the value is a positive number
ASTLiteralNode * sizeVal = arrayExp->calc();
if(sizeVal == NULL ||
sizeVal->value < 0) {
sa->semanticError("Array size must be a positive integer", lineNumber);
}
}
if(this->next != NULL)
this->next->semAnalyze();
//this->typeAnalyze();
}
void ASTVariableDeclarationNode :: scopeAnalyze(){
sa->getST()->insertDeclaration(this-> id, this);
}
void ASTVariableDeclarationNode::printNode(int indent, ostream * output) {
ostringstream oss;
this->output = output;
printIndented("var declaration", indent);
if(ASTNode::lookup != NULL) {
printIndented("id: " + ASTNode::lookup->getIdentifierName(id), indent + 2);
}
printIndented("type: " + Scanner::namesRev[declarationType], indent + 2);
oss << "arrayParam? " << (isArray ? "YES" : "NO");
printIndented(oss.str(), indent + 2);
if(isArray) {
printIndented("size:", indent + 2);
arrayExp->printNode(indent + 4, output);
}
if(next != NULL) {
next->printNode(indent, output);
}
}