Skip to content

Commit 2aae604

Browse files
committed
发现PARTEVAL有BUG,大改一遍应该改好了
1 parent 1bd0bd9 commit 2aae604

File tree

7 files changed

+74
-43
lines changed

7 files changed

+74
-43
lines changed

SCMath.pro.user

Lines changed: 3 additions & 3 deletions
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-11-01T08:15:45. -->
3+
<!-- Written by QtCreator 4.2.1, 2018-11-01T09:09:04. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>
@@ -282,7 +282,7 @@
282282
<value type="int">13</value>
283283
<value type="int">14</value>
284284
</valuelist>
285-
<value type="int" key="PE.EnvironmentAspect.Base">-1</value>
285+
<value type="int" key="PE.EnvironmentAspect.Base">2</value>
286286
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
287287
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">SCMath</value>
288288
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
@@ -292,7 +292,7 @@
292292
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.ProFile">SCMath.pro</value>
293293
<value type="bool" key="Qt4ProjectManager.Qt4RunConfiguration.UseDyldImageSuffix">false</value>
294294
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.UserWorkingDirectory"></value>
295-
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.UserWorkingDirectory.default"></value>
295+
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.UserWorkingDirectory.default">D:/gitreg/build-SCMath-Desktop_Qt_5_8_0_MinGW_32bit-Debug</value>
296296
<value type="uint" key="RunConfiguration.QmlDebugServerPort">3768</value>
297297
<value type="bool" key="RunConfiguration.UseCppDebugger">false</value>
298298
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>

excep.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@ class VarRefUnbindExcep : public Excep {
2929

3030
};
3131

