-
Notifications
You must be signed in to change notification settings - Fork 0
/
scope.cpp
118 lines (99 loc) · 2.35 KB
/
scope.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
#include "scope.h"
Scope::~Scope()
{
delete this->topASTNode;
for(auto i:this->functionList)
delete i.second;
for(auto i:this->variableList)
delete i.second;
for(Scope* i:this->sonScope)
delete i;
if(fatherScope)
for(auto i = fatherScope->sonScope.begin(); i < fatherScope->sonScope.end(); i++)
{
if(*i == this)
fatherScope->sonScope.erase(i);
}
}
Variable* Scope::addVariable(string name)
{
Variable* var=new Variable();
#ifdef READABLEGEN
var->NAME=name;
#endif
this->variableList[name]=var;
return var;
}
void Scope::addVariable(string name, Variable *var)
{
#ifdef READABLEGEN
var->NAME=name;
#endif
this->variableList[name]=var;
}
void Scope::addFunction(string name, Function *fun)
{
#ifdef READABLEGEN
fun->NAME=name;
#endif
this->functionList[name]=fun;
}
/*void Scope::deleteFunction(string name)
{
delete this->functionList[name];
this->functionList.erase(name);
}
void Scope::deleteVariable(string name)
{
delete this->variableList[name];
this->variableList.erase(name);
}*/
void Scope::deleteVariable(Variable *var)
{
auto p = variableList.begin();
while(p != variableList.end())
{
if(p->second == var)
variableList.erase(p++);
else
p++;
}
}
void Scope::deleteFunction(Function *fun)
{
auto p = functionList.begin();
while(p != functionList.end())
{
if(p->second == fun)
functionList.erase(p++);
else
p++;
}
}
Variable* Scope::findVariable(string name,bool thisScope)
{
//冷漠:variableList[name]会在variablList里面创建一个空的name
Variable* result = nullptr;
if(variableList.count(name))
result = variableList[name];
if(result != nullptr)
return result;
if(thisScope)
return result;
if(fatherScope == nullptr)
return nullptr;
return this->fatherScope->findVariable(name,false);
}
Function* Scope::findFunction(string name,bool thisScope)
{
Function* result = nullptr;
if(functionList.count(name))
result = functionList[name];
if(result != nullptr)
return result;
if(thisScope)
return result;
if(fatherScope == nullptr)
return nullptr;
return this->fatherScope->findFunction(name,false);
}