Create abstract Node model
This commit is contained in:
@@ -14,6 +14,7 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
|
||||
runtime group: 'org.jetbrains.kotlin', name: 'kotlin-reflect', version: '1.3.61'
|
||||
testCompile group: 'junit', name: 'junit', version: '4.12'
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
import dsl.token.tokenizer.DefaultTokenizer
|
||||
import interpreter.Interpreter
|
||||
import java.io.File
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val interpreter = Interpreter()
|
||||
interpreter.run(File("/home/bartek/Developent/SMNP-Kotlin/examples/adeste.mus"))
|
||||
}
|
||||
36
src/main/kotlin/dsl/ast/node/Node.kt
Normal file
36
src/main/kotlin/dsl/ast/node/Node.kt
Normal file
@@ -0,0 +1,36 @@
|
||||
package dsl.ast.node
|
||||
|
||||
import dsl.token.model.entity.TokenPosition
|
||||
|
||||
class NoneNode : Node(0, TokenPosition.NONE)
|
||||
|
||||
abstract class Node(numberOfChildren: Int, val position: TokenPosition) {
|
||||
protected var children: Array<Node> = Array(numberOfChildren) { NONE }
|
||||
|
||||
constructor(children: Array<Node>, position: TokenPosition) : this(children.size, position) {
|
||||
this.children = children
|
||||
}
|
||||
|
||||
operator fun get(index: Int) = children[index]
|
||||
|
||||
operator fun set(index: Int, value: Node) {
|
||||
children[index] = value
|
||||
}
|
||||
|
||||
fun pretty(prefix: String = "", last: Boolean = true, first: Boolean = true) {
|
||||
var newPrefix = prefix
|
||||
var newLast = last
|
||||
val newFirst = first
|
||||
|
||||
println(newPrefix + (if (newFirst) "" else if (newLast) "└─" else "├─") + this::class.simpleName + " " + position)
|
||||
newPrefix += if (newLast) " " else "│ "
|
||||
for((index, child) in children.withIndex()) {
|
||||
newLast = index == children.size - 1
|
||||
child.pretty(newPrefix, newLast, false)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
val NONE = NoneNode()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user