Skip to content

Commit 2bab1e3

Browse files
committed
完成一个比较低效的析构
1 parent df6e7ed commit 2bab1e3

6 files changed

Lines changed: 35 additions & 9 deletions

File tree

SCMath.pro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ SOURCES += \
77
ast.cpp \
88
main.cpp \
99
nodetype.cpp \
10-
funinterface.cpp
10+
funinterface.cpp \
11+
scope.cpp
1112

1213
HEADERS += \
1314
ast.h \

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, 2018-10-16T20:00:47. -->
3+
<!-- Written by QtCreator 4.2.1, 2018-10-16T21:20:37. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>

nodetype.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
#include "nodetype.h"
22

3+
BasicNode::~BasicNode()
4+
{
5+
for(BasicNode* node:this->sonNode)
6+
{
7+
if(node->getType()!=Variable) //这个随着域释放,不被连环析构
8+
delete node;
9+
}
10+
}
11+
312
void VariableNode::setVal(BasicNode num)
413
{
514
if(num.getType()==Num||num.getType()==String)
@@ -27,7 +36,9 @@ void iterEval(BasicNode* &node)
2736
throw string("ProNode cannot be function's sonNode");
2837
else
2938
{
30-
node=node->eval(); //节点的替换在这里(父节点)完成,子节点只需要返回即可(fix:现在还没析构原来的子节点,待会写)
39+
BasicNode* result=node->eval();
40+
delete node;
41+
node=result; //节点的替换在这里(父节点)完成,子节点只需要返回即可
3142
}
3243
}
3344

nodetype.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
#include <functional>
55
using namespace std;
66

7-
enum nodeType{Basic,Num,String,Variable,Pro,Fun};
7+
enum nodeType{Num,String,Variable,Pro,Fun};
88

99
class BasicNode //不可直接创建对象
1010
{
1111
protected:
1212
bool isRet=false;
1313
public:
14-
virtual int getType(){return Basic;}
14+
virtual int getType();
1515
virtual void addNode(BasicNode* node) {this->sonNode.push_back(node);} //使用该方法添加成员可查错
1616
virtual BasicNode* eval();
17+
virtual ~BasicNode();
1718

1819
void setRet() {this->isRet=true;} //不可eval节点设置ret无效
1920
bool getRet() {return this->isRet;}
@@ -30,7 +31,7 @@ class NumNode : public BasicNode
3031
public:
3132
virtual int getType() {return Num;}
3233
virtual void addNode(BasicNode *node) {throw string("NumNode no sonNode");}
33-
virtual BasicNode* eval() {return dynamic_cast<BasicNode*>(this);}
34+
virtual BasicNode* eval() {return dynamic_cast<BasicNode*>(new NumNode(num));} //字面量在计算后将会析构,这里要new新的
3435
NumNode(double num) {this->num=num;}
3536

3637
double getNum() {return this->num;}
@@ -44,7 +45,7 @@ class StringNode : public BasicNode
4445
public:
4546
virtual int getType() {return String;}
4647
virtual void addNode(BasicNode *node) {throw string("String no sonNode");}
47-
virtual BasicNode* eval() {return dynamic_cast<BasicNode*>(this);}
48+
virtual BasicNode* eval() {return dynamic_cast<BasicNode*>(new StringNode(str));} //同上
4849
StringNode(string str) {this->str=str;}
4950

5051
string getStr() {return this->str;}
@@ -59,7 +60,7 @@ class VariableNode : public BasicNode
5960
public:
6061
virtual int getType() {return Variable;}
6162
virtual void addNode(BasicNode* node) {throw string("VariableNode no sonNode");}
62-
virtual BasicNode* eval() {return dynamic_cast<BasicNode*>(this);}
63+
virtual BasicNode* eval() {return dynamic_cast<BasicNode*>(this);} //变量在scope销毁时释放,所以可以返回自身
6364

6465
bool isEmpty() {return this->isempty;}
6566
void setVal(BasicNode val);
@@ -94,6 +95,7 @@ class Function
9495
Function(int parnum,ProNode* pronode=nullptr,bool VLP=false):parnum(parnum),pronode(pronode),VLP(VLP){} //普通函数(有函数体)
9596
Function(int parnum,canBE canBEfun,BE BEfun,bool VLP=false):
9697
parnum(parnum),canBEfun(canBEfun),BEfun(BEfun),VLP(VLP),iscanBE(true){} //调用到函数接口
98+
~Function() {delete pronode;}
9799

98100
ProNode* getPronode() {return this->pronode;}
99101
int getParnum() {return this->parnum;}
@@ -105,7 +107,7 @@ class Function
105107
class FunNode : public BasicNode
106108
{
107109
protected:
108-
Function* funEntity;
110+
Function* funEntity; //所有权在scope,不在这里析构
109111
public:
110112
virtual int getType() {return Fun;}
111113
virtual void addNode(BasicNode* node);

scope.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "scope.h"
2+
3+
Scope::~Scope()
4+
{
5+
for(auto i:this->functionList)
6+
delete i.second;
7+
for(auto i:this->sonScope)
8+
delete i.second;
9+
for(Scope* i:this->sonScope)
10+
delete i;
11+
}

scope.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
class Scope
66
{
77
public:
8+
~Scope();
89
map<string,VariableNode*> valueList;
910
map<string,Function*> functionList;
1011
vector<Scope*> sonScope;

0 commit comments

Comments
 (0)