diff --git a/codeOptimizers/src/prog8/optimizer/StatementOptimizer.kt b/codeOptimizers/src/prog8/optimizer/StatementOptimizer.kt index d0f6a6971..197a6d596 100644 --- a/codeOptimizers/src/prog8/optimizer/StatementOptimizer.kt +++ b/codeOptimizers/src/prog8/optimizer/StatementOptimizer.kt @@ -71,6 +71,11 @@ class StatementOptimizer(private val program: Program, } override fun after(ifElse: IfElse, parent: Node): Iterable { + 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)) } } diff --git a/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt b/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt index a90edf96b..4371bc77e 100644 --- a/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt +++ b/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt @@ -110,6 +110,22 @@ if CONDITION==0 } override fun after(whileLoop: WhileLoop, parent: Node): Iterable { + /* + 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 } ==> diff --git a/docs/source/todo.rst b/docs/source/todo.rst index f3af7cef9..4685774f0 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -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()