32+
class argumentNumExceedingExcep : public Excep {
33+
34+
};
35+
36+
class parameterNumExceedingExcep : public Excep {
37+
38+
};
39+
40+
enum callCheckMismatchType{NumberMismatch,TypeMisMatch};
41+
class callCheckMismatchExcep : public Excep
42+
{
43+
private:
44+
int type;
45+
public:
46+
callCheckMismatchExcep(int type):type(type) {}
47+
int getType() {return this->type;}
48+
};
49+
3250
class addSonExcep : public Excep
3351
{
3452
private:

main.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ int main()
1212
BasicNode* ans = ast::ToAST(s);
1313
output::outputAST(ans);
1414
cout << endl;
15-
try{
16-
output::outputAST(ans ->eval());
17-
}
18-
catch(string ){};
15+
output::outputAST(ans ->eval());
1916
cout << endl;
2017
output::outputAST(ans);
2118
return 0;

nodetype.cpp

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ void FunNode::addNode(BasicNode *node)
172172
{
173173
if(!this->funEntity->isVLP()) //支持变长参数就先不进行参数个数检查
174174
if(this->sonNode.size()+1>this->funEntity->getParnum())
175-
throw string("Exceeding the number of function parameters");
175+
throw parameterNumExceedingExcep();
176176

177177
this->sonNode.push_back(node);
178178
}
@@ -181,7 +181,21 @@ BasicNode* FunNode::eval()
181181
{
182182
if(this->funEntity==nullptr)
183183
throw string("funEntity is null");
184-
return this->funEntity->eval(this->sonNode);
184+
#ifdef PARTEVAL
185+
try
186+
{
187+
#endif
188+
return this->funEntity->eval(this->sonNode);
189+
}
190+
#ifdef PARTEVAL
191+
catch(callCheckMismatchExcep e) //因为未赋值变量未求值使得参数类型不匹配,放弃对这个函数求值
192+
{
193+
if(e.getType()==TypeMisMatch)
194+
return this;
195+
else
196+
throw e;
197+
}
198+
#endif
185199
}
186200

187201

@@ -197,12 +211,14 @@ void recursionEval(BasicNode* &node)
197211
{
198212
BasicNode* result;
199213
#ifdef PARTEVAL
200-
try {
214+
try
215+
{
201216
#endif
202217
result=node->eval();
203218
#ifdef PARTEVAL
204219
}
205-
catch(unassignedEvalExcep) {}
220+
catch(unassignedEvalExcep) //对未赋值变量求值,保持原样
221+
{result=node;}
206222
#endif
207223
if(node->getType()!=Var)
208224
delete node;
@@ -225,32 +241,32 @@ BasicNode* Function::eval(vector<BasicNode *> &sonNode)
225241
if(this->canBEfun(sonNode)) //参数合法
226242
return this->BEfun(sonNode);
227243
else
228-
throw string("sonNode type mismatch");
244+
throw callCheckMismatchExcep(TypeMisMatch);
229245
}
230246
else //不能基础求值就是正常有函数体Pro的
231247
{
232-
this->bindFormalPar(sonNode); //子节点绑定到实参
248+
this->bindArgument(sonNode); //子节点绑定到实参
233249
vector<BasicNode*>&funbody=this->pronode->sonNode;
234-
for(int i=0;i<funbody.size()-1;i++) //最后一个可能是返回值,先留着后面单独处理
250+
for(unsigned int i=0;i<funbody.size()-1;i++) //最后一个可能是返回值,先留着后面单独处理
235251
{
236252
recursionEval(funbody.at(i));
237253
if(funbody.at(i)->isRet())
238254
{
239-
this->unbindFormalPar();
255+
this->unbindArgument();
240256
return funbody.at(i);
241257
}
242258
}
243259
//前面都不是返回值,最后一个是
244260
BasicNode* lastnode=funbody.at(funbody.size()-1);
245261
if(lastnode==nullptr)
246262
{
247-
this->unbindFormalPar();
263+
this->unbindArgument();
248264
return nullptr;
249265
}
250266
else
251267
{
252268
recursionEval(lastnode);
253-
this->unbindFormalPar();
269+
this->unbindArgument();
254270
return lastnode;
255271
}
256272
}
@@ -259,30 +275,30 @@ BasicNode* Function::eval(vector<BasicNode *> &sonNode)
259275
Function::~Function()
260276
{
261277
delete this->pronode;
262-
for(VarReference* i:this->formalParList)
278+
for(VarReference* i:this->argumentList)
263279
delete i;
264280
}
265281

266-
void Function::addFormalPar(VarReference *var)
282+
void Function::addArgument(VarReference *var)
267283
{
268-
if(this->formalParList.size()+1>this->getParnum())
269-
throw string("Exceeding the number of parameters");
270-
this->formalParList.push_back(var);
284+
if(this->argumentList.size()+1>this->getParnum())
285+
throw argumentNumExceedingExcep();
286+
this->argumentList.push_back(var);
271287
}
272288

273-
void Function::unbindFormalPar()
289+
void Function::unbindArgument()
274290
{
275-
for(VarReference* i:this->formalParList)
291+
for(VarReference* i:this->argumentList)
276292
i->unbind();
277293
}
278294

279-
void Function::bindFormalPar(vector<BasicNode *> &sonNode)
295+
void Function::bindArgument(vector<BasicNode *> &sonNode)
280296
{
281-
if(sonNode.size()!=this->formalParList.size())
282-
throw string("sonNode number mismatch");
283-
for(int i=0;i<this->formalParList.size();i++)
297+
if(sonNode.size()!=this->argumentList.size())
298+
throw callCheckMismatchExcep(NumberMismatch);
299+
for(unsigned int i=0;i<this->argumentList.size();i++)
284300
{
285-
VarReference* formalpar=this->formalParList.at(i);
301+
VarReference* formalpar=this->argumentList.at(i);
286302
formalpar->bind(sonNode.at(i));
287303
}
288304
}

nodetype.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ class NumNode : public BasicNode
3131
double num;
3232
public:
3333
virtual int getType() {return Num;}
34-
virtual void addNode(BasicNode *node) {throw addSonExcep(Num);}
35-
virtual BasicNode* eval() {return dynamic_cast<BasicNode*>(this);}
34+
virtual void addNode(BasicNode*) {throw addSonExcep(Num);}
35+
virtual BasicNode* eval() {return this;}
3636
NumNode(double num) {this->num=num;}
3737
NumNode(NumNode* node):num(node->num){}
3838

@@ -46,8 +46,8 @@ class StringNode : public BasicNode
4646
string str;
4747
public:
4848
virtual int getType() {return String;}
49-
virtual void addNode(BasicNode *node) {throw addSonExcep(String);}
50-
virtual BasicNode* eval() {return dynamic_cast<BasicNode*>(this);}
49+
virtual void addNode(BasicNode*) {throw addSonExcep(String);}
50+
virtual BasicNode* eval() {return this;}
5151
StringNode(string str) {this->str=str;}
5252
StringNode(StringNode* node):str(node->str){}
5353

@@ -66,7 +66,7 @@ class VarNode : public BasicNode
6666
void assignmentChecking(BasicNode* val);
6767
public:
6868
virtual int getType() {return Var;}
69-
virtual void addNode(BasicNode* node) {throw addSonExcep(Var);}
69+
virtual void addNode(BasicNode*) {throw addSonExcep(Var);}
7070
virtual BasicNode* eval();
7171
virtual ~VarNode();
7272
VarNode(int valtype=-1);
@@ -99,7 +99,7 @@ class VarRefNode : public BasicNode
9999
void setBorrowVal(BasicNode* val);
100100
public:
101101
virtual int getType() {return VarRef;}
102-
virtual void addNode(BasicNode* node) {throw addSonExcep(VarRef);}
102+
virtual void addNode(BasicNode*) {throw addSonExcep(VarRef);}
103103
virtual ~VarRefNode();
104104
virtual BasicNode* eval(); //eval结果是目前形参绑定到的实参
105105
VarRefNode(int valtype=-1);
@@ -135,9 +135,9 @@ class Function
135135
bool iscanBE=false;
136136
//关于pro求值
137137
ProNode* pronode=nullptr; //是ret节点返回,最后一个元素视为返回值(如果没有填nullptr)(fix:这个ret路子可能是错的)
138-
vector<VarReference*>formalParList; //形参列表,持有所有权。(warn:用了这种方法将很难并行化,一个函数实体同时只能被一组实参占用)
139-
void unbindFormalPar();
140-
void bindFormalPar(vector<BasicNode*>&sonNode);
138+
vector<VarReference*>argumentList; //形参列表,持有所有权。(warn:用了这种方法将很难并行化,一个函数实体同时只能被一组实参占用)
139+
void unbindArgument();
140+
void bindArgument(vector<BasicNode*>&sonNode);
141141
public:
142142
Function(unsigned int parnum,ProNode* pronode,bool VLP=false):parnum(parnum),pronode(pronode),VLP(VLP){} //普通函数(有函数体)
143143
Function(unsigned int parnum,canBE canBEfun,BE BEfun,bool VLP=false):
@@ -147,7 +147,7 @@ class Function
147147
ProNode* getFunBody() {return this->pronode;}
148148
unsigned int getParnum() {return this->parnum;}
149149
bool isVLP() {return this->VLP;}
150-
void addFormalPar(VarReference* var); //先在外面new好,然后转移所有权进来
150+
void addArgument(VarReference* var); //先在外面new好,然后转移所有权进来
151151
BasicNode* eval(vector<BasicNode *> &sonNode);
152152
#ifdef READABLEGEN
153153
string NAME;

output.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void output::outputAST(BasicNode* now, int fatherpriority)
2424
return;
2525
}
2626
cout << l->NAME << '(';
27-
for(int i = 0 ; i < l->getParnum(); i++){
27+
for(unsigned int i = 0 ; i < l->getParnum(); i++){
2828
outputAST(t->sonNode[i], 0);
2929
if(i != l->getParnum() - 1)
3030
cout << ", ";

scope.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@ Scope::~Scope()
1414
void Scope::addVariable(string name)
1515
{
1616
Variable* var=new Variable();
17-
#ifdef READABLEcodegen
17+
#ifdef READABLEGEN
1818
var->NAME=name;
1919
#endif
2020
this->variableList[name]=var;
2121
}
2222

2323
void Scope::addVariable(string name, Variable *var)
2424
{
25-
#ifdef READABLEcodegen
25+
#ifdef READABLEGEN
2626
var->NAME=name;
2727
#endif
2828
this->variableList[name]=var;
2929
}
3030

3131
void Scope::addFunction(string name, Function *fun)
3232
{
33-
#ifdef READABLEcodegen
33+
#ifdef READABLEGEN
3434
fun->NAME=name;
3535
#endif
3636
this->functionList[name]=fun;

0 commit comments

Comments
 (0)