Skip to content
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 @@ -28,6 +28,7 @@ public boolean createInstruction(ExpressRunner aCompile,InstructionSet result,
if (hasDef == true&& isRoot == false
&& node.getTreeType().isEqualsOrChild("STAT_BLOCK")){
result.insertInstruction(tmpPoint,new InstructionOpenNewArea().setLine(node.getLine()));
forStack.peek().sign();
result.insertInstruction(result.getCurrentPoint() + 1,new InstructionCloseNewArea().setLine(node.getLine()));
returnVal = false;
}else{
Expand Down
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() - item.getMoveTimes() +1) ;
}
for(InstructionGoTo item:rel.continueList){
item.setOffset(selfAddPoint - item.getOffset() - 1);
item.setOffset(selfAddPoint - item.getOffset() - item.getMoveTimes());
}

//生成作用域结束指令
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@ public class ForRelBreakContinue{
List<InstructionGoTo> breakList = new ArrayList<InstructionGoTo>();
List<InstructionGoTo> continueList = new ArrayList<InstructionGoTo>();

public void sign(){
for(InstructionGoTo instructionGoTo : this.breakList){
instructionGoTo.setMoveTimes(instructionGoTo.getMoveTimes()+1);
}
for(InstructionGoTo instructionGoTo : this.continueList){
instructionGoTo.setMoveTimes(instructionGoTo.getMoveTimes()+1);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public boolean createInstruction(ExpressRunner aCompile,InstructionSet result,
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()));
forStack.peek().sign();
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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class InstructionGoTo extends Instruction{
*/
int offset;
public String name;
int moveTimes;

public InstructionGoTo(int aOffset){
this.offset = aOffset;
Expand Down Expand Up @@ -39,4 +40,11 @@ public void setOffset(int offset) {
this.offset = offset;
}

public int getMoveTimes() {
return moveTimes;
}

public void setMoveTimes(int moveTimes) {
this.moveTimes = moveTimes;
}
}
17 changes: 17 additions & 0 deletions src/test/java/com/ql/util/express/test/ExpressTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.List;

import com.ql.util.express.DefaultContext;
import org.junit.Assert;

import com.ql.util.express.ExpressRunner;
Expand All @@ -19,6 +20,22 @@ public void testDemo() throws Exception{
System.out.println("表达式计算:" + express + " = " + r);
}

@org.junit.Test
public void testDemo1() throws Exception{
String express = "arr = [3,2,1,4,5,1,2];for(i = 0; i < arr.length; i++){boolean flag = false;for(j = 0; j < arr.length-i-1; j++){if(arr[j] > arr[j+1]){temp = arr[j];arr[j] = arr[j+1];arr[j+1] = temp;flag = true;}}if(!flag){break;}}";
ExpressRunner runner = new ExpressRunner();
DefaultContext context = new DefaultContext();
Object r = runner.execute(express,context, null, false,true);
}

@org.junit.Test
public void testDemo2() throws Exception{
String express = "for(i = 0; i < 10; i++){break;}";
ExpressRunner runner = new ExpressRunner();
DefaultContext context = new DefaultContext();
Object r = runner.execute(express,context, null, false,true);
}

@org.junit.Test
public void tes10000次() throws Exception{
ExpressRunner runner = new ExpressRunner();
Expand Down