From 07d7343339bd221a11dd340b21174d90b5119e3d Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 25 Aug 2017 17:46:25 -0700 Subject: [PATCH] Abstract some Binary and Unary node logic. --- .../abacus/parsing/ShuntingYardParser.java | 4 ++-- .../org/nwapw/abacus/tree/NumberReducer.java | 4 ++-- .../org/nwapw/abacus/tree/BinaryNode.kt | 8 +------ .../org/nwapw/abacus/tree/NumberBinaryNode.kt | 22 +++++++++++++++++++ .../org/nwapw/abacus/tree/NumberUnaryNode.kt | 19 ++++++++++++++++ .../kotlin/org/nwapw/abacus/tree/UnaryNode.kt | 7 +----- 6 files changed, 47 insertions(+), 17 deletions(-) create mode 100644 core/src/main/kotlin/org/nwapw/abacus/tree/NumberBinaryNode.kt create mode 100644 core/src/main/kotlin/org/nwapw/abacus/tree/NumberUnaryNode.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 95bc4b2..b7bac6d 100644 --- a/core/src/main/java/org/nwapw/abacus/parsing/ShuntingYardParser.java +++ b/core/src/main/java/org/nwapw/abacus/parsing/ShuntingYardParser.java @@ -130,11 +130,11 @@ public class ShuntingYardParser implements Parser>, PluginListe TreeNode right = constructRecursive(matches); TreeNode left = constructRecursive(matches); if (left == null || right == null) return null; - else return new BinaryNode(operator, left, right); + else return new NumberBinaryNode(operator, left, right); } else { TreeNode applyTo = constructRecursive(matches); if (applyTo == null) return null; - else return new UnaryNode(operator, applyTo); + else return new NumberUnaryNode(operator, applyTo); } } else if (matchType == TokenType.NUM) { return new NumberNode(match.getContent()); 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 5160a3b..c744195 100644 --- a/core/src/main/java/org/nwapw/abacus/tree/NumberReducer.java +++ b/core/src/main/java/org/nwapw/abacus/tree/NumberReducer.java @@ -33,12 +33,12 @@ public class NumberReducer implements Reducer { return abacus.numberFromString(((NumberNode) node).getNumber()); } else if(node instanceof VariableNode) { return abacus.numberFromString("0"); - } else if (node instanceof BinaryNode) { + } else if (node instanceof NumberBinaryNode) { NumberInterface left = (NumberInterface) children[0]; NumberInterface right = (NumberInterface) children[1]; NumberOperator operator = abacus.getPluginManager().operatorFor(((BinaryNode) node).getOperation()); return operator.apply(left, right); - } else if (node instanceof UnaryNode) { + } else if (node instanceof NumberUnaryNode) { NumberInterface child = (NumberInterface) children[0]; NumberOperator operator = abacus.getPluginManager().operatorFor(((UnaryNode) node).getOperation()); return operator.apply(child); 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 01efa8d..4d0d9dd 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/tree/BinaryNode.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/tree/BinaryNode.kt @@ -11,13 +11,7 @@ package org.nwapw.abacus.tree * @param left the left node. * @param right the right node. */ -class BinaryNode(val operation: String, val left: TreeNode? = null, val right: TreeNode?) : TreeNode() { - - override fun reduce(reducer: Reducer): T? { - val leftReduce = left?.reduce(reducer) ?: return null - val rightReduce = right?.reduce(reducer) ?: return null - return reducer.reduceNode(this, leftReduce, rightReduce) - } +abstract class BinaryNode(val operation: String, val left: TreeNode? = null, val right: TreeNode?) : TreeNode() { override fun toString(): String { return "(" + (left?.toString() ?: "null") + operation + (right?.toString() ?: "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 new file mode 100644 index 0000000..74c2206 --- /dev/null +++ b/core/src/main/kotlin/org/nwapw/abacus/tree/NumberBinaryNode.kt @@ -0,0 +1,22 @@ +package org.nwapw.abacus.tree + +/** + * A binary operator node that reduces its children. + * + * NumberBinaryNode operates by simply reducing its children and + * then using the result of that reduction to reduce itself. + * + * @param operation the operation this node performs. + * @param left the left child of this node. + * @param right the right child of this node. + */ +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 + return reducer.reduceNode(this, left, right) + } + +} \ No newline at end of file diff --git a/core/src/main/kotlin/org/nwapw/abacus/tree/NumberUnaryNode.kt b/core/src/main/kotlin/org/nwapw/abacus/tree/NumberUnaryNode.kt new file mode 100644 index 0000000..e4fee3f --- /dev/null +++ b/core/src/main/kotlin/org/nwapw/abacus/tree/NumberUnaryNode.kt @@ -0,0 +1,19 @@ +package org.nwapw.abacus.tree + +/** + * A unary operator node that reduces its children. + * + * NumberUnaryNode operates by simply reducing its child, + * and using the result of that reduction to reduce itself. + * @param operation the operation this node performs. + * @param child the child this node should be applied to. + */ +class NumberUnaryNode(operation: String, child: TreeNode?) + : UnaryNode(operation, child) { + + override fun reduce(reducer: Reducer): T? { + val child = applyTo?.reduce(reducer) ?: return null + return reducer.reduceNode(this, child) + } + +} \ No newline at end of file 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 c2375b2..285231b 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/tree/UnaryNode.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/tree/UnaryNode.kt @@ -9,12 +9,7 @@ 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. */ -class UnaryNode(val operation: String, val applyTo: TreeNode? = null) : TreeNode() { - - override fun reduce(reducer: Reducer): T? { - val reducedChild = applyTo?.reduce(reducer) ?: return null - return reducer.reduceNode(this, reducedChild) - } +abstract class UnaryNode(val operation: String, val applyTo: TreeNode? = null) : TreeNode() { override fun toString(): String { return "(" + (applyTo?.toString() ?: "null") + ")" + operation