1
0
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:
2017-08-07 18:57:43 -07:00
parent e54b5cdd66
commit 3bdc0e2ae5
10 changed files with 78 additions and 322 deletions

View 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)
}
}