Skip to content

Commit

Permalink
BL-747 Correctly detect switch vs forindex
Browse files Browse the repository at this point in the history
  • Loading branch information
bdw429s committed Nov 15, 2024
1 parent 0c27701 commit 5bd1fff
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

import ortus.boxlang.compiler.ast.BoxNode;
import ortus.boxlang.compiler.ast.statement.BoxBreak;
import ortus.boxlang.compiler.ast.statement.BoxDo;
import ortus.boxlang.compiler.ast.statement.BoxForIn;
import ortus.boxlang.compiler.ast.statement.BoxForIndex;
import ortus.boxlang.compiler.ast.statement.BoxSwitch;
import ortus.boxlang.compiler.ast.statement.BoxWhile;
import ortus.boxlang.compiler.javaboxpiler.JavaTranspiler;
import ortus.boxlang.compiler.javaboxpiler.transformer.AbstractTransformer;
import ortus.boxlang.compiler.javaboxpiler.transformer.TransformerContext;
Expand All @@ -45,9 +50,11 @@ public Node transform( BoxNode node, TransformerContext context ) throws Illegal
if ( exitsAllowed.equals( ExitsAllowed.COMPONENT ) ) {
template = "if(true) return Component.BodyResult.ofBreak(" + componentLabel + ");";
} else if ( exitsAllowed.equals( ExitsAllowed.LOOP ) ) {
@SuppressWarnings( "unchecked" )
BoxNode specificExit = node.getFirstNodeOfTypes( BoxDo.class, BoxForIndex.class, BoxForIn.class, BoxSwitch.class, BoxWhile.class );
String breakDetectionName = null;
Integer breakCounter = transpiler.peekForLoopBreakCounter();
if ( breakCounter != null ) {
if ( specificExit instanceof BoxForIndex && breakCounter != null ) {
breakDetectionName = "didBreak" + breakCounter;
template = "if(true) { " + breakDetectionName + "=true; break " + breakLabel + "; }";
} else {
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/TestCases/phase1/CoreLangTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,26 @@ public void testSentinelIncrementDoesNotRunAfterBreak() {
assertThat( variables.get( Key.of( "i" ) ) ).isEqualTo( 0 );
}

@DisplayName( "sentinel with switch that uses break" )
@Test
public void testSentinelWithSwitchThatUsesBreak() {

instance.executeSource(
"""
safety = 0;
for( i=0; i<5; i++ ) {
safety++;
assert safety < 100;
switch( "brad" ) {
case "brad":
break;
}
}
""",
context );
assertThat( variables.get( Key.of( "i" ) ) ).isEqualTo( 5 );
}

@DisplayName( "nested sentinel break" )
@Test
public void testNestedSentinelBreak() {
Expand Down

0 comments on commit 5bd1fff

Please sign in to comment.