File tree Expand file tree Collapse file tree 4 files changed +44
-7
lines changed
Expand file tree Collapse file tree 4 files changed +44
-7
lines changed Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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+ }
Original file line number Diff line number Diff 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);
You can’t perform that action at this time.
0 commit comments