1
0
mirror of https://github.com/DanilaFe/abacus synced 2026-01-31 19:15:20 +00:00

Abstract the call functionality, and add TreeValueFunctionNode.

This commit is contained in:
2017-08-25 01:17:52 -07:00
parent c5cd0f81ad
commit bc26ad0b88
5 changed files with 48 additions and 37 deletions

View File

@@ -0,0 +1,29 @@
package org.nwapw.abacus.tree
/**
* Represents a more generic function call.
*
* This class does not specify how it should be reduced, allowing other classes
* to extend this functionality.
*
* @param callTo the name of the things being called.
*/
abstract class CallNode(val callTo: String) : TreeNode() {
/**
* The list of children this node has.
*/
val children: MutableList<TreeNode> = mutableListOf()
override fun toString(): String {
val buffer = StringBuffer()
buffer.append(callTo)
buffer.append("(")
for(i in 0 until children.size){
buffer.append(children[i].toString())
buffer.append(if(i != children.size - 1) ", " else ")")
}
return buffer.toString()
}
}

View File

@@ -8,45 +8,11 @@ package org.nwapw.abacus.tree
*
* @param function the function string.
*/
class FunctionNode(val function: String) : TreeNode() {
/**
* List of function parameters added to this node.
*/
val children: MutableList<TreeNode> = mutableListOf()
class FunctionNode(function: String) : CallNode(function) {
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 buffer.toString()
}
/**
* Appends a child to this node's list of children.
*
* @node the node to append.
*/
fun appendChild(node: TreeNode) {
children.add(node)
}
/**
* Prepends a child to this node's list of children.
*
* @node the node to prepend.
*/
fun prependChild(node: TreeNode) {
children.add(0, node)
}
}

View File

@@ -0,0 +1,16 @@
package org.nwapw.abacus.tree
/**
* A tree node that represents a tree value function call.
*
* This is in many ways similar to a simple FunctionNode, and the distinction
* is mostly to help the reducer. Besides that, this class also does not
* even attempt to reduce its children.
*/
class TreeValueFunctionNode(name: String) : CallNode(name) {
override fun <T : Any> reduce(reducer: Reducer<T>): T? {
return reducer.reduceNode(this)
}
}