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 { interface IStatement : Node {
fun accept(processor: IAstModifyingVisitor) : IStatement fun accept(visitor: IAstModifyingVisitor) : IStatement
fun accept(processor: IAstVisitor) fun accept(visitor: IAstVisitor)
fun makeScopedName(name: String): String { fun makeScopedName(name: String): String {
// easy way out is to always return the full scoped name. // 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. // 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 { interface IExpression: Node {
fun constValue(program: Program): LiteralValue? fun constValue(program: Program): LiteralValue?
fun accept(processor: IAstModifyingVisitor): IExpression fun accept(visitor: IAstModifyingVisitor): IExpression
fun accept(processor: IAstVisitor) fun accept(visitor: IAstVisitor)
fun referencesIdentifier(name: String): Boolean fun referencesIdentifier(name: String): Boolean
fun inferType(program: Program): DataType? fun inferType(program: Program): DataType?

View File

@ -1,6 +1,7 @@
package prog8.compiler package prog8.compiler
import prog8.ast.IFunctionCall import prog8.ast.IFunctionCall
import prog8.ast.IStatement
import prog8.ast.Module import prog8.ast.Module
import prog8.ast.Program import prog8.ast.Program
import prog8.ast.base.* import prog8.ast.base.*
@ -159,16 +160,22 @@ class AstToSourceCode(val output: (text: String) -> Unit): IAstVisitor {
else { else {
outputln("{ ") outputln("{ ")
scopelevel++ scopelevel++
subroutine.statements.forEach { outputStatements(subroutine.statements)
outputi("")
it.accept(this)
output("\n")
}
scopelevel-- scopelevel--
outputi("}") 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) { override fun visit(functionCall: FunctionCall) {
printout(functionCall as IFunctionCall) printout(functionCall as IFunctionCall)
} }
@ -240,13 +247,15 @@ class AstToSourceCode(val output: (text: String) -> Unit): IAstVisitor {
literalValue.isNumeric -> output(literalValue.asNumericValue.toString()) literalValue.isNumeric -> output(literalValue.asNumericValue.toString())
literalValue.isString -> output("\"${literalValue.strvalue}\"") literalValue.isString -> output("\"${literalValue.strvalue}\"")
literalValue.isArray -> { literalValue.isArray -> {
output("[") if(literalValue.arrayvalue!=null) {
for(v in literalValue.arrayvalue!!) { output("[")
v.accept(this) for (v in literalValue.arrayvalue) {
if(v!==literalValue.arrayvalue.last()) v.accept(this)
output(", ") 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) { override fun visit(scope: AnonymousScope) {
outputln("{") outputln("{")
scopelevel++ scopelevel++
scope.statements.forEach { outputStatements(scope.statements)
outputi("")
it.accept(this)
output("\n")
}
scopelevel-- scopelevel--
outputi("}") outputi("}")
} }

View File

@ -12,13 +12,10 @@
@(xw) = 1 ; @todo should turn border white @(xw) = 1 ; @todo should turn border white
@(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=Y
A=99 @($d021)=Y
A=A @(xw) = Y
A=22
@($d021)=A
@(xw) = A
} }