Refactor Node to refer other Nodes instead of Any
This commit is contained in:
@@ -2,12 +2,10 @@ package dsl.ast.model.node
|
||||
|
||||
import dsl.token.model.entity.Token
|
||||
|
||||
abstract class AtomAbstractNode(token: Token) : Node(1, token.position) {
|
||||
val value: Any
|
||||
get() = children[0]
|
||||
|
||||
init {
|
||||
children[0] = token.value
|
||||
abstract class AtomAbstractNode(val token: Token) : Node(1, token.position) {
|
||||
override fun pretty(prefix: String, last: Boolean, first: Boolean) {
|
||||
println(prefix + (if (first) "" else if (last) "└─" else "├─") + this::class.simpleName + " " + position)
|
||||
println(prefix + (if (last) " " else "│ ") + "└ " + token)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package dsl.ast.model.node
|
||||
|
||||
abstract class BinaryOperatorAbstractNode(lhs: Node, operator: Node, rhs: Node) : Node(3, operator.position) {
|
||||
val lhs: Any
|
||||
val lhs: Node
|
||||
get() = children[0]
|
||||
|
||||
val operator: Any
|
||||
val operator: Node
|
||||
get() = children[1]
|
||||
|
||||
val rhs: Any
|
||||
val rhs: Node
|
||||
get() = children[2]
|
||||
|
||||
init {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package dsl.ast.model.node
|
||||
|
||||
class BlockNode(begin: Node, statements: List<Node>, end: Node) : Node(statements, begin.position) {
|
||||
val statements: List<Any>
|
||||
val statements: List<Node>
|
||||
get() = children
|
||||
|
||||
init {
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package dsl.ast.model.node
|
||||
|
||||
class ConditionNode(trueBranchToken: Node, condition: Node, trueBranch: Node, falseBranchToken: Node, falseBranch: Node) : Node(3, trueBranchToken.position) {
|
||||
val condition: Any
|
||||
val condition: Node
|
||||
get() = children[0]
|
||||
|
||||
val trueBranch: Any
|
||||
val trueBranch: Node
|
||||
get() = children[1]
|
||||
|
||||
val falseBranch: Any
|
||||
val falseBranch: Node
|
||||
get() = children[2]
|
||||
|
||||
init {
|
||||
|
||||
@@ -3,20 +3,20 @@ package dsl.ast.model.node
|
||||
import dsl.token.model.entity.TokenPosition
|
||||
|
||||
abstract class Node(numberOfChildren: Int, val position: TokenPosition) {
|
||||
var children: MutableList<Any> = MutableList(numberOfChildren) { NONE }
|
||||
var children: MutableList<Node> = MutableList(numberOfChildren) { NONE }
|
||||
protected set
|
||||
|
||||
constructor(children: List<Any>, position: TokenPosition) : this(children.size, position) {
|
||||
constructor(children: List<Node>, position: TokenPosition) : this(children.size, position) {
|
||||
this.children = children.toMutableList()
|
||||
}
|
||||
|
||||
operator fun get(index: Int) = children[index]
|
||||
|
||||
operator fun set(index: Int, value: Any) {
|
||||
operator fun set(index: Int, value: Node) {
|
||||
children[index] = value
|
||||
}
|
||||
|
||||
fun pretty(prefix: String = "", last: Boolean = true, first: Boolean = true) {
|
||||
open fun pretty(prefix: String = "", last: Boolean = true, first: Boolean = true) {
|
||||
var newPrefix = prefix
|
||||
var newLast = last
|
||||
val nodeName = this::class.simpleName ?: "<anonymous>"
|
||||
@@ -25,11 +25,8 @@ abstract class Node(numberOfChildren: Int, val position: TokenPosition) {
|
||||
newPrefix += if (newLast) " " else "│ "
|
||||
for ((index, child) in children.withIndex()) {
|
||||
newLast = index == children.size - 1
|
||||
if (child is Node) {
|
||||
child.pretty(newPrefix, newLast, false)
|
||||
} else {
|
||||
println(newPrefix + (if (newLast) "└ " else "├ ") + child)
|
||||
}
|
||||
child.pretty(newPrefix, newLast, false)
|
||||
//println(newPrefix + (if (newLast) "└ " else "├ ") + child) // todo move to atom nodes
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package dsl.ast.model.node
|
||||
|
||||
class ReturnNode(value: Node) : Node(1, value.position) {
|
||||
val value: Any
|
||||
val value: Node
|
||||
get() = children[0]
|
||||
|
||||
init {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package dsl.ast.model.node
|
||||
|
||||
class ThrowNode(value: Node) : Node(1, value.position) {
|
||||
val value: Any
|
||||
val value: Node
|
||||
get() = children[0]
|
||||
|
||||
init {
|
||||
|
||||
@@ -2,11 +2,4 @@ package dsl.ast.model.node
|
||||
|
||||
import dsl.token.model.entity.Token
|
||||
|
||||
class TokenNode(token: Token) : Node(1, token.position) {
|
||||
val token: Any
|
||||
get() = children[0]
|
||||
|
||||
init {
|
||||
children[0] = token
|
||||
}
|
||||
}
|
||||
class TokenNode(token: Token) : AtomAbstractNode(token)
|
||||
@@ -1,10 +1,10 @@
|
||||
package dsl.ast.model.node
|
||||
|
||||
abstract class UnaryOperatorAbstractNode(operator: Node, operand: Node) : Node(2, operator.position) {
|
||||
val operator: Any
|
||||
val operator: Node
|
||||
get() = children[0]
|
||||
|
||||
val operand: Any
|
||||
val operand: Node
|
||||
get() = children[1]
|
||||
|
||||
init {
|
||||
|
||||
@@ -15,6 +15,7 @@ class AtomParser : Parser() {
|
||||
) {
|
||||
it[1]
|
||||
}
|
||||
|
||||
val literalParser = oneOf(
|
||||
listOf(
|
||||
parenthesesParser,
|
||||
|
||||
Reference in New Issue
Block a user