Create some base nodes
This commit is contained in:
5
src/main/kotlin/dsl/ast/model/node/AccessOperatorNode.kt
Normal file
5
src/main/kotlin/dsl/ast/model/node/AccessOperatorNode.kt
Normal file
@@ -0,0 +1,5 @@
|
||||
package dsl.ast.model.node
|
||||
|
||||
import dsl.token.model.entity.TokenPosition
|
||||
|
||||
class AccessOperatorNode(position: TokenPosition) : BinaryOperatorAbstractNode(position)
|
||||
12
src/main/kotlin/dsl/ast/model/node/AtomAbstractNode.kt
Normal file
12
src/main/kotlin/dsl/ast/model/node/AtomAbstractNode.kt
Normal file
@@ -0,0 +1,12 @@
|
||||
package dsl.ast.model.node
|
||||
|
||||
import dsl.token.model.entity.TokenPosition
|
||||
|
||||
abstract class AtomAbstractNode(position: TokenPosition) : Node(1, position) {
|
||||
var value: Any
|
||||
get() = children[0]
|
||||
set(value) {
|
||||
children[0] = value
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package dsl.ast.model.node
|
||||
|
||||
import dsl.token.model.entity.TokenPosition
|
||||
|
||||
abstract class BinaryOperatorAbstractNode(position: TokenPosition) : Node(3, position) {
|
||||
var lhs: Any
|
||||
get() = children[0]
|
||||
set(value) {
|
||||
children[0] = value
|
||||
}
|
||||
|
||||
var operator: Any
|
||||
get() = children[1]
|
||||
set(value) {
|
||||
children[1] = value
|
||||
}
|
||||
|
||||
var rhs: Any
|
||||
get() = children[2]
|
||||
set(value) {
|
||||
children[2] = value
|
||||
}
|
||||
}
|
||||
5
src/main/kotlin/dsl/ast/model/node/BoolLiteralNode.kt
Normal file
5
src/main/kotlin/dsl/ast/model/node/BoolLiteralNode.kt
Normal file
@@ -0,0 +1,5 @@
|
||||
package dsl.ast.model.node
|
||||
|
||||
import dsl.token.model.entity.TokenPosition
|
||||
|
||||
class BoolLiteralNode(position: TokenPosition) : AtomAbstractNode(position)
|
||||
5
src/main/kotlin/dsl/ast/model/node/FloatLiteralNode.kt
Normal file
5
src/main/kotlin/dsl/ast/model/node/FloatLiteralNode.kt
Normal file
@@ -0,0 +1,5 @@
|
||||
package dsl.ast.model.node
|
||||
|
||||
import dsl.token.model.entity.TokenPosition
|
||||
|
||||
class FloatLiteralNode(position: TokenPosition) : AtomAbstractNode(position)
|
||||
5
src/main/kotlin/dsl/ast/model/node/IntegerLiteralNode.kt
Normal file
5
src/main/kotlin/dsl/ast/model/node/IntegerLiteralNode.kt
Normal file
@@ -0,0 +1,5 @@
|
||||
package dsl.ast.model.node
|
||||
|
||||
import dsl.token.model.entity.TokenPosition
|
||||
|
||||
class IntegerLiteralNode(position: TokenPosition) : AtomAbstractNode(position)
|
||||
5
src/main/kotlin/dsl/ast/model/node/MinusOperatorNode.kt
Normal file
5
src/main/kotlin/dsl/ast/model/node/MinusOperatorNode.kt
Normal file
@@ -0,0 +1,5 @@
|
||||
package dsl.ast.model.node
|
||||
|
||||
import dsl.token.model.entity.TokenPosition
|
||||
|
||||
class MinusOperatorNode(position: TokenPosition) : UnaryOperatorAbstractNode(position)
|
||||
38
src/main/kotlin/dsl/ast/model/node/Node.kt
Normal file
38
src/main/kotlin/dsl/ast/model/node/Node.kt
Normal file
@@ -0,0 +1,38 @@
|
||||
package dsl.ast.model.node
|
||||
|
||||
import dsl.token.model.entity.TokenPosition
|
||||
|
||||
abstract class Node(numberOfChildren: Int, val position: TokenPosition) {
|
||||
protected var children: MutableList<Any> = MutableList(numberOfChildren) { NONE }
|
||||
|
||||
constructor(children: List<Any>, position: TokenPosition) : this(children.size, position) {
|
||||
this.children = children.toMutableList()
|
||||
}
|
||||
|
||||
operator fun get(index: Int) = children[index]
|
||||
|
||||
operator fun set(index: Int, value: Any) {
|
||||
children[index] = value
|
||||
}
|
||||
|
||||
fun pretty(prefix: String = "", last: Boolean = true, first: Boolean = true) {
|
||||
var newPrefix = prefix
|
||||
var newLast = last
|
||||
val nodeName = this::class.simpleName ?: "<anonymous>"
|
||||
|
||||
println(newPrefix + (if (first) "" else if (newLast) "└─" else "├─") + nodeName + " " + position)
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
val NONE = NoneNode()
|
||||
}
|
||||
}
|
||||
7
src/main/kotlin/dsl/ast/model/node/NoneNode.kt
Normal file
7
src/main/kotlin/dsl/ast/model/node/NoneNode.kt
Normal file
@@ -0,0 +1,7 @@
|
||||
package dsl.ast.model.node
|
||||
|
||||
import dsl.token.model.entity.TokenPosition
|
||||
|
||||
class NoneNode : Node(0,
|
||||
TokenPosition.NONE
|
||||
)
|
||||
5
src/main/kotlin/dsl/ast/model/node/NoteLiteralNode.kt
Normal file
5
src/main/kotlin/dsl/ast/model/node/NoteLiteralNode.kt
Normal file
@@ -0,0 +1,5 @@
|
||||
package dsl.ast.model.node
|
||||
|
||||
import dsl.token.model.entity.TokenPosition
|
||||
|
||||
class NoteLiteralNode(position: TokenPosition) : AtomAbstractNode(position)
|
||||
5
src/main/kotlin/dsl/ast/model/node/StringLiteralNode.kt
Normal file
5
src/main/kotlin/dsl/ast/model/node/StringLiteralNode.kt
Normal file
@@ -0,0 +1,5 @@
|
||||
package dsl.ast.model.node
|
||||
|
||||
import dsl.token.model.entity.TokenPosition
|
||||
|
||||
class StringLiteralNode(position: TokenPosition) : AtomAbstractNode(position)
|
||||
15
src/main/kotlin/dsl/ast/model/node/TokenNode.kt
Normal file
15
src/main/kotlin/dsl/ast/model/node/TokenNode.kt
Normal file
@@ -0,0 +1,15 @@
|
||||
package dsl.ast.model.node
|
||||
|
||||
import dsl.token.model.entity.Token
|
||||
|
||||
class TokenNode(_token: Token) : Node(1, _token.position) {
|
||||
var type: Any
|
||||
get() = children[0]
|
||||
set(value) {
|
||||
children[0] = value
|
||||
}
|
||||
|
||||
init {
|
||||
type = _token
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package dsl.ast.model.node
|
||||
|
||||
import dsl.token.model.entity.TokenPosition
|
||||
|
||||
abstract class UnaryOperatorAbstractNode(position: TokenPosition) : Node(2, position) {
|
||||
var operator: Any
|
||||
get() = children[0]
|
||||
set(value) {
|
||||
children[0] = value
|
||||
}
|
||||
|
||||
var value: Any
|
||||
get() = children[1]
|
||||
set(_value) {
|
||||
children[1] = _value
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
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