mirror of
https://github.com/DanilaFe/abacus
synced 2024-11-17 08:03:09 -08:00
Abstract some Binary and Unary node logic.
This commit is contained in:
parent
73075c57b9
commit
07d7343339
|
@ -130,11 +130,11 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
||||||
TreeNode right = constructRecursive(matches);
|
TreeNode right = constructRecursive(matches);
|
||||||
TreeNode left = constructRecursive(matches);
|
TreeNode left = constructRecursive(matches);
|
||||||
if (left == null || right == null) return null;
|
if (left == null || right == null) return null;
|
||||||
else return new BinaryNode(operator, left, right);
|
else return new NumberBinaryNode(operator, left, right);
|
||||||
} else {
|
} else {
|
||||||
TreeNode applyTo = constructRecursive(matches);
|
TreeNode applyTo = constructRecursive(matches);
|
||||||
if (applyTo == null) return null;
|
if (applyTo == null) return null;
|
||||||
else return new UnaryNode(operator, applyTo);
|
else return new NumberUnaryNode(operator, applyTo);
|
||||||
}
|
}
|
||||||
} else if (matchType == TokenType.NUM) {
|
} else if (matchType == TokenType.NUM) {
|
||||||
return new NumberNode(match.getContent());
|
return new NumberNode(match.getContent());
|
||||||
|
|
|
@ -33,12 +33,12 @@ public class NumberReducer implements Reducer<NumberInterface> {
|
||||||
return abacus.numberFromString(((NumberNode) node).getNumber());
|
return abacus.numberFromString(((NumberNode) node).getNumber());
|
||||||
} else if(node instanceof VariableNode) {
|
} else if(node instanceof VariableNode) {
|
||||||
return abacus.numberFromString("0");
|
return abacus.numberFromString("0");
|
||||||
} else if (node instanceof BinaryNode) {
|
} else if (node instanceof NumberBinaryNode) {
|
||||||
NumberInterface left = (NumberInterface) children[0];
|
NumberInterface left = (NumberInterface) children[0];
|
||||||
NumberInterface right = (NumberInterface) children[1];
|
NumberInterface right = (NumberInterface) children[1];
|
||||||
NumberOperator operator = abacus.getPluginManager().operatorFor(((BinaryNode) node).getOperation());
|
NumberOperator operator = abacus.getPluginManager().operatorFor(((BinaryNode) node).getOperation());
|
||||||
return operator.apply(left, right);
|
return operator.apply(left, right);
|
||||||
} else if (node instanceof UnaryNode) {
|
} else if (node instanceof NumberUnaryNode) {
|
||||||
NumberInterface child = (NumberInterface) children[0];
|
NumberInterface child = (NumberInterface) children[0];
|
||||||
NumberOperator operator = abacus.getPluginManager().operatorFor(((UnaryNode) node).getOperation());
|
NumberOperator operator = abacus.getPluginManager().operatorFor(((UnaryNode) node).getOperation());
|
||||||
return operator.apply(child);
|
return operator.apply(child);
|
||||||
|
|
|
@ -11,13 +11,7 @@ package org.nwapw.abacus.tree
|
||||||
* @param left the left node.
|
* @param left the left node.
|
||||||
* @param right the right node.
|
* @param right the right node.
|
||||||
*/
|
*/
|
||||||
class BinaryNode(val operation: String, val left: TreeNode? = null, val right: TreeNode?) : TreeNode() {
|
abstract 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
return "(" + (left?.toString() ?: "null") + operation + (right?.toString() ?: "null") + ")"
|
return "(" + (left?.toString() ?: "null") + operation + (right?.toString() ?: "null") + ")"
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -9,12 +9,7 @@ package org.nwapw.abacus.tree
|
||||||
* @param operation the operation applied to the given node.
|
* @param operation the operation applied to the given node.
|
||||||
* @param applyTo the node to which the operation will be applied.
|
* @param applyTo the node to which the operation will be applied.
|
||||||
*/
|
*/
|
||||||
class UnaryNode(val operation: String, val applyTo: TreeNode? = null) : TreeNode() {
|
abstract 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
return "(" + (applyTo?.toString() ?: "null") + ")" + operation
|
return "(" + (applyTo?.toString() ?: "null") + ")" + operation
|
||||||
|
|
Loading…
Reference in New Issue
Block a user