got rid of PtScopeVarsDecls node, just insert variable nodes directly

This commit is contained in:
Irmen de Jong 2023-01-29 13:25:15 +01:00
parent e9ec310d8a
commit e08da659e5
4 changed files with 12 additions and 22 deletions

View File

@ -134,12 +134,6 @@ class PtNop(position: Position): PtNode(position) {
}
class PtScopeVarsDecls(position: Position): PtNode(position) {
override fun printProperties() {}
}
// find the parent node of a specific type or interface
// (useful to figure out in what namespace/block something is defined, etc.)
inline fun <reified T> findParentNode(node: PtNode): T? {

View File

@ -234,7 +234,6 @@ class IRCodeGen(
internal fun translateNode(node: PtNode): IRCodeChunks {
val chunks = when(node) {
is PtScopeVarsDecls -> emptyList() // vars should be looked up via symbol table
is PtVariable -> emptyList() // var should be looked up via symbol table
is PtMemMapped -> emptyList() // memmapped var should be looked up via symbol table
is PtConstant -> emptyList() // constants have all been folded into the code
@ -1180,7 +1179,7 @@ class IRCodeGen(
when(child) {
is PtNop -> { /* nothing */ }
is PtAssignment -> { /* global variable initialization is done elsewhere */ }
is PtScopeVarsDecls -> { /* vars should be looked up via symbol table */ }
is PtVariable, is PtConstant, is PtMemMapped -> { /* vars should be looked up via symbol table */ }
is PtSub -> {
val sub = IRSubroutine(child.name, translate(child.parameters), child.returntype, child.position)
for (subchild in child.children) {

View File

@ -12,7 +12,6 @@ import prog8.code.SymbolTable
import prog8.code.ast.*
import prog8.code.core.CompilationOptions
import prog8.code.core.DataType
import prog8.code.core.Position
import prog8.code.core.SourceCode
import prog8.compiler.BuiltinFunctions
import prog8.compiler.builtinFunctionReturnType
@ -132,16 +131,16 @@ class IntermediateAstMaker(private val program: Program, private val symbolTable
}
val (vardecls, statements) = srcBlock.statements.partition { it is VarDecl }
val block = PtBlock(srcBlock.name, srcBlock.address, srcBlock.isInLibrary, forceOutput, alignment, srcBlock.position)
if(vardecls.isNotEmpty()) block.add(makeScopeVarsDecls(vardecls, srcBlock.position))
makeScopeVarsDecls(vardecls).forEach { block.add(it) }
for (stmt in statements)
block.add(transformStatement(stmt))
return block
}
private fun makeScopeVarsDecls(vardecls: List<Statement>, position: Position): PtNode {
val decls = PtScopeVarsDecls(position)
private fun makeScopeVarsDecls(vardecls: Iterable<Statement>): Iterable<PtNamedNode> {
val decls = mutableListOf<PtNamedNode>()
vardecls.forEach {
decls.add(transformStatement(it as VarDecl))
decls.add(transformStatement(it as VarDecl) as PtNamedNode)
}
return decls
}
@ -336,8 +335,7 @@ class IntermediateAstMaker(private val program: Program, private val symbolTable
srcSub.inline,
srcSub.position)
sub.parameters.forEach { it.parent=sub }
if(vardecls.isNotEmpty()) sub.add(makeScopeVarsDecls(vardecls, sub.position))
makeScopeVarsDecls(vardecls).forEach { sub.add(it) }
for (statement in statements)
sub.add(transformStatement(statement))

View File

@ -43,7 +43,7 @@ class TestIntermediateAst: FunSpec({
ast.name shouldBe result.program.name
ast.allBlocks().any() shouldBe true
val entry = ast.entrypoint() ?: fail("no main.start() found")
entry.children.size shouldBe 4
entry.children.size shouldBe 5
entry.name shouldBe "start"
entry.scopedName shouldBe "main.start"
val blocks = ast.allBlocks().toList()
@ -51,24 +51,23 @@ class TestIntermediateAst: FunSpec({
blocks[0].name shouldBe "main"
blocks[0].scopedName shouldBe "main"
val vars = entry.children[0] as PtScopeVarsDecls
val ccInit = entry.children[1] as PtAssignment
val ccInit = entry.children[2] as PtAssignment
ccInit.target.identifier?.name shouldBe "main.start.cc"
(ccInit.value as PtNumber).number shouldBe 0.0
val ccdecl = vars.children[0] as PtVariable
val ccdecl = entry.children[0] as PtVariable
ccdecl.name shouldBe "cc"
ccdecl.scopedName shouldBe "main.start.cc"
ccdecl.type shouldBe DataType.UBYTE
val arraydecl = vars.children[1] as PtVariable
val arraydecl = entry.children[1] as PtVariable
arraydecl.name shouldBe "array"
arraydecl.type shouldBe DataType.ARRAY_UB
val containmentCast = (entry.children[2] as PtAssignment).value as PtTypeCast
val containmentCast = (entry.children[3] as PtAssignment).value as PtTypeCast
containmentCast.type shouldBe DataType.UBYTE
val containment = containmentCast.value as PtContainmentCheck
(containment.element as PtNumber).number shouldBe 11.0
val fcall = (entry.children[3] as PtAssignment).value as PtFunctionCall
val fcall = (entry.children[4] as PtAssignment).value as PtFunctionCall
fcall.void shouldBe false
fcall.type shouldBe DataType.UBYTE
ast.print()