From e08da659e5f64e3c3172027453ad43d99a421326 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 29 Jan 2023 13:25:15 +0100 Subject: [PATCH] got rid of PtScopeVarsDecls node, just insert variable nodes directly --- codeCore/src/prog8/code/ast/AstBase.kt | 6 ------ .../src/prog8/codegen/intermediate/IRCodeGen.kt | 3 +-- .../compiler/astprocessing/IntermediateAstMaker.kt | 12 +++++------- compiler/test/ast/TestIntermediateAst.kt | 13 ++++++------- 4 files changed, 12 insertions(+), 22 deletions(-) diff --git a/codeCore/src/prog8/code/ast/AstBase.kt b/codeCore/src/prog8/code/ast/AstBase.kt index 5f373646b..0e3726918 100644 --- a/codeCore/src/prog8/code/ast/AstBase.kt +++ b/codeCore/src/prog8/code/ast/AstBase.kt @@ -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 findParentNode(node: PtNode): T? { diff --git a/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt b/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt index fe6005817..2af57ae31 100644 --- a/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt +++ b/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt @@ -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) { diff --git a/compiler/src/prog8/compiler/astprocessing/IntermediateAstMaker.kt b/compiler/src/prog8/compiler/astprocessing/IntermediateAstMaker.kt index 31f8f2cdc..6fc313899 100644 --- a/compiler/src/prog8/compiler/astprocessing/IntermediateAstMaker.kt +++ b/compiler/src/prog8/compiler/astprocessing/IntermediateAstMaker.kt @@ -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, position: Position): PtNode { - val decls = PtScopeVarsDecls(position) + private fun makeScopeVarsDecls(vardecls: Iterable): Iterable { + val decls = mutableListOf() 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)) diff --git a/compiler/test/ast/TestIntermediateAst.kt b/compiler/test/ast/TestIntermediateAst.kt index b928d5681..4b240b5c6 100644 --- a/compiler/test/ast/TestIntermediateAst.kt +++ b/compiler/test/ast/TestIntermediateAst.kt @@ -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()