From ddf96943f0aa8581c40fc5efcba7a6e2426b1f88 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Mon, 31 Jan 2022 22:36:10 +0100 Subject: [PATCH] remove Nop ast node. --- .../src/prog8/codegen/cpu6502/AsmGen.kt | 5 ++--- .../compiler/astprocessing/AstChecker.kt | 3 +-- .../astprocessing/BeforeAsmAstChanger.kt | 2 +- .../astprocessing/ParentNodeChecker.kt | 8 +------ .../compiler/astprocessing/VariousCleanups.kt | 4 ---- .../src/prog8/ast/AstToSourceTextConverter.kt | 4 +--- compilerAst/src/prog8/ast/AstToplevel.kt | 2 +- .../src/prog8/ast/antlr/Antlr2Kotlin.kt | 4 ++-- .../src/prog8/ast/statements/AstStatements.kt | 21 ++++--------------- compilerAst/src/prog8/ast/walk/AstWalker.kt | 13 +++--------- compilerAst/src/prog8/ast/walk/IAstVisitor.kt | 5 +---- 11 files changed, 17 insertions(+), 54 deletions(-) diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt index cf4f21fb1..d85bca51a 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt @@ -837,7 +837,6 @@ class AsmGen(private val program: Program, outputSourceLine(stmt) when(stmt) { is VarDecl -> translate(stmt) - is Nop -> {} is Directive -> translate(stmt) is Return -> translate(stmt) is Subroutine -> translateSubroutine(stmt) @@ -859,7 +858,7 @@ class AsmGen(private val program: Program, is GoSub -> translate(stmt) is PostIncrDecr -> postincrdecrAsmGen.translate(stmt) is Label -> translate(stmt) - is Branch -> translate(stmt) + is ConditionalBranch -> translate(stmt) is IfElse -> translate(stmt) is ForLoop -> forloopsAsmGen.translate(stmt) is RepeatLoop -> translate(stmt) @@ -1536,7 +1535,7 @@ $repeatLabel lda $counterVar scope.statements.forEach{ translate(it) } } - private fun translate(stmt: Branch) { + private fun translate(stmt: ConditionalBranch) { if(stmt.truepart.isEmpty() && stmt.elsepart.isNotEmpty()) throw AssemblyError("only else part contains code, shoud have been switched already") diff --git a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt index a06a387f8..d6edcaae0 100644 --- a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt +++ b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt @@ -205,8 +205,7 @@ internal class AstChecker(private val program: Program, is Label, is VarDecl, is InlineAssembly, - is IStatementContainer, - is Nop -> true + is IStatementContainer -> true is Assignment -> { val target = statement.target.identifier!!.targetStatement(program) target === statement.previousSibling() // an initializer assignment is okay diff --git a/compiler/src/prog8/compiler/astprocessing/BeforeAsmAstChanger.kt b/compiler/src/prog8/compiler/astprocessing/BeforeAsmAstChanger.kt index fed8a6cec..2e4f64360 100644 --- a/compiler/src/prog8/compiler/astprocessing/BeforeAsmAstChanger.kt +++ b/compiler/src/prog8/compiler/astprocessing/BeforeAsmAstChanger.kt @@ -324,7 +324,7 @@ internal class BeforeAsmAstChanger(val program: Program, private val options: Co complexArrayIndexedExpressions.add(arrayIndexedExpression) } - override fun visit(branch: Branch) {} + override fun visit(branch: ConditionalBranch) {} override fun visit(forLoop: ForLoop) {} diff --git a/compiler/src/prog8/compiler/astprocessing/ParentNodeChecker.kt b/compiler/src/prog8/compiler/astprocessing/ParentNodeChecker.kt index 2cea34ec0..6b2aefebb 100644 --- a/compiler/src/prog8/compiler/astprocessing/ParentNodeChecker.kt +++ b/compiler/src/prog8/compiler/astprocessing/ParentNodeChecker.kt @@ -47,7 +47,7 @@ internal class ParentNodeChecker: AstWalker() { return noModifications } - override fun before(branch: Branch, parent: Node): Iterable { + override fun before(branch: ConditionalBranch, parent: Node): Iterable { if(branch.parent!==parent) throw FatalAstException("parent node mismatch at $branch") return noModifications @@ -233,12 +233,6 @@ internal class ParentNodeChecker: AstWalker() { return noModifications } - override fun before(nop: Nop, parent: Node): Iterable { - if(nop.parent!==parent) - throw FatalAstException("parent node mismatch at $nop") - return noModifications - } - override fun before(scope: AnonymousScope, parent: Node): Iterable { if(scope.parent!==parent) throw FatalAstException("parent node mismatch at $scope") diff --git a/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt b/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt index 5c6a202eb..94f3d9bd1 100644 --- a/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt +++ b/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt @@ -17,10 +17,6 @@ import prog8.compilerinterface.IErrorReporter internal class VariousCleanups(val program: Program, val errors: IErrorReporter, val options: CompilationOptions): AstWalker() { - override fun before(nop: Nop, parent: Node): Iterable { - return listOf(IAstModification.Remove(nop, parent as IStatementContainer)) - } - override fun after(scope: AnonymousScope, parent: Node): Iterable { return if(parent is IStatementContainer) listOf(ScopeFlatten(scope, parent as IStatementContainer)) diff --git a/compilerAst/src/prog8/ast/AstToSourceTextConverter.kt b/compilerAst/src/prog8/ast/AstToSourceTextConverter.kt index 8259f3c60..3c887c50a 100644 --- a/compilerAst/src/prog8/ast/AstToSourceTextConverter.kt +++ b/compilerAst/src/prog8/ast/AstToSourceTextConverter.kt @@ -2,13 +2,11 @@ package prog8.ast import prog8.ast.antlr.escape import prog8.ast.base.DataType -import prog8.ast.base.FatalAstException import prog8.ast.base.NumericDatatypes import prog8.ast.base.VarDeclType import prog8.ast.expressions.* import prog8.ast.statements.* import prog8.ast.walk.IAstVisitor -import prog8.compilerinterface.Encoding /** @@ -259,7 +257,7 @@ class AstToSourceTextConverter(val output: (text: String) -> Unit, val program: } } - override fun visit(branch: Branch) { + override fun visit(branch: ConditionalBranch) { output("if_${branch.condition.toString().lowercase()} ") branch.truepart.accept(this) if(branch.elsepart.statements.isNotEmpty()) { diff --git a/compilerAst/src/prog8/ast/AstToplevel.kt b/compilerAst/src/prog8/ast/AstToplevel.kt index 26f355adc..873bf133f 100644 --- a/compilerAst/src/prog8/ast/AstToplevel.kt +++ b/compilerAst/src/prog8/ast/AstToplevel.kt @@ -92,7 +92,7 @@ interface IStatementContainer { if(found!=null) return found } - is Branch -> { + is ConditionalBranch -> { val found = stmt.truepart.searchSymbol(name) ?: stmt.elsepart.searchSymbol(name) if(found!=null) return found diff --git a/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt b/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt index 9a0a82158..f0654b3d0 100644 --- a/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt +++ b/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt @@ -518,14 +518,14 @@ private fun Prog8ANTLRParser.Else_partContext.toAst(): MutableList { return statement_block()?.toAst() ?: mutableListOf(statement().toAst()) } -private fun Prog8ANTLRParser.Branch_stmtContext.toAst(): Branch { +private fun Prog8ANTLRParser.Branch_stmtContext.toAst(): ConditionalBranch { val branchcondition = branchcondition().toAst() val trueStatements = statement_block()?.toAst() ?: mutableListOf(statement().toAst()) val elseStatements = else_part()?.toAst() ?: mutableListOf() val trueScope = AnonymousScope(trueStatements, statement_block()?.toPosition() ?: statement().toPosition()) val elseScope = AnonymousScope(elseStatements, else_part()?.toPosition() ?: toPosition()) - return Branch(branchcondition, trueScope, elseScope, toPosition()) + return ConditionalBranch(branchcondition, trueScope, elseScope, toPosition()) } private fun Prog8ANTLRParser.BranchconditionContext.toAst() = BranchCondition.valueOf( diff --git a/compilerAst/src/prog8/ast/statements/AstStatements.kt b/compilerAst/src/prog8/ast/statements/AstStatements.kt index d8a7c9e65..6551c82f1 100644 --- a/compilerAst/src/prog8/ast/statements/AstStatements.kt +++ b/compilerAst/src/prog8/ast/statements/AstStatements.kt @@ -639,19 +639,6 @@ class AnonymousScope(override var statements: MutableList, override fun accept(visitor: AstWalker, parent: Node) = visitor.visit(this, parent) } -class Nop(override val position: Position): Statement() { - override lateinit var parent: Node - - override fun linkParents(parent: Node) { - this.parent = parent - } - - override fun replaceChildNode(node: Node, replacement: Node) = throw FatalAstException("can't replace here") - override fun copy() = Nop(position) - override fun accept(visitor: IAstVisitor) = visitor.visit(this) - override fun accept(visitor: AstWalker, parent: Node) = visitor.visit(this, parent) -} - class AsmGenInfo { // This class contains various attributes that influence the assembly code generator. // Conceptually it should be part of any INameScope. @@ -818,10 +805,10 @@ class IfElse(var condition: Expression, } -class Branch(var condition: BranchCondition, - var truepart: AnonymousScope, - var elsepart: AnonymousScope, - override val position: Position) : Statement() { +class ConditionalBranch(var condition: BranchCondition, + var truepart: AnonymousScope, + var elsepart: AnonymousScope, + override val position: Position) : Statement() { override lateinit var parent: Node override fun linkParents(parent: Node) { diff --git a/compilerAst/src/prog8/ast/walk/AstWalker.kt b/compilerAst/src/prog8/ast/walk/AstWalker.kt index a4f9c73d9..4b9feb1b0 100644 --- a/compilerAst/src/prog8/ast/walk/AstWalker.kt +++ b/compilerAst/src/prog8/ast/walk/AstWalker.kt @@ -85,7 +85,7 @@ abstract class AstWalker { open fun before(assignTarget: AssignTarget, parent: Node): Iterable = noModifications open fun before(assignment: Assignment, parent: Node): Iterable = noModifications open fun before(block: Block, parent: Node): Iterable = noModifications - open fun before(branch: Branch, parent: Node): Iterable = noModifications + open fun before(branch: ConditionalBranch, parent: Node): Iterable = noModifications open fun before(breakStmt: Break, parent: Node): Iterable = noModifications open fun before(containment: ContainmentCheck, parent: Node): Iterable = noModifications open fun before(decl: VarDecl, parent: Node): Iterable = noModifications @@ -105,7 +105,6 @@ abstract class AstWalker { open fun before(memread: DirectMemoryRead, parent: Node): Iterable = noModifications open fun before(memwrite: DirectMemoryWrite, parent: Node): Iterable = noModifications open fun before(module: Module, parent: Node): Iterable = noModifications - open fun before(nop: Nop, parent: Node): Iterable = noModifications open fun before(numLiteral: NumericLiteralValue, parent: Node): Iterable = noModifications open fun before(postIncrDecr: PostIncrDecr, parent: Node): Iterable = noModifications open fun before(program: Program): Iterable = noModifications @@ -129,7 +128,7 @@ abstract class AstWalker { open fun after(assignTarget: AssignTarget, parent: Node): Iterable = noModifications open fun after(assignment: Assignment, parent: Node): Iterable = noModifications open fun after(block: Block, parent: Node): Iterable = noModifications - open fun after(branch: Branch, parent: Node): Iterable = noModifications + open fun after(branch: ConditionalBranch, parent: Node): Iterable = noModifications open fun after(breakStmt: Break, parent: Node): Iterable = noModifications open fun after(containment: ContainmentCheck, parent: Node): Iterable = noModifications open fun after(builtinFunctionPlaceholder: BuiltinFunctionPlaceholder, parent: Node): Iterable = noModifications @@ -150,7 +149,6 @@ abstract class AstWalker { open fun after(memread: DirectMemoryRead, parent: Node): Iterable = noModifications open fun after(memwrite: DirectMemoryWrite, parent: Node): Iterable = noModifications open fun after(module: Module, parent: Node): Iterable = noModifications - open fun after(nop: Nop, parent: Node): Iterable = noModifications open fun after(numLiteral: NumericLiteralValue, parent: Node): Iterable = noModifications open fun after(postIncrDecr: PostIncrDecr, parent: Node): Iterable = noModifications open fun after(program: Program): Iterable = noModifications @@ -299,7 +297,7 @@ abstract class AstWalker { track(after(ifElse, parent), ifElse, parent) } - fun visit(branch: Branch, parent: Node) { + fun visit(branch: ConditionalBranch, parent: Node) { track(before(branch, parent), branch, parent) branch.truepart.accept(this, branch) branch.elsepart.accept(this, branch) @@ -443,11 +441,6 @@ abstract class AstWalker { track(after(inlineAssembly, parent), inlineAssembly, parent) } - fun visit(nop: Nop, parent: Node) { - track(before(nop, parent), nop, parent) - track(after(nop, parent), nop, parent) - } - fun visit(whenStmt: When, parent: Node) { track(before(whenStmt, parent), whenStmt, parent) whenStmt.condition.accept(this, whenStmt) diff --git a/compilerAst/src/prog8/ast/walk/IAstVisitor.kt b/compilerAst/src/prog8/ast/walk/IAstVisitor.kt index b12205765..1135e5469 100644 --- a/compilerAst/src/prog8/ast/walk/IAstVisitor.kt +++ b/compilerAst/src/prog8/ast/walk/IAstVisitor.kt @@ -71,7 +71,7 @@ interface IAstVisitor { ifElse.elsepart.accept(this) } - fun visit(branch: Branch) { + fun visit(branch: ConditionalBranch) { branch.truepart.accept(this) branch.elsepart.accept(this) } @@ -169,9 +169,6 @@ interface IAstVisitor { fun visit(inlineAssembly: InlineAssembly) { } - fun visit(nop: Nop) { - } - fun visit(whenStmt: When) { whenStmt.condition.accept(this) whenStmt.choices.forEach { it.accept(this) }