Repository navigation
Expand file tree
/
Copy pathProgramTree.cpp
More file actions
137 lines (126 loc) · 2.9 KB
/
Copy pathProgramTree.cpp
File metadata and controls
137 lines (126 loc) · 2.9 KB
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
#include "ProgramTree.hpp"
#include <stdlib.h>
#include <string>
#include <iostream>
ProgramTree* ProgramTree::getChild(int i){
return children[i];
}
ProgramTree* ProgramTree::getParent(){
return parent;
}
vector<ProgramTree*> ProgramTree::getChildren(){
return children;
}
ProgramTree::ProgramTree(int i,ParseTreeType t){
//indentation of command
indent=i;
treeType=t;
intValue=0;
stringValue="";
parent=NULL;
}
string ProgramTree::getStringValue(){
return stringValue;
}
int ProgramTree::getIntValue(){
return intValue;
}
void ProgramTree::setIntValue(int i){
intValue=i;
}
void ProgramTree::setParent(ProgramTree* p){
parent=p;
}
void ProgramTree::addChild(ProgramTree* c){
c->setParent(this);
children.push_back(c);
c->setParent(this);
//cout<<"parent: "<<this->getType()<<"to "<<c->getType()<<endl;
}
void ProgramTree::setStringValue(string s){
stringValue=s;
}
//for debug
void ProgramTree::print(){
string indentString="";
indentString.append(indent,' ');
cout<<indentString;
string theType=to_string(treeType);
switch(treeType){
case varAssignSt: theType="varAssignSt";
break;
case ifElSt: theType="ifElSt";
break;
case ifBool: theType="ifBool";
break;
case variable: theType="variable";
break;
case intConst: theType="intConst";
break;
case mathSt: theType="mathSt";
break;
case ifStatments: theType="ifStatments";
break;
case elseStatments: theType="elseStatments";
break;
case printSt: theType="printSt";
break;
case str: theType="str";
break;
case program: theType="program";
break;
case functionDecleration: theType="functionDecleration";
break;
case functionStatments: theType="functionStatments";
break;
case returnStatment: theType="returnStatment";
break;
}
cout<<"Type"<<": "<<theType;
cout<<" INT V: "<<intValue;
cout<<" String Value: "<<stringValue;
cout<<" Indentation: "<<indent;
cout<<" This: "<<this;
cout<<" Parent: "<<parent;
cout<<endl;
if(children.size()>0){
cout<<"!!children of "<<theType<<endl;
for(int i=0;i<children.size();i++){
children[i]->print();
}
cout<<"!!end children of "<<theType<<endl;
}
}
ParseTreeType ProgramTree::getType(){
return treeType;
}
int ProgramTree::getIndent(){
return indent;
}
//gets the scope of a program command
//For telling which variable a function is refering to if the name is the same
ProgramTree* ProgramTree::getScope(){
/*ProgramTree* scope=this;
cout<<scope<<endl;
while(parent!=NULL && scope->getType()!=program){
scope=parent;
}
return scope;*/
//scope of root elemnt is the root element
if(parent==NULL||this->getType()==program){
return this;
}
else if(parent->getType()==functionDecleration){
//children of functions have a scope of the function
return parent;
}
else{
//all other elements have the same scope as their parents
return parent->getScope();
}
}
ProgramTree::~ProgramTree(){
for(int i=0;i<children.size();i++){
delete children[i];
}
}