mirror of
https://github.com/irmen/prog8.git
synced 2025-04-04 11:32:21 +00:00
use indexOfFirst. Also avoid initializing a for loop variable twice in a row.
This commit is contained in:
parent
0c461ffe2e
commit
a70276c190
compiler/src/prog8
ast
compiler/target/c64/codegen
examples
@ -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<Module>): 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
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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 {
|
||||
|
@ -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<Statement>,
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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())
|
||||
|
@ -8,6 +8,10 @@
|
||||
main {
|
||||
|
||||
sub start() {
|
||||
@($d020)=0
|
||||
}
|
||||
ubyte i
|
||||
for i in 0 to 20 {
|
||||
c64scr.print_ub(i)
|
||||
c64.CHROUT('\n')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user