Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复 break continue 导致死循环以及嵌套异常问题 #130

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ public boolean createInstruction(ExpressRunner aCompile,InstructionSet result,
//修改Break和Continue指令的跳转位置,循环出堆
ForRelBreakContinue rel = forStack.pop();
for(InstructionGoTo item:rel.breakList){
item.setOffset(result.getCurrentPoint() - item.getOffset()) ;
item.setOffset(result.getCurrentPoint() - item.getOffset() + 1) ;
}
for(InstructionGoTo item:rel.continueList){
item.setOffset(selfAddPoint - item.getOffset() - 1);
item.setOffset(selfAddPoint - item.getOffset());
}

//生成作用域结束指令
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,23 @@ public boolean createInstruction(ExpressRunner aCompile,InstructionSet result,
children[2] = new ExpressNode(aCompile.getNodeTypeManager().findNodeType("STAT_BLOCK"),null);
}
int [] finishPoint = new int[children.length];
boolean r1 = aCompile.createInstructionSetPrivate(result,forStack,children[0],false);//condition

boolean r1 = aCompile.createInstructionSetPrivate(result,forStack,children[0],false);//condition
InstructionGoToWithCondition goToFalseBody = new InstructionGoToWithCondition(false,
0,true);
goToFalseBody.setLine(node.getLine());
result.addInstruction(goToFalseBody);
finishPoint[0] = result.getCurrentPoint();
boolean r2 = aCompile.createInstructionSetPrivate(result,forStack,children[1],false);//true
result.insertInstruction(finishPoint[0]+1,new InstructionGoToWithCondition(false,result.getCurrentPoint() - finishPoint[0] + 2,true).setLine(node.getLine()));

boolean r2 = aCompile.createInstructionSetPrivate(result,forStack,children[1],false);//true
InstructionGoTo goToEnd = new InstructionGoTo(0);
goToEnd.setLine(node.getLine());
result.addInstruction(goToEnd);
goToFalseBody.setOffset(result.getCurrentPoint() - finishPoint[0] + 1);

finishPoint[1] = result.getCurrentPoint();
boolean r3 = aCompile.createInstructionSetPrivate(result,forStack,children[2],false);//false
result.insertInstruction(finishPoint[1]+1,new InstructionGoTo(result.getCurrentPoint() - finishPoint[1] + 1).setLine(node.getLine()));
goToEnd.setOffset(result.getCurrentPoint() - finishPoint[1] + 1);
return r1 || r2 || r3;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.ql.util.express.test;

import com.ql.util.express.*;
import com.ql.util.express.instruction.op.OperatorBase;
import org.junit.Assert;
import org.junit.Test;

import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;


public class ForFlowFunctionTest {

Expand All @@ -20,5 +19,4 @@ public void testABC() throws Exception {
Object r = runner.execute(express, context, null, false, true);
Assert.assertTrue("for循环后面跟着一个函数的时候错误", r.toString().equals("10"));
}

}
2 changes: 1 addition & 1 deletion src/test/java/com/ql/util/express/test/IfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ public void testIf() throws Exception{
System.out.println("环境结果:" + expressContext);
Assert.assertTrue("表达式执行错误:" + expresses[i][0] + " 期望值:" + expresses[i][1] +" 运算结果:" + result ,expresses[i][1].equals(result == null?"null":result.toString()));
}
}
}
}
9 changes: 6 additions & 3 deletions src/test/java/com/ql/util/express/test/NewExpressTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ public void testParse() throws Exception{
{"int 中国 = 100; int 美国 = 200 ;return 中国 + 美国","300"},
{"1 加 1 ","2"},
{" 'a' love 'b' love 'c'","c{b{a}b}c"},
{"if 1==2 then {return 10}else{return 100}","100"}
{"if 1==2 then {return 10}else{return 100}","100"},
{"for (i=1;i<10;i++){continue}i", "10"},
{"for (i=1;i<10;i++){break}i", "1"}
};
for(int i=0;i<expresses.length;i++){
IExpressContext<String,Object> expressContext = new DefaultContext<String,Object>();
Expand All @@ -78,8 +80,9 @@ public void testParse() throws Exception{
runner.addOperator("love","+",new LoveOperator("love"));
Object result = runner.execute(expresses[i][0],expressContext, null, false,true);
System.out.println("运算结果:" + result);
System.out.println("环境结果:" + expressContext);
Assert.assertTrue("表达式执行错误:" + expresses[i][0] + " 期望值:" + expresses[i][1] +" 运算结果:" + result ,expresses[i][1].equals(result == null?"null":result.toString()));
System.out.println("环境结果:" + expressContext);
Assert.assertEquals("表达式执行错误:" + expresses[i][0] + " 期望值:" + expresses[i][1] + " 运算结果:"
+ result, expresses[i][1], result == null ? "null" : result.toString());
}
}
}