diff --git a/codeCore/src/prog8/code/ast/AstBase.kt b/codeCore/src/prog8/code/ast/AstBase.kt index c2fbe6e97..457d32f58 100644 --- a/codeCore/src/prog8/code/ast/AstBase.kt +++ b/codeCore/src/prog8/code/ast/AstBase.kt @@ -14,16 +14,6 @@ sealed class PtNode(val position: Position) { val children = mutableListOf() lateinit var parent: PtNode - fun printIndented(indent: Int) { - print(" ".repeat(indent)) - print("${this.javaClass.simpleName} ") - printProperties() - println() - children.forEach { it.printIndented(indent+1) } - } - - abstract fun printProperties() - fun add(child: PtNode) { children.add(child) child.parent = this @@ -41,9 +31,7 @@ sealed class PtNode(val position: Position) { } -class PtNodeGroup : PtNode(Position.DUMMY) { - override fun printProperties() {} -} +class PtNodeGroup : PtNode(Position.DUMMY) sealed class PtNamedNode(var name: String, position: Position): PtNode(position) { @@ -67,10 +55,6 @@ class PtProgram( val memsizer: IMemSizer, val encoding: IStringEncoding ) : PtNode(Position.DUMMY) { - fun print() = printIndented(0) - override fun printProperties() { - print("'$name'") - } // fun allModuleDirectives(): Sequence = // children.asSequence().flatMap { it.children }.filterIsInstance().distinct() @@ -91,10 +75,6 @@ class PtBlock(name: String, val source: SourceCode, // taken from the module the block is defined in. position: Position ) : PtNamedNode(name, position) { - override fun printProperties() { - print("$name addr=$address library=$library forceOutput=$forceOutput alignment=$alignment") - } - enum class BlockAlignment { NONE, WORD, @@ -104,8 +84,6 @@ class PtBlock(name: String, class PtInlineAssembly(val assembly: String, val isIR: Boolean, position: Position) : PtNode(position) { - override fun printProperties() {} - init { require(!assembly.startsWith('\n') && !assembly.startsWith('\r')) { "inline assembly should be trimmed" } require(!assembly.endsWith('\n') && !assembly.endsWith('\r')) { "inline assembly should be trimmed" } @@ -113,28 +91,16 @@ class PtInlineAssembly(val assembly: String, val isIR: Boolean, position: Positi } -class PtLabel(name: String, position: Position) : PtNamedNode(name, position) { - override fun printProperties() { - print(name) - } -} +class PtLabel(name: String, position: Position) : PtNamedNode(name, position) -class PtBreakpoint(position: Position): PtNode(position) { - override fun printProperties() {} -} +class PtBreakpoint(position: Position): PtNode(position) -class PtIncludeBinary(val file: Path, val offset: UInt?, val length: UInt?, position: Position) : PtNode(position) { - override fun printProperties() { - print("filename=$file offset=$offset length=$length") - } -} +class PtIncludeBinary(val file: Path, val offset: UInt?, val length: UInt?, position: Position) : PtNode(position) -class PtNop(position: Position): PtNode(position) { - override fun printProperties() {} -} +class PtNop(position: Position): PtNode(position) // find the parent node of a specific type or interface diff --git a/codeCore/src/prog8/code/ast/AstExpressions.kt b/codeCore/src/prog8/code/ast/AstExpressions.kt index 85a60e84a..93fcf43af 100644 --- a/codeCore/src/prog8/code/ast/AstExpressions.kt +++ b/codeCore/src/prog8/code/ast/AstExpressions.kt @@ -21,10 +21,6 @@ sealed class PtExpression(val type: DataType, position: Position) : PtNode(posit } } - override fun printProperties() { - print(type) - } - infix fun isSameAs(other: PtExpression): Boolean { return when(this) { is PtAddressOf -> other is PtAddressOf && other.type==type && other.identifier isSameAs identifier @@ -101,9 +97,6 @@ class PtBuiltinFunctionCall(val name: String, val args: List get() = children.map { it as PtExpression } - override fun printProperties() { - print("$name void=$void noSideFx=$hasNoSideEffects") - } } @@ -113,10 +106,6 @@ class PtBinaryExpression(val operator: String, type: DataType, position: Positio get() = children[0] as PtExpression val right: PtExpression get() = children[1] as PtExpression - - override fun printProperties() { - print("$operator -> $type") - } } @@ -139,23 +128,15 @@ class PtFunctionCall(val name: String, val args: List get() = children.map { it as PtExpression } - override fun printProperties() { - print("$name void=$void") - } } -class PtIdentifier(val name: String, type: DataType, position: Position) : PtExpression(type, position) { - override fun printProperties() { - print("$name $type") - } -} +class PtIdentifier(val name: String, type: DataType, position: Position) : PtExpression(type, position) class PtMemoryByte(position: Position) : PtExpression(DataType.UBYTE, position) { val address: PtExpression get() = children.single() as PtExpression - override fun printProperties() {} } @@ -176,10 +157,6 @@ class PtNumber(type: DataType, val number: Double, position: Position) : PtExpre } } - override fun printProperties() { - print("$number ($type)") - } - override fun hashCode(): Int = Objects.hash(type, number) override fun equals(other: Any?): Boolean { @@ -200,10 +177,6 @@ class PtPrefix(val operator: String, type: DataType, position: Position): PtExpr // note: the "not" operator may no longer occur in the ast; not x should have been replaced with x==0 require(operator in setOf("+", "-", "~")) { "invalid prefix operator: $operator" } } - - override fun printProperties() { - print(operator) - } } @@ -214,16 +187,10 @@ class PtRange(type: DataType, position: Position) : PtExpression(type, position) get() = children[1] as PtExpression val step: PtNumber get() = children[2] as PtNumber - - override fun printProperties() {} } class PtString(val value: String, val encoding: Encoding, position: Position) : PtExpression(DataType.STR, position) { - override fun printProperties() { - print("$encoding:\"$value\"") - } - override fun hashCode(): Int = Objects.hash(value, encoding) override fun equals(other: Any?): Boolean { if(other==null || other !is PtString) @@ -240,11 +207,7 @@ class PtTypeCast(type: DataType, position: Position) : PtExpression(type, positi // special node that isn't created from compiling user code, but used internally in the Intermediate Code -class PtMachineRegister(val register: Int, type: DataType, position: Position) : PtExpression(type, position) { - override fun printProperties() { - print("reg=$register $type") - } -} +class PtMachineRegister(val register: Int, type: DataType, position: Position) : PtExpression(type, position) fun constValue(expr: PtExpression): Double? = if(expr is PtNumber) expr.number else null diff --git a/codeCore/src/prog8/code/ast/AstStatements.kt b/codeCore/src/prog8/code/ast/AstStatements.kt index 9e4b23a71..8f9c5f440 100644 --- a/codeCore/src/prog8/code/ast/AstStatements.kt +++ b/codeCore/src/prog8/code/ast/AstStatements.kt @@ -15,11 +15,7 @@ class PtAsmSub( val returns: List>, val inline: Boolean, position: Position -) : PtNamedNode(name, position), IPtSubroutine { - override fun printProperties() { - print("$name inline=$inline") - } -} +) : PtNamedNode(name, position), IPtSubroutine class PtSub( @@ -28,10 +24,6 @@ class PtSub( val returntype: DataType?, position: Position ) : PtNamedNode(name, position), IPtSubroutine { - override fun printProperties() { - print(name) - } - init { // params and return value should not be str if(parameters.any{ it.type !in NumericDatatypes }) @@ -43,11 +35,7 @@ class PtSub( } -class PtSubroutineParameter(name: String, val type: DataType, position: Position): PtNamedNode(name, position) { - override fun printProperties() { - print("$type $name") - } -} +class PtSubroutineParameter(name: String, val type: DataType, position: Position): PtNamedNode(name, position) class PtAssignment(position: Position) : PtNode(position) { @@ -56,8 +44,6 @@ class PtAssignment(position: Position) : PtNode(position) { val value: PtExpression get() = children[1] as PtExpression - override fun printProperties() { } - val isInplaceAssign: Boolean by lazy { val target = target.children.single() as PtExpression when(val source = value) { @@ -105,8 +91,6 @@ class PtAssignTarget(position: Position) : PtNode(position) { } } - override fun printProperties() {} - infix fun isSameAs(expression: PtExpression): Boolean = expression.isSameAs(this) } @@ -116,10 +100,6 @@ class PtConditionalBranch(val condition: BranchCondition, position: Position) : get() = children[0] as PtNodeGroup val falseScope: PtNodeGroup get() = children[1] as PtNodeGroup - - override fun printProperties() { - print(condition) - } } @@ -130,8 +110,6 @@ class PtForLoop(position: Position) : PtNode(position) { get() = children[1] as PtExpression val statements: PtNodeGroup get() = children[2] as PtNodeGroup - - override fun printProperties() {} } @@ -142,8 +120,6 @@ class PtIfElse(position: Position) : PtNode(position) { get() = children[1] as PtNodeGroup val elseScope: PtNodeGroup get() = children[2] as PtNodeGroup - - override fun printProperties() {} } @@ -151,12 +127,6 @@ class PtJump(val identifier: PtIdentifier?, val address: UInt?, val generatedLabel: String?, position: Position) : PtNode(position) { - override fun printProperties() { - identifier?.printProperties() - if(address!=null) print(address.toHex()) - if(generatedLabel!=null) print(generatedLabel) - } - init { identifier?.let {it.parent = this } } @@ -166,10 +136,6 @@ class PtJump(val identifier: PtIdentifier?, class PtPostIncrDecr(val operator: String, position: Position) : PtNode(position) { val target: PtAssignTarget get() = children.single() as PtAssignTarget - - override fun printProperties() { - print(operator) - } } @@ -178,8 +144,6 @@ class PtRepeatLoop(position: Position) : PtNode(position) { get() = children[0] as PtExpression val statements: PtNodeGroup get() = children[1] as PtNodeGroup - - override fun printProperties() {} } @@ -192,8 +156,6 @@ class PtReturn(position: Position) : PtNode(position) { else null } - - override fun printProperties() {} } @@ -204,27 +166,16 @@ sealed interface IPtVariable { class PtVariable(name: String, override val type: DataType, val zeropage: ZeropageWish, val value: PtExpression?, val arraySize: UInt?, position: Position) : PtNamedNode(name, position), IPtVariable { - override fun printProperties() { - print("$type $name") - } init { value?.let {it.parent=this} } } -class PtConstant(name: String, override val type: DataType, val value: Double, position: Position) : PtNamedNode(name, position), IPtVariable { - override fun printProperties() { - print("$type $name = $value") - } -} +class PtConstant(name: String, override val type: DataType, val value: Double, position: Position) : PtNamedNode(name, position), IPtVariable -class PtMemMapped(name: String, override val type: DataType, val address: UInt, val arraySize: UInt?, position: Position) : PtNamedNode(name, position), IPtVariable { - override fun printProperties() { - print("&$type $name = ${address.toHex()}") - } -} +class PtMemMapped(name: String, override val type: DataType, val address: UInt, val arraySize: UInt?, position: Position) : PtNamedNode(name, position), IPtVariable class PtWhen(position: Position) : PtNode(position) { @@ -232,8 +183,6 @@ class PtWhen(position: Position) : PtNode(position) { get() = children[0] as PtExpression val choices: PtNodeGroup get() = children[1] as PtNodeGroup - - override fun printProperties() {} } @@ -242,5 +191,4 @@ class PtWhenChoice(val isElse: Boolean, position: Position) : PtNode(position) { get() = children[0] as PtNodeGroup val statements: PtNodeGroup get() = children[1] as PtNodeGroup - override fun printProperties() {} } diff --git a/compiler/test/ast/TestIntermediateAst.kt b/compiler/test/ast/TestIntermediateAst.kt index 26fa1a91c..8d944c80a 100644 --- a/compiler/test/ast/TestIntermediateAst.kt +++ b/compiler/test/ast/TestIntermediateAst.kt @@ -66,7 +66,7 @@ class TestIntermediateAst: FunSpec({ val fcall = (entry.children[4] as PtAssignment).value as PtFunctionCall fcall.void shouldBe false fcall.type shouldBe DataType.UBYTE - ast.print() + printAst(ast, ::println) } }) \ No newline at end of file