Abstract some Binary and Unary node logic.

This commit is contained in:
Danila Fedorin 2017-08-25 17:46:25 -07:00
parent 73075c57b9
commit 07d7343339
6 changed files with 47 additions and 17 deletions

View File

@ -130,11 +130,11 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, 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());

View File

@ -33,12 +33,12 @@ public class NumberReducer implements Reducer<NumberInterface> {
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);

View File

@ -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 <T : Any> reduce(reducer: Reducer<T>): 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") + ")"

View File

@ -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 <T : Any> reduce(reducer: Reducer<T>): T? {
val left = left?.reduce(reducer) ?: return null
val right = right?.reduce(reducer) ?: return null
return reducer.reduceNode(this, left, right)
}
}

View File

@ -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 <T : Any> reduce(reducer: Reducer<T>): T? {
val child = applyTo?.reduce(reducer) ?: return null
return reducer.reduceNode(this, child)
}
}

View File

@ -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 <T : Any> reduce(reducer: Reducer<T>): 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