From 3581017489436f3c9e72b17740d02d27be28cac8 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 9 Jul 2019 09:02:56 +0200 Subject: [PATCH] added ast printing of when statement --- .../src/prog8/compiler/AstToSourceCode.kt | 27 ++++++++++++++++++- compiler/src/prog8/compiler/Main.kt | 1 - examples/test.p8 | 3 +++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/compiler/src/prog8/compiler/AstToSourceCode.kt b/compiler/src/prog8/compiler/AstToSourceCode.kt index cddc7d4dd..d119684f8 100644 --- a/compiler/src/prog8/compiler/AstToSourceCode.kt +++ b/compiler/src/prog8/compiler/AstToSourceCode.kt @@ -1,5 +1,6 @@ package prog8.compiler +import prog8.ast.antlr.escape import prog8.ast.IFunctionCall import prog8.ast.IStatement import prog8.ast.Module @@ -245,7 +246,7 @@ class AstToSourceCode(val output: (text: String) -> Unit): IAstVisitor { override fun visit(literalValue: LiteralValue) { when { literalValue.isNumeric -> output(literalValue.asNumericValue.toString()) - literalValue.isString -> output("\"${literalValue.strvalue}\"") + literalValue.isString -> output("\"${escape(literalValue.strvalue!!)}\"") literalValue.isArray -> { if(literalValue.arrayvalue!=null) { output("[") @@ -385,6 +386,30 @@ class AstToSourceCode(val output: (text: String) -> Unit): IAstVisitor { output(builtinFunctionStatementPlaceholder.name) } + override fun visit(whenStatement: WhenStatement) { + output("when ") + whenStatement.condition.accept(this) + outputln(" {") + scopelevel++ + whenStatement.choices.forEach { it.accept(this) } + scopelevel-- + outputlni("}") + } + + override fun visit(whenChoice: WhenChoice) { + if(whenChoice.value==null) + outputi("else -> ") + else { + outputi("") + whenChoice.value.accept(this) + output(" -> ") + } + if(whenChoice.statements.statements.size==1) + whenChoice.statements.statements.single().accept(this) + else + whenChoice.statements.accept(this) + outputln("") + } override fun visit(nopStatement: NopStatement) { TODO("NOP???") } diff --git a/compiler/src/prog8/compiler/Main.kt b/compiler/src/prog8/compiler/Main.kt index 2f95981ed..2371dab64 100644 --- a/compiler/src/prog8/compiler/Main.kt +++ b/compiler/src/prog8/compiler/Main.kt @@ -65,7 +65,6 @@ fun compileProgram(filepath: Path, val time3 = measureTimeMillis { programAst.reorderStatements() // reorder statements and add type casts, to please the compiler later } - printAst(programAst) //println(" time3: $time3") val time4 = measureTimeMillis { programAst.checkValid(compilerOptions) // check if tree is valid diff --git a/examples/test.p8 b/examples/test.p8 index bf3b93dba..49a371c67 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -9,6 +9,9 @@ Y=22 uword uw = A*Y + str teststring = "hello" + c64scr.print(&teststring) + when uw { 12345 -> { A=44