mirror of
https://github.com/irmen/prog8.git
synced 2024-09-10 09:54:32 +00:00
optimize when with const value (remove other choices from code)
This commit is contained in:
parent
723ab54f97
commit
efe4df92dc
@ -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
|
return noModifications
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,27 +1,17 @@
|
|||||||
%import gfx2
|
%zeropage basicsafe
|
||||||
|
%option no_sysinit
|
||||||
|
%import textio
|
||||||
|
|
||||||
main {
|
main {
|
||||||
sub start() {
|
const ubyte FOO = 0
|
||||||
gfx2.screen_mode(2)
|
const ubyte BAR = 1
|
||||||
|
|
||||||
gfx2.safe_circle(120, 140, 200, 1)
|
sub start() {
|
||||||
sys.wait(30)
|
when FOO+BAR {
|
||||||
gfx2.safe_circle(520, 140, 200, 1)
|
1-> txt.print("path 1")
|
||||||
sys.wait(30)
|
2-> txt.print("path 2")
|
||||||
gfx2.safe_circle(120, 340, 200, 1)
|
else-> txt.print("path 3")
|
||||||
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 {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
txt.nl()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user