Skip to content

Commit 914361c

Browse files
committed
对多级scope更好的支持
1 parent 0eac24e commit 914361c

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

SCMath.pro.user

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE QtCreatorProject>
3-
<!-- Written by QtCreator 4.2.1, 2019-03-16T16:42:03. -->
3+
<!-- Written by QtCreator 4.2.1, 2019-03-26T23:43:14. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>

ast.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,14 @@ BasicNode* ast::__ToAST(string &s)
169169
else //变量
170170
{
171171
string name=s.substr(i, j - i);
172-
if(record::globalScope.haveVariable(name)==false) //先前没有这变量
172+
Variable* v=record::globalScope.findVariable(name);
173+
if(v==nullptr) //先前没有这变量
173174
{
174-
Variable* v=record::globalScope.addVariable(name);
175+
v=record::globalScope.addVariable(name);
175176
Variable* pv=record::globalScope.addVariable("ptr"+name);
176177
pv->setBorrowVal(v);
177178
}
178-
stackAST.push(record::globalScope.variableList[name]);
179+
stackAST.push(v);
179180
i = j;
180181
}
181182
}

scope.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,39 @@ void Scope::deleteFunction(Function *fun)
6666
this->functionList.erase(p.first);
6767
}
6868
}
69+
70+
Variable* Scope::findVariable(string name,bool thisScope)
71+
{
72+
Variable* result=this->variableList[name];
73+
74+
if(result==0)
75+
result=nullptr;
76+
77+
if(thisScope)
78+
return result;
79+
else
80+
{
81+
if(result==nullptr)
82+
return nullptr;
83+
else
84+
return this->fatherScope->findVariable(name,false);
85+
}
86+
}
87+
88+
Function* Scope::findFunction(string name,bool thisScope)
89+
{
90+
Function* result=this->functionList[name];
91+
92+
if(result==0)
93+
result=nullptr;
94+
95+
if(thisScope)
96+
return result;
97+
else
98+
{
99+
if(result==nullptr)
100+
return nullptr;
101+
else
102+
return this->fatherScope->findFunction(name,false);
103+
}
104+
}

scope.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ class Scope //作用是记录不同语句块内函数及变量名到实体的映
1414
Scope* fatherScope; //因为可能顺着作用域向上层找变量,所以需要上级节点的指针
1515
Scope(Scope* fatherScope=nullptr):fatherScope(fatherScope){}
1616

17-
Variable *addVariable(string name);
17+
Variable* addVariable(string name);
1818
void addVariable(string name,Variable* var);
1919
void addFunction(string name,Function* fun);
20-
bool haveVariable(string name) {return this->variableList[name]!=0;}
21-
bool haveFunction(string name) {return this->functionList[name]!=0;}
20+
Variable* findVariable(string name, bool thisScope=true);
21+
Function* findFunction(string name,bool thisScope=true);
2222
//直接删除实体,在已经将所有实体引用在树中清空时使用(目前还没看到有什么用)
2323
//void deleteVariable(string name);
2424
//void deleteFunction(string name);

0 commit comments

Comments
 (0)