fix weird compiler warning for while 1 {..}

This commit is contained in:
Irmen de Jong 2023-12-02 20:24:45 +01:00
parent 1509de390e
commit db52a9466c
3 changed files with 24 additions and 5 deletions

View File

@ -71,6 +71,11 @@ class StatementOptimizer(private val program: Program,
}
override fun after(ifElse: IfElse, parent: Node): Iterable<IAstModification> {
val constvalue = ifElse.condition.constValue(program)
if(constvalue!=null) {
errors.warn("condition is always ${constvalue.asBooleanValue}", ifElse.condition.position)
}
// remove empty if statements
if(ifElse.truepart.isEmpty() && ifElse.elsepart.isEmpty())
return listOf(IAstModification.Remove(ifElse, parent as IStatementContainer))
@ -87,17 +92,12 @@ class StatementOptimizer(private val program: Program,
)
}
val constvalue = ifElse.condition.constValue(program)
if(constvalue!=null) {
return if(constvalue.asBooleanValue){
// always true -> keep only if-part
if(!ifElse.definingModule.isLibrary)
errors.warn("condition is always true", ifElse.condition.position)
listOf(IAstModification.ReplaceNode(ifElse, ifElse.truepart, parent))
} else {
// always false -> keep only else-part
if(!ifElse.definingModule.isLibrary)
errors.warn("condition is always false", ifElse.condition.position)
listOf(IAstModification.ReplaceNode(ifElse, ifElse.elsepart, parent))
}
}

View File

@ -110,6 +110,22 @@ if CONDITION==0
}
override fun after(whileLoop: WhileLoop, parent: Node): Iterable<IAstModification> {
/*
while true -> repeat
while false -> discard
*/
val constCondition = whileLoop.condition.constValue(program)?.asBooleanValue
if(constCondition==true) {
errors.warn("condition is always true", whileLoop.condition.position)
val repeat = RepeatLoop(null, whileLoop.body, whileLoop.position)
return listOf(IAstModification.ReplaceNode(whileLoop, repeat, parent))
} else if(constCondition==false) {
errors.warn("condition is always false", whileLoop.condition.position)
return listOf(IAstModification.Remove(whileLoop, parent as IStatementContainer))
}
/*
while CONDITION { STUFF }
==>

View File

@ -2,6 +2,9 @@
TODO
====
- get rid of jvmTarget in gradle scripts?
- make sizeof() works with an asmbinary label
- should string encodings not be restricted by the compiler target? (at least, ISO should be everywhere?)
- uword scanline_buf = memory("scanline", 320, 0) different result when inside a sub or outside a sub??! (imageviewer iff module)
- [on branch: shortcircuit] investigate McCarthy evaluation again? this may also reduce code size perhaps for things like if a>4 or a<2 ....
- once VAL_1 is merged into the kernal properly, remove all the workarounds in cx16 floats.parse_f()