fix a while loop in ELSE after an return.

This commit is contained in:
Volker Berlin 2022-01-30 14:34:39 +01:00
parent cbcf9fdfbc
commit 71d2b91895
2 changed files with 24 additions and 0 deletions

View File

@ -185,6 +185,14 @@ class BranchManager {
if( loop == null ) {
loop = new ParsedBlock( JavaBlockOperator.LOOP, start, 0, start, parsedBlock.lineNumber );
loops.put( start, loop );
// if a condition form outside of the loop point inside the loop then it must be conditional return and a jump to the loop condition.
for( int n = b - 1; n >= 0; n-- ) {
ParsedBlock prevBlock = parsedOperations.get( n );
if( prevBlock.op == JavaBlockOperator.IF && prevBlock.startPosition < start && prevBlock.endPosition > start
&& prevBlock.endPosition < parsedBlock.startPosition ) {
prevBlock.endPosition = start;
}
}
}
loop.nextPosition = parsedBlock.startPosition; // Jump position for Continue
loop.endPosition = parsedBlock.nextPosition;

View File

@ -53,6 +53,7 @@ public class ControlFlowOperators extends AbstractBaseTest {
addParam( list, script, "whileLoopWithContinue" );
addParam( list, script, "whileLoopInElse_3" );
addParam( list, script, "whileLoopInElse_13" );
addParam( list, script, "whileLoopInElseAfterReturn" );
addParam( list, script, "forLoop" );
addParam( list, script, "conditionalOperator" );
addParam( list, script, "conditionalOperator2" );
@ -376,6 +377,21 @@ public class ControlFlowOperators extends AbstractBaseTest {
return whileLoopInElse( 13 );
}
@Export
public static int whileLoopInElseAfterReturn() {
int yIndex = 13;
int result = 0;
if (yIndex == 3) {
return 42;
} else {
while (yIndex > 7) {
result++;
yIndex--;
}
}
return result;
}
@Export
static int forLoop() {
int a = 0;