fix bug with cascaded if blocks

This commit is contained in:
Volker Berlin 2020-05-17 13:57:30 +02:00
parent 55ddeac911
commit f383ef26e8
2 changed files with 31 additions and 4 deletions

View File

@ -433,10 +433,21 @@ class BranchManger {
// first search for first GOTO, any IF that point after the GOTO can not be part of the primary IF condition.
// This occur with a second IF inside of the THEN. This can jump directly to the end of the ELSE.
int maxElse = Integer.MAX_VALUE;
for( int i = 0; i < parsedOperations.size(); i++ ) {
int parsedOpCount = parsedOperations.size();
for( int i = 0; i < parsedOpCount; i++ ) {
ParsedBlock parsedBlock = parsedOperations.get( i );
if( parsedBlock.op == JavaBlockOperator.GOTO ) {
maxElse = parsedBlock.nextPosition;
maxElse = parsedBlock.endPosition;
// find the last IF that point to this GOTO
int gotoNext = parsedBlock.nextPosition;
for( ; i > 0; i-- ) {
parsedBlock = parsedOperations.get( i-1 );
if( parsedBlock.endPosition == gotoNext ) {
break;
}
}
parsedOpCount = i;
break;
}
}
@ -444,7 +455,7 @@ class BranchManger {
int ifCount = 0;
int thenPos = startBlock.nextPosition;
int elsePos = startBlock.endPosition;
for( ; ifCount < parsedOperations.size(); ifCount++ ) {
for( ; ifCount < parsedOpCount; ifCount++ ) {
ParsedBlock parsedBlock = parsedOperations.get( ifCount );
if( parsedBlock.op != JavaBlockOperator.IF || parsedBlock.endPosition < elsePos || parsedBlock.endPosition > maxElse ) {
// seems a second IF inside the THEN part.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2018 - 2019 Volker Berlin (i-net software)
* Copyright 2018 - 2020 Volker Berlin (i-net software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -73,6 +73,7 @@ public class ControlFlowOperators extends AbstractBaseTest {
addParam( list, script, "ifAndOr6" );
addParam( list, script, "ifAndOr8" );
addParam( list, script, "ifWithoutElseAndLoop" );
addParam( list, script, "ifOrWithMulti" );
addParam( list, script, "stringSwitchNormalFoo" );
addParam( list, script, "stringSwitchNormalBar" );
addParam( list, script, "stringSwitchNormalDefault" );
@ -495,6 +496,21 @@ public class ControlFlowOperators extends AbstractBaseTest {
return ifWithoutElseAndLoop;
}
@Export
static int ifOrWithMulti() {
int len = 4;
// the GOTO before the ELSE is not related to the main IF condition
if( (len == 4 || len == 9) ) {
if( len == 9 ) {
len = 13;
} else {
len = 42;
}
}
return len;
}
@Export
static int stringSwitchNormalFoo() {
return stringSwitchNormal( "foo" );