mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-27 17:15:21 +00:00
Rewrite tree nodes in Kotlin. Documentation pending.
This commit is contained in:
31
src/main/kotlin/org/nwapw/abacus/tree/FunctionNode.kt
Normal file
31
src/main/kotlin/org/nwapw/abacus/tree/FunctionNode.kt
Normal file
@@ -0,0 +1,31 @@
|
||||
package org.nwapw.abacus.tree
|
||||
|
||||
data class FunctionNode(val function: String) : TreeNode() {
|
||||
|
||||
val children: MutableList<TreeNode> = mutableListOf()
|
||||
|
||||
override fun <T : Any> reduce(reducer: Reducer<T>): T? {
|
||||
val children = Array<Any?>(children.size, { children[it].reduce(reducer) ?: return null; })
|
||||
return reducer.reduceNode(this, *children)
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
val buffer = StringBuffer()
|
||||
buffer.append(function)
|
||||
buffer.append('(')
|
||||
for (i in 0 until children.size) {
|
||||
buffer.append(children[i].toString())
|
||||
buffer.append(if (i == children.size - 1) ")" else ",")
|
||||
}
|
||||
return super.toString()
|
||||
}
|
||||
|
||||
fun appendChild(node: TreeNode){
|
||||
children.add(node)
|
||||
}
|
||||
|
||||
fun prependChild(node: TreeNode){
|
||||
children.add(0, node)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user