From 1575d3e57443a06d15f45047d530884b973b4edf Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 7 Sep 2017 12:31:40 -0700 Subject: [PATCH] Remove nullability from tree nodes. --- .../nwapw/abacus/parsing/ShuntingYardParser.java | 15 ++++++++------- .../kotlin/org/nwapw/abacus/tree/BinaryNode.kt | 4 ++-- .../main/kotlin/org/nwapw/abacus/tree/CallNode.kt | 8 ++------ .../kotlin/org/nwapw/abacus/tree/FunctionNode.kt | 2 +- .../org/nwapw/abacus/tree/NumberBinaryNode.kt | 6 +++--- .../org/nwapw/abacus/tree/NumberUnaryNode.kt | 4 ++-- .../org/nwapw/abacus/tree/TreeValueBinaryNode.kt | 2 +- .../nwapw/abacus/tree/TreeValueFunctionNode.kt | 2 +- .../org/nwapw/abacus/tree/TreeValueUnaryNode.kt | 2 +- .../kotlin/org/nwapw/abacus/tree/UnaryNode.kt | 4 ++-- 10 files changed, 23 insertions(+), 26 deletions(-) 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 9d326a8..611f5c9 100644 --- a/core/src/main/java/org/nwapw/abacus/parsing/ShuntingYardParser.java +++ b/core/src/main/java/org/nwapw/abacus/parsing/ShuntingYardParser.java @@ -154,19 +154,20 @@ public class ShuntingYardParser implements Parser>, PluginListe return new VariableNode(match.getContent()); } else if (matchType == TokenType.FUNCTION || matchType == TokenType.TREE_VALUE_FUNCTION) { String functionName = match.getContent(); - CallNode node; - if (matchType == TokenType.FUNCTION) { - node = new FunctionNode(functionName); - } else { - node = new TreeValueFunctionNode(functionName); - } + List children = new ArrayList<>(); while (!matches.isEmpty() && matches.get(0).getType() != TokenType.INTERNAL_FUNCTION_END) { TreeNode argument = constructRecursive(matches); if (argument == null) return null; - node.getChildren().add(0, argument); + children.add(0, argument); } if (matches.isEmpty()) return null; matches.remove(0); + CallNode node; + if (matchType == TokenType.FUNCTION) { + node = new FunctionNode(functionName, children); + } else { + node = new TreeValueFunctionNode(functionName, children); + } return node; } return null; diff --git a/core/src/main/kotlin/org/nwapw/abacus/tree/BinaryNode.kt b/core/src/main/kotlin/org/nwapw/abacus/tree/BinaryNode.kt index 4d0d9dd..aa04571 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/tree/BinaryNode.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/tree/BinaryNode.kt @@ -11,10 +11,10 @@ package org.nwapw.abacus.tree * @param left the left node. * @param right the right node. */ -abstract class BinaryNode(val operation: String, val left: TreeNode? = null, val right: TreeNode?) : TreeNode() { +abstract class BinaryNode(val operation: String, val left: TreeNode, val right: TreeNode) : TreeNode() { override fun toString(): String { - return "(" + (left?.toString() ?: "null") + operation + (right?.toString() ?: "null") + ")" + return "(" + left.toString() + operation + right.toString() + ")" } } \ No newline at end of file diff --git a/core/src/main/kotlin/org/nwapw/abacus/tree/CallNode.kt b/core/src/main/kotlin/org/nwapw/abacus/tree/CallNode.kt index 5f99494..38595dd 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/tree/CallNode.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/tree/CallNode.kt @@ -7,13 +7,9 @@ package org.nwapw.abacus.tree * to extend this functionality. * * @param callTo the name of the things being called. + * @param children the children of this node. */ -abstract class CallNode(val callTo: String) : TreeNode() { - - /** - * The list of children this node has. - */ - val children: MutableList = mutableListOf() +abstract class CallNode(val callTo: String, val children: List) : TreeNode() { override fun toString(): String { val buffer = StringBuffer() 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 813ad52..d6a7696 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/tree/FunctionNode.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/tree/FunctionNode.kt @@ -8,7 +8,7 @@ package org.nwapw.abacus.tree * * @param function the function string. */ -class FunctionNode(function: String) : CallNode(function) { +class FunctionNode(function: String, children: List) : CallNode(function, children) { override fun reduce(reducer: Reducer): T? { val children = Array(children.size, { children[it].reduce(reducer) ?: return null; }) diff --git a/core/src/main/kotlin/org/nwapw/abacus/tree/NumberBinaryNode.kt b/core/src/main/kotlin/org/nwapw/abacus/tree/NumberBinaryNode.kt index 74c2206..36f585f 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/tree/NumberBinaryNode.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/tree/NumberBinaryNode.kt @@ -10,12 +10,12 @@ package org.nwapw.abacus.tree * @param left the left child of this node. * @param right the right child of this node. */ -class NumberBinaryNode(operation: String, left: TreeNode?, right: TreeNode?) +class NumberBinaryNode(operation: String, left: TreeNode, right: TreeNode) : BinaryNode(operation, left, right) { override fun reduce(reducer: Reducer): T? { - val left = left?.reduce(reducer) ?: return null - val right = right?.reduce(reducer) ?: return null + val left = left.reduce(reducer) ?: return null + val right = right.reduce(reducer) ?: return null return reducer.reduceNode(this, left, right) } diff --git a/core/src/main/kotlin/org/nwapw/abacus/tree/NumberUnaryNode.kt b/core/src/main/kotlin/org/nwapw/abacus/tree/NumberUnaryNode.kt index e4fee3f..1dff327 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/tree/NumberUnaryNode.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/tree/NumberUnaryNode.kt @@ -8,11 +8,11 @@ package org.nwapw.abacus.tree * @param operation the operation this node performs. * @param child the child this node should be applied to. */ -class NumberUnaryNode(operation: String, child: TreeNode?) +class NumberUnaryNode(operation: String, child: TreeNode) : UnaryNode(operation, child) { override fun reduce(reducer: Reducer): T? { - val child = applyTo?.reduce(reducer) ?: return null + val child = applyTo.reduce(reducer) return reducer.reduceNode(this, child) } diff --git a/core/src/main/kotlin/org/nwapw/abacus/tree/TreeValueBinaryNode.kt b/core/src/main/kotlin/org/nwapw/abacus/tree/TreeValueBinaryNode.kt index 4edfef3..e29319b 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/tree/TreeValueBinaryNode.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/tree/TreeValueBinaryNode.kt @@ -11,7 +11,7 @@ package org.nwapw.abacus.tree * @param left the left child of this node. * @param right the right child of this node. */ -class TreeValueBinaryNode(operation: String, left: TreeNode?, right: TreeNode?) +class TreeValueBinaryNode(operation: String, left: TreeNode, right: TreeNode) : BinaryNode(operation, left, right) { override fun reduce(reducer: Reducer): T? { diff --git a/core/src/main/kotlin/org/nwapw/abacus/tree/TreeValueFunctionNode.kt b/core/src/main/kotlin/org/nwapw/abacus/tree/TreeValueFunctionNode.kt index 68c358e..ca24d9d 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/tree/TreeValueFunctionNode.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/tree/TreeValueFunctionNode.kt @@ -7,7 +7,7 @@ package org.nwapw.abacus.tree * 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) { +class TreeValueFunctionNode(name: String, children: List) : CallNode(name, children) { override fun reduce(reducer: Reducer): T? { return reducer.reduceNode(this) diff --git a/core/src/main/kotlin/org/nwapw/abacus/tree/TreeValueUnaryNode.kt b/core/src/main/kotlin/org/nwapw/abacus/tree/TreeValueUnaryNode.kt index fdc0db5..7c3d836 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/tree/TreeValueUnaryNode.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/tree/TreeValueUnaryNode.kt @@ -9,7 +9,7 @@ package org.nwapw.abacus.tree * @param operation the operation this node performs. * @param child the node the operation should be applied to. */ -class TreeValueUnaryNode(operation: String, child: TreeNode?) +class TreeValueUnaryNode(operation: String, child: TreeNode) : UnaryNode(operation, child) { override fun reduce(reducer: Reducer): T? { diff --git a/core/src/main/kotlin/org/nwapw/abacus/tree/UnaryNode.kt b/core/src/main/kotlin/org/nwapw/abacus/tree/UnaryNode.kt index 285231b..ea518e2 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/tree/UnaryNode.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/tree/UnaryNode.kt @@ -9,10 +9,10 @@ package org.nwapw.abacus.tree * @param operation the operation applied to the given node. * @param applyTo the node to which the operation will be applied. */ -abstract class UnaryNode(val operation: String, val applyTo: TreeNode? = null) : TreeNode() { +abstract class UnaryNode(val operation: String, val applyTo: TreeNode) : TreeNode() { override fun toString(): String { - return "(" + (applyTo?.toString() ?: "null") + ")" + operation + return "(" + applyTo.toString() + ")" + operation } } \ No newline at end of file