From bc26ad0b8826330968908d5f789ea3e52af1854b Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 25 Aug 2017 01:17:52 -0700 Subject: [PATCH] Abstract the call functionality, and add TreeValueFunctionNode. --- .../abacus/parsing/ShuntingYardParser.java | 2 +- .../org/nwapw/abacus/tree/NumberReducer.java | 2 +- .../kotlin/org/nwapw/abacus/tree/CallNode.kt | 29 +++++++++++++++ .../org/nwapw/abacus/tree/FunctionNode.kt | 36 +------------------ .../abacus/tree/TreeValueFunctionNode.kt | 16 +++++++++ 5 files changed, 48 insertions(+), 37 deletions(-) create mode 100644 core/src/main/kotlin/org/nwapw/abacus/tree/CallNode.kt create mode 100644 core/src/main/kotlin/org/nwapw/abacus/tree/TreeValueFunctionNode.kt diff --git a/core/src/main/java/org/nwapw/abacus/parsing/ShuntingYardParser.java b/core/src/main/java/org/nwapw/abacus/parsing/ShuntingYardParser.java index c792463..c1a35a5 100644 --- a/core/src/main/java/org/nwapw/abacus/parsing/ShuntingYardParser.java +++ b/core/src/main/java/org/nwapw/abacus/parsing/ShuntingYardParser.java @@ -144,7 +144,7 @@ public class ShuntingYardParser implements Parser>, 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); diff --git a/core/src/main/java/org/nwapw/abacus/tree/NumberReducer.java b/core/src/main/java/org/nwapw/abacus/tree/NumberReducer.java index 4dae775..a252896 100644 --- a/core/src/main/java/org/nwapw/abacus/tree/NumberReducer.java +++ b/core/src/main/java/org/nwapw/abacus/tree/NumberReducer.java @@ -46,7 +46,7 @@ public class NumberReducer implements Reducer { 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); } diff --git a/core/src/main/kotlin/org/nwapw/abacus/tree/CallNode.kt b/core/src/main/kotlin/org/nwapw/abacus/tree/CallNode.kt new file mode 100644 index 0000000..5c059ae --- /dev/null +++ b/core/src/main/kotlin/org/nwapw/abacus/tree/CallNode.kt @@ -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 = 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() + } + +} \ No newline at end of file diff --git a/core/src/main/kotlin/org/nwapw/abacus/tree/FunctionNode.kt b/core/src/main/kotlin/org/nwapw/abacus/tree/FunctionNode.kt index dcc8b09..813ad52 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/tree/FunctionNode.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/tree/FunctionNode.kt @@ -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 = mutableListOf() +class FunctionNode(function: String) : CallNode(function) { override fun reduce(reducer: Reducer): T? { val children = Array(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) - } - } \ No newline at end of file diff --git a/core/src/main/kotlin/org/nwapw/abacus/tree/TreeValueFunctionNode.kt b/core/src/main/kotlin/org/nwapw/abacus/tree/TreeValueFunctionNode.kt new file mode 100644 index 0000000..68c358e --- /dev/null +++ b/core/src/main/kotlin/org/nwapw/abacus/tree/TreeValueFunctionNode.kt @@ -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 reduce(reducer: Reducer): T? { + return reducer.reduceNode(this) + } + +}