From 3bdc0e2ae559b52ca09a5a24ba75c8887f66a5c9 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Mon, 7 Aug 2017 18:57:43 -0700 Subject: [PATCH] Rewrite tree nodes in Kotlin. Documentation pending. --- .../org/nwapw/abacus/tree/BinaryNode.java | 108 ------------------ .../org/nwapw/abacus/tree/FunctionNode.java | 85 -------------- .../org/nwapw/abacus/tree/NumberNode.java | 49 -------- .../java/org/nwapw/abacus/tree/TreeNode.java | 17 --- .../java/org/nwapw/abacus/tree/UnaryNode.java | 63 ---------- .../org/nwapw/abacus/tree/BinaryNode.kt | 15 +++ .../org/nwapw/abacus/tree/FunctionNode.kt | 31 +++++ .../org/nwapw/abacus/tree/NumberNode.kt | 15 +++ .../kotlin/org/nwapw/abacus/tree/TreeNode.kt | 7 ++ .../kotlin/org/nwapw/abacus/tree/UnaryNode.kt | 10 ++ 10 files changed, 78 insertions(+), 322 deletions(-) delete mode 100644 src/main/java/org/nwapw/abacus/tree/BinaryNode.java delete mode 100644 src/main/java/org/nwapw/abacus/tree/FunctionNode.java delete mode 100644 src/main/java/org/nwapw/abacus/tree/NumberNode.java delete mode 100644 src/main/java/org/nwapw/abacus/tree/TreeNode.java delete mode 100644 src/main/java/org/nwapw/abacus/tree/UnaryNode.java create mode 100644 src/main/kotlin/org/nwapw/abacus/tree/BinaryNode.kt create mode 100644 src/main/kotlin/org/nwapw/abacus/tree/FunctionNode.kt create mode 100644 src/main/kotlin/org/nwapw/abacus/tree/NumberNode.kt create mode 100644 src/main/kotlin/org/nwapw/abacus/tree/TreeNode.kt create mode 100644 src/main/kotlin/org/nwapw/abacus/tree/UnaryNode.kt diff --git a/src/main/java/org/nwapw/abacus/tree/BinaryNode.java b/src/main/java/org/nwapw/abacus/tree/BinaryNode.java deleted file mode 100644 index 6ec7b80..0000000 --- a/src/main/java/org/nwapw/abacus/tree/BinaryNode.java +++ /dev/null @@ -1,108 +0,0 @@ -package org.nwapw.abacus.tree; - -/** - * A tree node that represents an operation being applied to two operands. - */ -public class BinaryNode extends TreeNode { - - /** - * The operation being applied. - */ - private String operation; - /** - * The left node of the operation. - */ - private TreeNode left; - /** - * The right node of the operation. - */ - private TreeNode right; - - private BinaryNode() { - } - - /** - * Creates a new operation node with the given operation - * and no child nodes. - * - * @param operation the operation. - */ - public BinaryNode(String operation) { - this(operation, null, null); - } - - /** - * Creates a new operation node with the given operation - * and child nodes. - * - * @param operation the operation. - * @param left the left node of the expression. - * @param right the right node of the expression. - */ - public BinaryNode(String operation, TreeNode left, TreeNode right) { - this.operation = operation; - this.left = left; - this.right = right; - } - - /** - * Gets the operation in this node. - * - * @return the operation in this node. - */ - public String getOperation() { - return operation; - } - - /** - * Gets the left sub-expression of this node. - * - * @return the left node. - */ - public TreeNode getLeft() { - return left; - } - - /** - * Sets the left sub-expression of this node. - * - * @param left the sub-expression to apply. - */ - public void setLeft(TreeNode left) { - this.left = left; - } - - /** - * Gets the right sub-expression of this node. - * - * @return the right node. - */ - public TreeNode getRight() { - return right; - } - - /** - * Sets the right sub-expression of this node. - * - * @param right the sub-expression to apply. - */ - public void setRight(TreeNode right) { - this.right = right; - } - - @Override - public T reduce(Reducer reducer) { - T leftReduce = left.reduce(reducer); - T rightReduce = right.reduce(reducer); - if (leftReduce == null || rightReduce == null) return null; - return reducer.reduceNode(this, leftReduce, rightReduce); - } - - @Override - public String toString() { - String leftString = left != null ? left.toString() : "null"; - String rightString = right != null ? right.toString() : "null"; - - return "(" + leftString + operation + rightString + ")"; - } -} diff --git a/src/main/java/org/nwapw/abacus/tree/FunctionNode.java b/src/main/java/org/nwapw/abacus/tree/FunctionNode.java deleted file mode 100644 index a0ee26e..0000000 --- a/src/main/java/org/nwapw/abacus/tree/FunctionNode.java +++ /dev/null @@ -1,85 +0,0 @@ -package org.nwapw.abacus.tree; - -import java.util.ArrayList; -import java.util.List; - -/** - * A node that represents a function call. - */ -public class FunctionNode extends TreeNode { - - /** - * The name of the function being called - */ - private String function; - /** - * The list of arguments to the function. - */ - private List children; - - /** - * Creates a function node with no function. - */ - private FunctionNode() { - } - - /** - * Creates a new function node with the given function name. - * - * @param function the function name. - */ - public FunctionNode(String function) { - this.function = function; - children = new ArrayList<>(); - } - - /** - * Gets the function name for this node. - * - * @return the function name. - */ - public String getFunction() { - return function; - } - - /** - * Adds a child to the end of this node's child list. - * - * @param node the child to add. - */ - public void appendChild(TreeNode node) { - children.add(node); - } - - /** - * Adds a new child to the beginning of this node's child list. - * - * @param node the node to add. - */ - public void prependChild(TreeNode node) { - children.add(0, node); - } - - @Override - public T reduce(Reducer reducer) { - Object[] reducedChildren = new Object[children.size()]; - for (int i = 0; i < reducedChildren.length; i++) { - reducedChildren[i] = children.get(i).reduce(reducer); - if (reducedChildren[i] == null) return null; - } - return reducer.reduceNode(this, reducedChildren); - } - - @Override - public String toString() { - StringBuilder buffer = new StringBuilder(); - buffer.append(function); - buffer.append("("); - for (int i = 0; i < children.size(); i++) { - buffer.append(children.get(i)); - buffer.append(i == children.size() - 1 ? "" : ", "); - } - buffer.append(")"); - return buffer.toString(); - } -} diff --git a/src/main/java/org/nwapw/abacus/tree/NumberNode.java b/src/main/java/org/nwapw/abacus/tree/NumberNode.java deleted file mode 100644 index 14fab68..0000000 --- a/src/main/java/org/nwapw/abacus/tree/NumberNode.java +++ /dev/null @@ -1,49 +0,0 @@ -package org.nwapw.abacus.tree; - -import org.nwapw.abacus.number.NumberInterface; - -/** - * A node implementation that represents a single number. - */ -public class NumberNode extends TreeNode { - - /** - * The number that is represented by this number node. - */ - private NumberInterface number; - - /** - * Creates a number node with no number. - */ - public NumberNode() { - number = null; - } - - /** - * Creates a new number node with the given double value. - * - * @param newNumber the number for which to create a number node. - */ - public NumberNode(NumberInterface newNumber) { - this.number = newNumber; - } - - /** - * Gets the number value of this node. - * - * @return the number value of this node. - */ - public NumberInterface getNumber() { - return number; - } - - @Override - public T reduce(Reducer reducer) { - return reducer.reduceNode(this); - } - - @Override - public String toString() { - return number != null ? number.toString() : "null"; - } -} diff --git a/src/main/java/org/nwapw/abacus/tree/TreeNode.java b/src/main/java/org/nwapw/abacus/tree/TreeNode.java deleted file mode 100644 index d98e638..0000000 --- a/src/main/java/org/nwapw/abacus/tree/TreeNode.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.nwapw.abacus.tree; - -/** - * An abstract class that represents an expression tree node. - */ -public abstract class TreeNode { - - /** - * The function that reduces a tree to a single vale. - * - * @param reducer the reducer used to reduce the tree. - * @param the type the reducer produces. - * @return the result of the reduction, or null on error. - */ - public abstract T reduce(Reducer reducer); - -} diff --git a/src/main/java/org/nwapw/abacus/tree/UnaryNode.java b/src/main/java/org/nwapw/abacus/tree/UnaryNode.java deleted file mode 100644 index 298d25d..0000000 --- a/src/main/java/org/nwapw/abacus/tree/UnaryNode.java +++ /dev/null @@ -1,63 +0,0 @@ -package org.nwapw.abacus.tree; - -public class UnaryNode extends TreeNode { - - /** - * The operation this node will apply. - */ - private String operation; - /** - * The tree node to apply the operation to. - */ - private TreeNode applyTo; - - /** - * Creates a new node with the given operation and no child. - * - * @param operation the operation for this node. - */ - public UnaryNode(String operation) { - this(operation, null); - } - - /** - * Creates a new node with the given operation and child. - * - * @param operation the operation for this node. - * @param applyTo the node to apply the function to. - */ - public UnaryNode(String operation, TreeNode applyTo) { - this.operation = operation; - this.applyTo = applyTo; - } - - @Override - public T reduce(Reducer reducer) { - Object reducedChild = applyTo.reduce(reducer); - if (reducedChild == null) return null; - return reducer.reduceNode(this, reducedChild); - } - - /** - * Gets the operation of this node. - * - * @return the operation this node performs. - */ - public String getOperation() { - return operation; - } - - /** - * Gets the node to which this node's operation applies. - * - * @return the tree node to which the operation will be applied. - */ - public TreeNode getApplyTo() { - return applyTo; - } - - @Override - public String toString() { - return "(" + (applyTo == null ? "null" : applyTo.toString()) + ")" + operation; - } -} diff --git a/src/main/kotlin/org/nwapw/abacus/tree/BinaryNode.kt b/src/main/kotlin/org/nwapw/abacus/tree/BinaryNode.kt new file mode 100644 index 0000000..3493515 --- /dev/null +++ b/src/main/kotlin/org/nwapw/abacus/tree/BinaryNode.kt @@ -0,0 +1,15 @@ +package org.nwapw.abacus.tree + +data 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) + } + + override fun toString(): String { + return "(" + (left?.toString() ?: "null") + operation + (right?.toString() ?: "null") + ")" + } + +} \ No newline at end of file diff --git a/src/main/kotlin/org/nwapw/abacus/tree/FunctionNode.kt b/src/main/kotlin/org/nwapw/abacus/tree/FunctionNode.kt new file mode 100644 index 0000000..bb95f3f --- /dev/null +++ b/src/main/kotlin/org/nwapw/abacus/tree/FunctionNode.kt @@ -0,0 +1,31 @@ +package org.nwapw.abacus.tree + +data class FunctionNode(val function: String) : TreeNode() { + + val children: MutableList = mutableListOf() + + 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 super.toString() + } + + fun appendChild(node: TreeNode){ + children.add(node) + } + + fun prependChild(node: TreeNode){ + children.add(0, node) + } + +} \ No newline at end of file diff --git a/src/main/kotlin/org/nwapw/abacus/tree/NumberNode.kt b/src/main/kotlin/org/nwapw/abacus/tree/NumberNode.kt new file mode 100644 index 0000000..5d9c09b --- /dev/null +++ b/src/main/kotlin/org/nwapw/abacus/tree/NumberNode.kt @@ -0,0 +1,15 @@ +package org.nwapw.abacus.tree + +import org.nwapw.abacus.number.NumberInterface + +data class NumberNode(val number: NumberInterface) : TreeNode() { + + override fun reduce(reducer: Reducer): T? { + return reducer.reduceNode(this) + } + + override fun toString(): String { + return number.toString() + } + +} \ No newline at end of file diff --git a/src/main/kotlin/org/nwapw/abacus/tree/TreeNode.kt b/src/main/kotlin/org/nwapw/abacus/tree/TreeNode.kt new file mode 100644 index 0000000..5ebe978 --- /dev/null +++ b/src/main/kotlin/org/nwapw/abacus/tree/TreeNode.kt @@ -0,0 +1,7 @@ +package org.nwapw.abacus.tree + +abstract class TreeNode { + + abstract fun reduce(reducer: Reducer) : T? + +} \ No newline at end of file diff --git a/src/main/kotlin/org/nwapw/abacus/tree/UnaryNode.kt b/src/main/kotlin/org/nwapw/abacus/tree/UnaryNode.kt new file mode 100644 index 0000000..c9adc62 --- /dev/null +++ b/src/main/kotlin/org/nwapw/abacus/tree/UnaryNode.kt @@ -0,0 +1,10 @@ +package org.nwapw.abacus.tree + +data 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) + } + +} \ No newline at end of file