Skip to content

Commit 21b5796

Browse files
committed
修正变量求值的若干问题
1 parent 399d721 commit 21b5796

5 files changed

Lines changed: 43 additions & 15 deletions

File tree

ast.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ void ast::Init()
99
BinOpPriority['*'] = 20;
1010
BinOpPriority['/'] = 20;
1111
BinOpPriority['^'] = 30;*/
12+
13+
1214
}
1315

1416
bool isNum(const char &c) {

nodetype.cpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ BasicNode::~BasicNode()
1414
}
1515
}
1616

17-
void VariableNode::setVal(BasicNode num)
17+
18+
VariableNode::~VariableNode()
19+
{
20+
delete this->val; //然后BasicNode析构
21+
}
22+
23+
void VariableNode::setVal(BasicNode* num)
1824
{
19-
if(isLiteral(&num))
25+
if(isLiteral(num))
2026
{
2127
this->isEmpty=false;
2228
this->num=num;
@@ -25,6 +31,20 @@ void VariableNode::setVal(BasicNode num)
2531
throw string("The value of a variable must be literal");
2632
}
2733

34+
BasicNode* VariableNode::eval()
35+
{
36+
if(this->isEmpty)
37+
return dynamic_cast<BasicNode*>(this);
38+
else
39+
return this->val;
40+
}
41+
42+
void VariableNode::clearVal()
43+
{
44+
this->isempty=true;
45+
delete this->val;
46+
}
47+
2848

2949
void FunNode::addNode(BasicNode *node)
3050
{
@@ -41,13 +61,16 @@ void iterEval(BasicNode* &node)
4161
throw string("ProNode cannot be function's sonNode");
4262
else
4363
{
44-
if(isLiteral(node)||node->getType()==Variable)
45-
return; //如果是字面量和变量,自己就是求值结果,下面再重新赋值一次就重复了
64+
if(isLiteral(node))
65+
return; //如果是字面量,自己就是求值结果,下面再重新赋值一次就重复了
4666
else
4767
{
4868
BasicNode* result=node->eval();
49-
delete node;
69+
if(node->getType()!=Variable)
70+
delete node;
5071
node=result; //节点的替换在这里(父节点)完成,子节点只需要返回即可
72+
//对于已经赋值的变量,整体过程是用值替代本身变量在AST中的位置,不过变量本身并没有被析构,因为变量的所有权在scope(后面可能还要访问)
73+
//对于未赋值的变量,求值结果是变量本身,AST没有变化
5174
}
5275
}
5376
}

nodetype.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ class StringNode : public BasicNode
5656
class VariableNode : public BasicNode
5757
{
5858
protected:
59-
BasicNode val;
59+
BasicNode* val=nullptr;
6060
bool isempty=true;
6161
public:
6262
virtual int getType() {return Variable;}
6363
virtual void addNode(BasicNode* node) {throw string("VariableNode no sonNode");}
64-
virtual BasicNode* eval() {return dynamic_cast<BasicNode*>(this);}
64+
virtual BasicNode* eval();
65+
virtual ~VariableNode();
6566

6667
bool isEmpty() {return this->isempty;}
67-
void setVal(BasicNode val);
68-
BasicNode getVal() {return this->val;}
69-
void clearVal() {this->isempty=true;}
68+
void setVal(BasicNode* val); //注意,传进来的应该是new出来的,传进来意味着转移所有权到本类
69+
void clearVal();
7070
};
7171

7272

@@ -85,7 +85,7 @@ class Function
8585
{
8686
private:
8787
int parnum; //参数个数
88-
ProNode* pronode; //是ret节点返回,最后一个元素视为返回值(如果没有填nullptr)
88+
ProNode* pronode; //是ret节点返回,最后一个元素视为返回值(如果没有填nullptr)(fix:这个路子是错的,ret方式需要更改)
8989
bool VLP; //是否不进行参数个数检查
9090
//关于基础求值
9191
canBE canBEfun;
@@ -98,7 +98,7 @@ class Function
9898
parnum(parnum),canBEfun(canBEfun),BEfun(BEfun),VLP(VLP),iscanBE(true){} //调用到函数接口
9999
~Function() {delete pronode;}
100100

101-
ProNode* getPronode() {return this->pronode;}
101+
ProNode* getFunBody() {return this->pronode;}
102102
int getParnum() {return this->parnum;}
103103
bool isVLP() {return this->VLP;}
104104
BasicNode* eval(vector<BasicNode *> &sonNode);
@@ -115,5 +115,5 @@ class FunNode : public BasicNode
115115
virtual BasicNode* eval();
116116
FunNode(Function* funEntity):funEntity(funEntity){}
117117

118-
ProNode* getFunBody() {return this->funEntity->getPronode();}
118+
ProNode* getFunBody() {return this->funEntity->getFunBody();}
119119
};

scope.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Scope::~Scope()
44
{
55
for(auto i:this->functionList)
66
delete i.second;
7-
for(auto i:this->sonScope)
7+
for(auto i:this->variableList)
88
delete i.second;
99
for(Scope* i:this->sonScope)
1010
delete i;

scope.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ class Scope
66
{
77
public:
88
~Scope();
9-
map<string,VariableNode*> valueList;
9+
map<string,VariableNode*> variableList;
1010
map<string,Function*> functionList;
1111
vector<Scope*> sonScope;
12+
13+
void addValue(string name) {this->variableList.insert(name,new VariableNode);} //先插进去后赋值
14+
void addFunction(string name,Function* fun) {this->functionList.insert(name,fun);} //这个Function必须在外面先new好,因为参数比较多
1215
};

0 commit comments

Comments
 (0)