diff --git a/compiler/src/prog8/ast/AstToplevel.kt b/compiler/src/prog8/ast/AstToplevel.kt index 4bca37ebf..cfeafef6b 100644 --- a/compiler/src/prog8/ast/AstToplevel.kt +++ b/compiler/src/prog8/ast/AstToplevel.kt @@ -187,6 +187,14 @@ interface INameScope { find(this) return result } + + fun nextSibling(stmt: Statement): Statement? { + val nextIdx = statements.indexOfFirst { it===stmt } + 1 + return if(nextIdx < statements.size) + statements[nextIdx] + else + null + } } interface IAssignable { @@ -230,7 +238,7 @@ class Program(val name: String, val modules: MutableList): Node { override fun replaceChildNode(node: Node, replacement: Node) { require(node is Module && replacement is Module) - val idx = modules.withIndex().find { it.value===node }!!.index + val idx = modules.indexOfFirst { it===node } modules[idx] = replacement replacement.parent = this } @@ -257,7 +265,7 @@ class Module(override val name: String, override fun definingScope(): INameScope = program.namespace override fun replaceChildNode(node: Node, replacement: Node) { require(node is Statement && replacement is Statement) - val idx = statements.withIndex().find { it.value===node }!!.index + val idx = statements.indexOfFirst { it===node } statements[idx] = replacement replacement.parent = this } diff --git a/compiler/src/prog8/ast/expressions/AstExpressions.kt b/compiler/src/prog8/ast/expressions/AstExpressions.kt index dc698f48b..e7ed11625 100644 --- a/compiler/src/prog8/ast/expressions/AstExpressions.kt +++ b/compiler/src/prog8/ast/expressions/AstExpressions.kt @@ -527,7 +527,7 @@ class ArrayLiteralValue(val type: InferredTypes.InferredType, // inferred be override fun replaceChildNode(node: Node, replacement: Node) { require(replacement is Expression) - val idx = value.withIndex().find { it.value===node }!!.index + val idx = value.indexOfFirst { it===node } value[idx] = replacement replacement.parent = this } @@ -776,7 +776,7 @@ class FunctionCall(override var target: IdentifierReference, if(node===target) target=replacement as IdentifierReference else { - val idx = args.withIndex().find { it.value===node }!!.index + val idx = args.indexOfFirst { it===node } args[idx] = replacement as Expression } replacement.parent = this diff --git a/compiler/src/prog8/ast/processing/AstWalker.kt b/compiler/src/prog8/ast/processing/AstWalker.kt index c233f4861..c2b02c7fe 100644 --- a/compiler/src/prog8/ast/processing/AstWalker.kt +++ b/compiler/src/prog8/ast/processing/AstWalker.kt @@ -52,7 +52,7 @@ interface IAstModification { class InsertAfter(val after: Statement, val stmt: Statement, val parent: Node) : IAstModification { override fun perform() { if(parent is INameScope) { - val idx = parent.statements.withIndex().find { it.value===after }!!.index + 1 + val idx = parent.statements.indexOfFirst { it===after } + 1 parent.statements.add(idx, stmt) stmt.linkParents(parent) } else { diff --git a/compiler/src/prog8/ast/statements/AstStatements.kt b/compiler/src/prog8/ast/statements/AstStatements.kt index 372ecb0de..c4cbe3cc5 100644 --- a/compiler/src/prog8/ast/statements/AstStatements.kt +++ b/compiler/src/prog8/ast/statements/AstStatements.kt @@ -64,7 +64,7 @@ class Block(override val name: String, override fun replaceChildNode(node: Node, replacement: Node) { require(replacement is Statement) - val idx = statements.withIndex().find { it.value===node }!!.index + val idx = statements.indexOfFirst { it ===node } statements[idx] = replacement replacement.parent = this } @@ -539,7 +539,7 @@ class FunctionCallStatement(override var target: IdentifierReference, if(node===target) target = replacement as IdentifierReference else { - val idx = args.withIndex().find { it.value===node }!!.index + val idx = args.indexOfFirst { it===node } args[idx] = replacement as Expression } replacement.parent = this @@ -586,7 +586,7 @@ class AnonymousScope(override var statements: MutableList, override fun replaceChildNode(node: Node, replacement: Node) { require(replacement is Statement) - val idx = statements.withIndex().find { it.value===node }!!.index + val idx = statements.indexOfFirst { it===node } statements[idx] = replacement replacement.parent = this } @@ -633,7 +633,7 @@ class Subroutine(override val name: String, override fun replaceChildNode(node: Node, replacement: Node) { require(replacement is Statement) - val idx = statements.withIndex().find { it.value===node }!!.index + val idx = statements.indexOfFirst { it===node } statements[idx] = replacement replacement.parent = this } @@ -903,7 +903,7 @@ class StructDecl(override val name: String, override fun replaceChildNode(node: Node, replacement: Node) { require(replacement is Statement) - val idx = statements.withIndex().find { it.value===node }!!.index + val idx = statements.indexOfFirst { it===node } statements[idx] = replacement replacement.parent = this } diff --git a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt index 8d1d6e481..193429fc0 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt @@ -1,5 +1,6 @@ package prog8.compiler.target.c64.codegen +import prog8.ast.INameScope import prog8.ast.Node import prog8.ast.Program import prog8.ast.antlr.escape @@ -836,10 +837,13 @@ internal class AsmGen(private val program: Program, } inits.add(stmt) } else { - val target = AssignTarget(IdentifierReference(listOf(stmt.name), stmt.position), null, null, stmt.position) - val assign = Assignment(target, null, stmt.value!!, stmt.position) - assign.linkParents(stmt.parent) - translate(assign) + val next = (stmt.parent as INameScope).nextSibling(stmt) + if (next !is ForLoop || next.loopVar.nameInSource.single() != stmt.name) { + val target = AssignTarget(IdentifierReference(listOf(stmt.name), stmt.position), null, null, stmt.position) + val assign = Assignment(target, null, stmt.value!!, stmt.position) + assign.linkParents(stmt.parent) + translate(assign) + } } } } diff --git a/examples/fibonacci.p8 b/examples/fibonacci.p8 index b552c5aa9..d0d27306d 100644 --- a/examples/fibonacci.p8 +++ b/examples/fibonacci.p8 @@ -7,8 +7,6 @@ main { sub start() { c64scr.print("fibonacci sequence\n") - ; TODO fix the double i=0 assignment generation in the asm code: - ubyte i for i in 0 to 20 { c64scr.print_uw(fib_next()) diff --git a/examples/test.p8 b/examples/test.p8 index c63cbdc06..dd39efb92 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -8,6 +8,10 @@ main { sub start() { - @($d020)=0 - } + ubyte i + for i in 0 to 20 { + c64scr.print_ub(i) + c64.CHROUT('\n') + } + } }