mirror of
https://github.com/DanilaFe/abacus
synced 2024-11-17 08:03:09 -08:00
Abstract the call functionality, and add TreeValueFunctionNode.
This commit is contained in:
parent
c5cd0f81ad
commit
bc26ad0b88
|
@ -144,7 +144,7 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
|||
while (!matches.isEmpty() && matches.get(0).getType() != TokenType.INTERNAL_FUNCTION_END) {
|
||||
TreeNode argument = constructRecursive(matches);
|
||||
if (argument == null) return null;
|
||||
node.prependChild(argument);
|
||||
node.getChildren().add(0, argument);
|
||||
}
|
||||
if (matches.isEmpty()) return null;
|
||||
matches.remove(0);
|
||||
|
|
|
@ -46,7 +46,7 @@ public class NumberReducer implements Reducer<NumberInterface> {
|
|||
for (int i = 0; i < convertedChildren.length; i++) {
|
||||
convertedChildren[i] = (NumberInterface) children[i];
|
||||
}
|
||||
Function function = abacus.getPluginManager().functionFor(((FunctionNode) node).getFunction());
|
||||
Function function = abacus.getPluginManager().functionFor(((FunctionNode) node).getCallTo());
|
||||
if (function == null) return null;
|
||||
return function.apply(convertedChildren);
|
||||
}
|
||||
|
|
29
core/src/main/kotlin/org/nwapw/abacus/tree/CallNode.kt
Normal file
29
core/src/main/kotlin/org/nwapw/abacus/tree/CallNode.kt
Normal 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()
|
||||
}
|
||||
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user