implemented Pt findTarget and siblings

This commit is contained in:
Irmen de Jong 2023-01-27 01:51:21 +01:00
parent e94319145f
commit c78d1e3c39
3 changed files with 57 additions and 11 deletions

View File

@ -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<PtNamedNode>().single { it.name==childName})
}
is PtBlock, is PtScopeVarsDecls, is PtNamedNode -> {
if(remainder.isEmpty())
return node
val childName = remainder.removeFirst()
return recurse(node.children.filterIsInstance<PtNamedNode>().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<Pair<DataType, RegisterOrStatusflag>> {

View File

@ -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

View File

@ -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)