Skip to content

Commit b6c5d92

Browse files
committed
支持Scope中删除实体,绑定顶层节点
1 parent eda903c commit b6c5d92

6 files changed

Lines changed: 55 additions & 16 deletions

File tree

ast.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "ast.h"
22
#include "funinterface.h"
3-
#ifdef parserdebug
3+
#ifdef READABLEcodegen
44
#include<iostream>
55
#endif
66

@@ -95,7 +95,7 @@ BasicNode* ast::ToAST(string s) {
9595
return stackAST.top();
9696
}
9797

98-
#ifdef parserdebug
98+
#ifdef READABLEcodegen
9999
void ast::outputAST(BasicNode* now){
100100
if(now == nullptr)
101101
return;

ast.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace ast
2020
BasicNode* ToAST(string);
2121
bool canpush(stack<string> &, string);
2222

23-
#ifdef parserdebug
23+
#ifdef READABLEcodegeng
2424
void outputAST(BasicNode *);
2525
#endif
2626

nodetype.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,7 @@ BasicNode* Function::eval(vector<BasicNode *> &sonNode)
250250

251251
Function::~Function()
252252
{
253-
if(this->pronode!=nullptr)
254-
delete this->pronode;
253+
delete this->pronode;
255254
for(VarReference* i:this->formalParList)
256255
delete i;
257256
}

nodetype.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <vector>
44
#include <functional>
55
using namespace std;
6-
#define parserdebug
6+
#define READABLEcodegen
77

88
enum nodeType{Num,String,Var,Pro,Fun,VarRef};
99

@@ -80,7 +80,7 @@ class VarNode : public BasicNode
8080
void setBorrowVal(BasicNode* val); //直接对值进行赋值,用这个不转移所有权(一般赋值为变量指针用)
8181
void setVarVal(VarNode* node); //传递变量的值到this的值,即需要进行一次解包
8282
void clearVal();
83-
#ifdef parserdebug
83+
#ifdef READABLEcodegen
8484
string NAME;
8585
#endif
8686
};
@@ -150,7 +150,7 @@ class Function
150150
bool isVLP() {return this->VLP;}
151151
void addFormalPar(VarReference* var); //先在外面new好,然后转移所有权进来
152152
BasicNode* eval(vector<BasicNode *> &sonNode);
153-
#ifdef parserdebug
153+
#ifdef READABLEcodegen
154154
string NAME;
155155
#endif
156156
};

scope.cpp

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
Scope::~Scope()
44
{
5+
delete this->topASTNode;
56
for(auto i:this->functionList)
67
delete i.second;
78
for(auto i:this->variableList)
@@ -10,27 +11,57 @@ Scope::~Scope()
1011
delete i;
1112
}
1213

13-
void Scope::addValue(string name)
14+
void Scope::addVariable(string name)
1415
{
1516
Variable* var=new Variable();
16-
#ifdef parserdebug
17+
#ifdef READABLEcodegen
1718
var->NAME=name;
1819
#endif
1920
this->variableList[name]=var;
2021
}
2122

22-
void Scope::addValue(string name, Variable *var)
23+
void Scope::addVariable(string name, Variable *var)
2324
{
24-
#ifdef parserdebug
25+
#ifdef READABLEcodegen
2526
var->NAME=name;
2627
#endif
2728
this->variableList[name]=var;
2829
}
2930

3031
void Scope::addFunction(string name, Function *fun)
3132
{
32-
#ifdef parserdebug
33+
#ifdef READABLEcodegen
3334
fun->NAME=name;
3435
#endif
3536
this->functionList[name]=fun;
3637
}
38+
39+
/*void Scope::deleteFunction(string name)
40+
{
41+
delete this->functionList[name];
42+
this->functionList.erase(name);
43+
}
44+
45+
void Scope::deleteVariable(string name)
46+
{
47+
delete this->variableList[name];
48+
this->variableList.erase(name);
49+
}*/
50+
51+
void Scope::deleteVariable(Variable *var)
52+
{
53+
for(auto p:this->variableList)
54+
{
55+
if(p.second==var)
56+
this->variableList.erase(p.first);
57+
}
58+
}
59+
60+
void Scope::deleteFunction(Function *fun)
61+
{
62+
for(auto p:this->functionList)
63+
{
64+
if(p.second==fun)
65+
this->functionList.erase(p.first);
66+
}
67+
}

scope.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,24 @@
44

55
class Scope //作用是记录不同语句块内函数及变量名到实体的映射,不是运行时栈
66
{
7+
private:
8+
BasicNode* topASTNode=nullptr; //这是完全包含本域所有实体引用的根节点,否则会释放外部需要使用的实体
79
public:
810
~Scope();
911
map<string,Variable*> variableList;
1012
map<string,Function*> functionList;
1113
vector<Scope*> sonScope;
12-
Scope* fatherScope;
14+
Scope* fatherScope; //因为可能顺着作用域向上层找变量,所以需要上级节点的指针
1315
Scope(Scope* fatherScope=nullptr):fatherScope(fatherScope){}
1416

15-
void addValue(string name);
16-
void addValue(string name,Variable* var);
17+
void addVariable(string name);
18+
void addVariable(string name,Variable* var);
1719
void addFunction(string name,Function* fun);
20+
//直接删除实体,在已经将所有实体引用在树中清空时使用(目前还没看到有什么用)
21+
//void deleteVariable(string name);
22+
//void deleteFunction(string name);
23+
//删除实体指针在域中的存储,在已经将所有实体引用在树中清空时使用且已经delete时使用
24+
void deleteVariable(Variable* var);
25+
void deleteFunction(Function* fun);
26+
void settopASTNode(BasicNode* topnode) {this->topASTNode=topnode;}
1827
};

0 commit comments

Comments
 (0)