diff --git a/codeOptimizers/src/prog8/optimizer/StatementOptimizer.kt b/codeOptimizers/src/prog8/optimizer/StatementOptimizer.kt index 0af583854..17e00b464 100644 --- a/codeOptimizers/src/prog8/optimizer/StatementOptimizer.kt +++ b/codeOptimizers/src/prog8/optimizer/StatementOptimizer.kt @@ -430,6 +430,27 @@ class StatementOptimizer(private val program: Program, } } } + + val constantValue = whenStmt.condition.constValue(program)?.number + if(constantValue!=null) { + // when condition is a constant + var matchingChoice: WhenChoice? = null + loop@ for(choice in whenStmt.choices) { + for(value in choice.values ?: emptyList()) { + if(value.constValue(program)?.number == constantValue) { + matchingChoice = choice + break@loop + } + } + } + if(matchingChoice==null) + matchingChoice = whenStmt.choices.singleOrNull { it.values==null } + if(matchingChoice!=null) { + // get rid of the whole when-statement and just leave the matching choice + return listOf(IAstModification.ReplaceNode(whenStmt, matchingChoice.statements, parent)) + } + } + return noModifications } diff --git a/examples/test.p8 b/examples/test.p8 index f60fec790..c35ff1d9b 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,27 +1,17 @@ -%import gfx2 +%zeropage basicsafe +%option no_sysinit +%import textio main { - sub start() { - gfx2.screen_mode(2) + const ubyte FOO = 0 + const ubyte BAR = 1 - gfx2.safe_circle(120, 140, 200, 1) - sys.wait(30) - gfx2.safe_circle(520, 140, 200, 1) - sys.wait(30) - gfx2.safe_circle(120, 340, 200, 1) - sys.wait(30) - gfx2.safe_circle(520, 340, 200, 1) - sys.wait(30) - - gfx2.safe_disc(120, 140, 200, 1) - sys.wait(30) - gfx2.safe_disc(520, 140, 200, 1) - sys.wait(30) - gfx2.safe_disc(120, 340, 200, 1) - sys.wait(30) - gfx2.safe_disc(520, 340, 200, 1) - - repeat { - } + sub start() { + when FOO+BAR { + 1-> txt.print("path 1") + 2-> txt.print("path 2") + else-> txt.print("path 3") } + txt.nl() + } }