From c78d1e3c39c1a5ef70d5323be08bd67cb9d42547 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 27 Jan 2023 01:51:21 +0100 Subject: [PATCH] implemented Pt findTarget and siblings --- .../src/prog8/codegen/cpu6502/Extensions.kt | 60 ++++++++++++++++--- .../prog8/codegen/cpu6502/ForLoopsAsmGen.kt | 4 +- .../cpu6502/assignment/AsmAssignment.kt | 4 +- 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/Extensions.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/Extensions.kt index eb99c74cf..1067a6220 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/Extensions.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/Extensions.kt @@ -58,13 +58,49 @@ fun PtExpression.isSimple(): Boolean { } } -internal fun PtIdentifier.targetStatement(program: PtProgram): PtNode? { - TODO("Not yet implemented target stmt for ${this.name}") -// if(nameInSource.size==1 && nameInSource[0] in program.builtinFunctions.names) -// BuiltinFunctionPlaceholder(nameInSource[0], position, parent) -// else -// definingScope.lookup(nameInSource) -// +internal fun PtIdentifier.targetStatement(program: PtProgram): PtNode { + return if(name in BuiltinFunctions) + this // just reuse the node itself to refer to the builtin function + else + program.lookup(name) +} + +internal fun PtProgram.lookup(name: String): PtNode { + val remainder = name.split('.').toMutableList() + + fun recurse(node: PtNode): PtNode { + when(node) { + is PtProgram -> { + val blockName = remainder.removeFirst() + return recurse(allBlocks().single { it.name==blockName }) + } + is PtAsmSub -> { + require(remainder.isEmpty()) + return node + } + is PtSub -> { + if(remainder.isEmpty()) + return node + if(remainder.size==1) { + // look to see if there is a block of vardecls + val scopevars = node.children.firstOrNull() as? PtScopeVarsDecls + if(scopevars!=null) + return recurse(scopevars) + } + val childName = remainder.removeFirst() + return recurse(node.children.filterIsInstance().single { it.name==childName}) + } + is PtBlock, is PtScopeVarsDecls, is PtNamedNode -> { + if(remainder.isEmpty()) + return node + val childName = remainder.removeFirst() + return recurse(node.children.filterIsInstance().single { it.name==childName}) + } + else -> throw IllegalArgumentException("invalid name $name in parent $node") + } + } + + return recurse(this) } internal fun PtIdentifier.targetVarDecl(program: PtProgram): PtVariable? = @@ -90,8 +126,14 @@ internal fun PtAsmSub.shouldKeepA(): KeepAresult { return KeepAresult(false, saveAonReturn) } -internal fun PtFunctionCall.targetSubroutine(program: PtProgram): IPtSubroutine? { - TODO() +internal fun PtFunctionCall.targetSubroutine(program: PtProgram): IPtSubroutine? = + this.targetStatement(program) as? IPtSubroutine + +internal fun PtFunctionCall.targetStatement(program: PtProgram): PtNode { + return if(name in BuiltinFunctions) + this // just reuse the node itself to refer to the builtin function + else + program.lookup(name) } internal fun IPtSubroutine.returnsWhatWhere(): List> { diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/ForLoopsAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/ForLoopsAsmGen.kt index 94a85bf87..b62d09cf0 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/ForLoopsAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/ForLoopsAsmGen.kt @@ -8,7 +8,9 @@ import prog8.code.ast.PtRange import prog8.code.core.* import kotlin.math.absoluteValue -internal class ForLoopsAsmGen(private val program: PtProgram, private val asmgen: AsmGen, private val zeropage: Zeropage) { +internal class ForLoopsAsmGen(private val program: PtProgram, + private val asmgen: AsmGen, + private val zeropage: Zeropage) { internal fun translate(stmt: PtForLoop) { val iterableDt = stmt.iterable.type diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AsmAssignment.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AsmAssignment.kt index fd913d0d3..93a278a82 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AsmAssignment.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AsmAssignment.kt @@ -135,7 +135,9 @@ internal class AsmAssignSource(val kind: SourceStorageKind, is PtString -> throw AssemblyError("string literal value should not occur anymore for asm generation") is PtArray -> throw AssemblyError("array literal value should not occur anymore for asm generation") is PtIdentifier -> { - val parameter: PtSubroutineParameter = TODO("search subroutine parameter that it may refer to ${value.name}") // value.targetVarDecl(program)?.subroutineParameter + val paramName = value.targetVarDecl(program)?.name + val parameter = value.targetStatement(program)?.definingSub()?.parameters?.singleOrNull { it.name===paramName } + println("assign to ${value.name} param=$parameter") // TODO WEG if(parameter?.definingAsmSub() != null) throw AssemblyError("can't assign from a asmsub register parameter $value ${value.position}") val varName=asmgen.asmVariableName(value)