diff --git a/compiler/src/prog8/optimizer/ConstantFolding.kt b/compiler/src/prog8/optimizer/ConstantFolding.kt index 8466abea5..7a27c354f 100644 --- a/compiler/src/prog8/optimizer/ConstantFolding.kt +++ b/compiler/src/prog8/optimizer/ConstantFolding.kt @@ -167,6 +167,15 @@ class ConstantFolding(private val program: Program) : IAstModifyingVisitor { * replace identifiers that refer to const value, with the value itself (if it's a simple type) */ override fun visit(identifier: IdentifierReference): Expression { + // don't replace when it's an assignment target or loop variable + if(identifier.parent is AssignTarget) + return identifier + var forloop = identifier.parent as? ForLoop + if(forloop==null) + forloop = identifier.parent.parent as? ForLoop + if(forloop!=null && identifier===forloop.loopVar) + return identifier + return try { val cval = identifier.constValue(program) ?: return identifier return when { diff --git a/examples/tehtriz.p8 b/examples/tehtriz.p8 index 11bfb039b..5d32de576 100644 --- a/examples/tehtriz.p8 +++ b/examples/tehtriz.p8 @@ -9,9 +9,6 @@ ; ; @todo show ghost? - -; TODO FIX COMPILATION ERROR can't change class of loopvar - main { const ubyte boardOffsetX = 14 @@ -354,8 +351,8 @@ waitkey: sub drawNextBlock() { const ubyte nextBlockXpos = 29 const ubyte nextBlockYpos = 5 - const ubyte x = 33 - for x in nextBlockXpos+3 to nextBlockXpos step -1 { ; TODO error because const + ubyte x + for x in nextBlockXpos+3 to nextBlockXpos step -1 { c64scr.setcc(x, nextBlockYpos, ' ', 0) c64scr.setcc(x, nextBlockYpos+1, ' ', 0) } diff --git a/examples/test.p8 b/examples/test.p8 index 59ec3b405..dcf6e7a61 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -6,19 +6,18 @@ main { sub start() { - ubyte i + const ubyte i = 33 - for i in [1,3,5,99] { - c64scr.print_ub(i) - c64.CHROUT(',') + i=33 + i++ + const ubyte q=33 + for q in [1,3,5,99] { + A=i } - c64.CHROUT('\n') - for A in [1,3,5,99] { - c64scr.print_ub(A) - c64.CHROUT(',') + while q<10 { + q++ } - c64.CHROUT('\n') } } diff --git a/examples/testarrays.p8 b/examples/testarrays.p8 index 1cf1afa60..dfb353ff2 100644 --- a/examples/testarrays.p8 +++ b/examples/testarrays.p8 @@ -2,10 +2,6 @@ %zeropage basicsafe %option enable_floats -; TODO complete asm code generation for all lines in this - - - main { sub start() { @@ -34,8 +30,6 @@ main { float fl ; read array - @($d020) = ub - A=s1[2] ub=s1[2] ub=s2[2] diff --git a/examples/testforloops.p8 b/examples/testforloops.p8 index a7ba3a9d4..f407838b6 100644 --- a/examples/testforloops.p8 +++ b/examples/testforloops.p8 @@ -17,7 +17,7 @@ main { } c64.CHROUT('\n') - for A in [1,3,5,99] { ; TODO FIX COMPILER ERROR array should have been moved to the heap + for A in [1,3,5,99] { c64scr.print_ub(A) c64.CHROUT(',') }