ast source printer fixes

This commit is contained in:
Irmen de Jong 2019-07-08 22:29:22 +02:00
parent c1102393bb
commit 65fa8c4613
3 changed files with 28 additions and 26 deletions

View File

@ -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?

View File

@ -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<IStatement>) {
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("}")
}

View File

@ -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
}