fix && operator in if expression

This commit is contained in:
Volker Berlin 2019-07-26 23:05:54 +02:00
parent 3ec23fa3c9
commit a019647094
2 changed files with 72 additions and 0 deletions

View File

@ -322,6 +322,28 @@ class BranchManger {
}
}
while( i > 0 ) {
// check if there is a second condition in the IF expression that is concatenated with "&&" operator
parsedBlock = parsedOperations.get( 0 );
if( parsedBlock.op == JavaBlockOperator.IF && parsedBlock.endPosition == elsePos ) {
parent.add( new BranchNode( startPos, parsedBlock.nextPosition - 1, WasmBlockOperator.IF, null ) );
parent.add( new BranchNode( parsedBlock.nextPosition - 1, parsedBlock.nextPosition, WasmBlockOperator.ELSE, WasmBlockOperator.END ) );
startPos = parsedBlock.nextPosition;
parsedOperations.remove( 0 );
i--;
((IfParsedBlock)parsedBlock).negateCompare();
for( int k = 0; k < instructions.size(); k++ ) {
WasmInstruction instr = instructions.get( k );
if( instr.getCodePosition() >= startPos ) {
instructions.add( k, new WasmConstInstruction( 0, startPos - 1, parsedBlock.lineNumber ) );
break;
}
}
continue;
}
break;
}
branch = new BranchNode( startPos, elsePos, WasmBlockOperator.IF, null );
parent.add( branch );
if( i > 0 ) {

View File

@ -56,6 +56,11 @@ public class ControlFlowOperators extends AbstractBaseTest {
addParam( list, script, "forLoop" );
addParam( list, script, "conditionalOperator" );
addParam( list, script, "redifineVariable" );
addParam( list, script, "ifAnd_0" );
addParam( list, script, "ifAnd_3" );
addParam( list, script, "ifAnd_6" );
addParam( list, script, "if4And_6" );
addParam( list, script, "if4And_7" );
}
rule.setTestParameters( list );
return list;
@ -320,5 +325,50 @@ public class ControlFlowOperators extends AbstractBaseTest {
return a + b;
}
}
@Export
static int ifAnd_0() {
return ifAnd( 0 );
}
@Export
static int ifAnd_3() {
return ifAnd( 3 );
}
@Export
static int ifAnd_6() {
return ifAnd( 6 );
}
private static int ifAnd( int condition ) {
int result;
if( condition > 0 && condition < 5 ) {
result = 42;
} else {
result = 76;
}
return result;
}
@Export
static int if4And_6() {
return if4And( 6 );
}
@Export
static int if4And_7() {
return if4And( 7 );
}
private static int if4And( int condition ) {
int result;
if( condition > 1 && condition > 3 && condition > 5 && condition > 7 ) {
result = 42;
} else {
result = 76;
}
return result;
}
}
}