mirror of
https://github.com/irmen/prog8.git
synced 2026-04-25 12:33:55 +00:00
simplify when into if-else, if possible
This commit is contained in:
+1
-1
@@ -18,7 +18,7 @@ allprojects {
|
||||
|
||||
kotlin {
|
||||
compilerOptions {
|
||||
freeCompilerArgs = listOf("-Xwhen-guards")
|
||||
freeCompilerArgs = listOf()
|
||||
jvmTarget = JvmTarget.JVM_11
|
||||
jvmDefault = JvmDefaultMode.NO_COMPATIBILITY
|
||||
// languageVersion.set(KotlinVersion.KOTLIN_2_3)
|
||||
|
||||
+9
-8
@@ -2,14 +2,15 @@
|
||||
%zeropage basicsafe
|
||||
|
||||
main {
|
||||
sub start() {
|
||||
const long value1, value2, value3 = 255
|
||||
const ubyte valueb = 255
|
||||
sub start() {
|
||||
when cx16.r0L {
|
||||
12 -> cx16.r0++
|
||||
else -> cx16.r0--
|
||||
}
|
||||
|
||||
txt.print_l(valueb+1)
|
||||
txt.nl()
|
||||
txt.print_l(value1+1)
|
||||
txt.nl()
|
||||
cx16.r0L = value2
|
||||
if cx16.r0L==12
|
||||
cx16.r0++
|
||||
else
|
||||
cx16.r0--
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ fun optimizeSimplifiedAst(program: PtProgram, options: CompilationOptions, st: S
|
||||
optimizeAssignTargets(program, st)
|
||||
+ optimizeFloatComparesToZero(program)
|
||||
+ optimizeLsbMsbOnStructfields(program)
|
||||
+ optimizeSingleWhens(program, errors)
|
||||
+ optimizeBinaryExpressions(program, options) > 0) {
|
||||
// keep rolling
|
||||
}
|
||||
@@ -210,5 +211,46 @@ private fun optimizeLsbMsbOnStructfields(program: PtProgram): Int {
|
||||
true
|
||||
}
|
||||
|
||||
return changes
|
||||
}
|
||||
|
||||
|
||||
private fun optimizeSingleWhens(program: PtProgram, errors: IErrorReporter): Int {
|
||||
var changes = 0
|
||||
|
||||
walkAst(program) { node: PtNode, depth: Int ->
|
||||
if(node is PtWhen && node.choices.children.size==2) {
|
||||
val choice1 = node.choices.children[0] as PtWhenChoice
|
||||
val choice2 = node.choices.children[1] as PtWhenChoice
|
||||
if(choice1.isElse && choice2.values.children.size==1 || choice2.isElse && choice1.values.children.size==1) {
|
||||
errors.info("when can be simplified into an if-else", node.position)
|
||||
val truescope: PtNodeGroup
|
||||
val elsescope: PtNodeGroup
|
||||
val comparisonValue : PtNumber
|
||||
if(choice1.isElse) {
|
||||
truescope = choice2.statements
|
||||
elsescope = choice1.statements
|
||||
comparisonValue = choice2.values.children.single() as PtNumber
|
||||
} else {
|
||||
truescope = choice1.statements
|
||||
elsescope = choice2.statements
|
||||
comparisonValue = choice1.values.children.single() as PtNumber
|
||||
}
|
||||
val ifelse = PtIfElse(node.position)
|
||||
val condition = PtBinaryExpression("==", DataType.BOOL, node.position)
|
||||
condition.add(node.value)
|
||||
condition.add(comparisonValue)
|
||||
ifelse.add(condition)
|
||||
ifelse.add(truescope)
|
||||
ifelse.add(elsescope)
|
||||
ifelse.parent = node.parent
|
||||
val index = node.parent.children.indexOf(node)
|
||||
node.parent.children[index] = ifelse
|
||||
changes++
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
return changes
|
||||
}
|
||||
Reference in New Issue
Block a user