From 65fa8c46130b1d5da6f9f982126e30f8c0ba8491 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Mon, 8 Jul 2019 22:29:22 +0200 Subject: [PATCH] ast source printer fixes --- compiler/src/prog8/ast/Interfaces.kt | 8 ++-- .../src/prog8/compiler/AstToSourceCode.kt | 37 +++++++++++-------- examples/test.p8 | 9 ++--- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/compiler/src/prog8/ast/Interfaces.kt b/compiler/src/prog8/ast/Interfaces.kt index a227854e3..cb6405519 100644 --- a/compiler/src/prog8/ast/Interfaces.kt +++ b/compiler/src/prog8/ast/Interfaces.kt @@ -34,8 +34,8 @@ interface Node { } interface IStatement : Node { - fun accept(processor: IAstModifyingVisitor) : IStatement - fun accept(processor: IAstVisitor) + fun accept(visitor: IAstModifyingVisitor) : IStatement + fun accept(visitor: IAstVisitor) fun makeScopedName(name: String): String { // easy way out is to always return the full scoped name. // it would be nicer to find only the minimal prefixed scoped name, but that's too much hassle for now. @@ -167,8 +167,8 @@ interface INameScope { interface IExpression: Node { fun constValue(program: Program): LiteralValue? - fun accept(processor: IAstModifyingVisitor): IExpression - fun accept(processor: IAstVisitor) + fun accept(visitor: IAstModifyingVisitor): IExpression + fun accept(visitor: IAstVisitor) fun referencesIdentifier(name: String): Boolean fun inferType(program: Program): DataType? diff --git a/compiler/src/prog8/compiler/AstToSourceCode.kt b/compiler/src/prog8/compiler/AstToSourceCode.kt index cee3855c6..cddc7d4dd 100644 --- a/compiler/src/prog8/compiler/AstToSourceCode.kt +++ b/compiler/src/prog8/compiler/AstToSourceCode.kt @@ -1,6 +1,7 @@ package prog8.compiler import prog8.ast.IFunctionCall +import prog8.ast.IStatement import prog8.ast.Module import prog8.ast.Program import prog8.ast.base.* @@ -159,16 +160,22 @@ class AstToSourceCode(val output: (text: String) -> Unit): IAstVisitor { else { outputln("{ ") scopelevel++ - subroutine.statements.forEach { - outputi("") - it.accept(this) - output("\n") - } + outputStatements(subroutine.statements) scopelevel-- outputi("}") } } + private fun outputStatements(statements: List) { + for(stmt in statements) { + if(stmt is VarDecl && stmt.autoGenerated) + continue // skip autogenerated decls + outputi("") + stmt.accept(this) + output("\n") + } + } + override fun visit(functionCall: FunctionCall) { printout(functionCall as IFunctionCall) } @@ -240,13 +247,15 @@ class AstToSourceCode(val output: (text: String) -> Unit): IAstVisitor { literalValue.isNumeric -> output(literalValue.asNumericValue.toString()) literalValue.isString -> output("\"${literalValue.strvalue}\"") literalValue.isArray -> { - output("[") - for(v in literalValue.arrayvalue!!) { - v.accept(this) - if(v!==literalValue.arrayvalue.last()) - output(", ") + if(literalValue.arrayvalue!=null) { + output("[") + for (v in literalValue.arrayvalue) { + v.accept(this) + if (v !== literalValue.arrayvalue.last()) + output(", ") + } + output("]") } - output("]") } } } @@ -335,11 +344,7 @@ class AstToSourceCode(val output: (text: String) -> Unit): IAstVisitor { override fun visit(scope: AnonymousScope) { outputln("{") scopelevel++ - scope.statements.forEach { - outputi("") - it.accept(this) - output("\n") - } + outputStatements(scope.statements) scopelevel-- outputi("}") } diff --git a/examples/test.p8 b/examples/test.p8 index 97aef4083..cd653158d 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -12,13 +12,10 @@ @(xw) = 1 ; @todo should turn border white @(xw) = 1 ; @todo should turn border white - Y=12 ; @todo gets removed in assembly???!!! + Y=2 ; @todo gets removed in assembly???!!! A=Y - A=99 - A=A - A=22 - @($d021)=A - @(xw) = A + @($d021)=Y + @(xw) = Y }