1
0
mirror of https://github.com/DanilaFe/abacus synced 2026-01-27 09:05:19 +00:00

Format code.

This commit is contained in:
2017-07-30 21:11:32 -07:00
parent 122874b97a
commit 3ce74303ed
39 changed files with 695 additions and 561 deletions

View File

@@ -18,25 +18,28 @@ public class BinaryInfixNode extends TreeNode {
*/
private TreeNode right;
private BinaryInfixNode() {}
private BinaryInfixNode() {
}
/**
* Creates a new operation node with the given operation
* and no child nodes.
*
* @param operation the operation.
*/
public BinaryInfixNode(String operation){
public BinaryInfixNode(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.
* @param left the left node of the expression.
* @param right the right node of the expression.
*/
public BinaryInfixNode(String operation, TreeNode left, TreeNode right){
public BinaryInfixNode(String operation, TreeNode left, TreeNode right) {
this.operation = operation;
this.left = left;
this.right = right;
@@ -44,6 +47,7 @@ public class BinaryInfixNode extends TreeNode {
/**
* Gets the operation in this node.
*
* @return the operation in this node.
*/
public String getOperation() {
@@ -52,6 +56,7 @@ public class BinaryInfixNode extends TreeNode {
/**
* Gets the left sub-expression of this node.
*
* @return the left node.
*/
public TreeNode getLeft() {
@@ -60,6 +65,7 @@ public class BinaryInfixNode extends TreeNode {
/**
* Sets the left sub-expression of this node.
*
* @param left the sub-expression to apply.
*/
public void setLeft(TreeNode left) {
@@ -68,6 +74,7 @@ public class BinaryInfixNode extends TreeNode {
/**
* Gets the right sub-expression of this node.
*
* @return the right node.
*/
public TreeNode getRight() {
@@ -76,6 +83,7 @@ public class BinaryInfixNode extends TreeNode {
/**
* Sets the right sub-expression of this node.
*
* @param right the sub-expression to apply.
*/
public void setRight(TreeNode right) {
@@ -86,7 +94,7 @@ public class BinaryInfixNode extends TreeNode {
public <T> T reduce(Reducer<T> reducer) {
T leftReduce = left.reduce(reducer);
T rightReduce = right.reduce(reducer);
if(leftReduce == null || rightReduce == null) return null;
if (leftReduce == null || rightReduce == null) return null;
return reducer.reduceNode(this, leftReduce, rightReduce);
}

View File

@@ -20,19 +20,22 @@ public class FunctionNode extends TreeNode {
/**
* Creates a function node with no function.
*/
private FunctionNode() { }
private FunctionNode() {
}
/**
* Creates a new function node with the given function name.
*
* @param function the function name.
*/
public FunctionNode(String function){
public FunctionNode(String function) {
this.function = function;
children = new ArrayList<>();
}
/**
* Gets the function name for this node.
*
* @return the function name.
*/
public String getFunction() {
@@ -41,14 +44,16 @@ public class FunctionNode extends TreeNode {
/**
* Adds a child to the end of this node's child list.
*
* @param node the child to add.
*/
public void appendChild(TreeNode node){
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) {
@@ -58,9 +63,9 @@ public class FunctionNode extends TreeNode {
@Override
public <T> T reduce(Reducer<T> reducer) {
Object[] reducedChildren = new Object[children.size()];
for(int i = 0; i < reducedChildren.length; i++){
for (int i = 0; i < reducedChildren.length; i++) {
reducedChildren[i] = children.get(i).reduce(reducer);
if(reducedChildren[i] == null) return null;
if (reducedChildren[i] == null) return null;
}
return reducer.reduceNode(this, reducedChildren);
}
@@ -70,7 +75,7 @@ public class FunctionNode extends TreeNode {
StringBuilder buffer = new StringBuilder();
buffer.append(function);
buffer.append("(");
for(int i = 0; i < children.size(); i++){
for (int i = 0; i < children.size(); i++) {
buffer.append(children.get(i));
buffer.append(i == children.size() - 1 ? "" : ", ");
}

View File

@@ -1,6 +1,5 @@
package org.nwapw.abacus.tree;
import org.nwapw.abacus.number.NaiveNumber;
import org.nwapw.abacus.number.NumberInterface;
/**
@@ -16,20 +15,22 @@ public class NumberNode extends TreeNode {
/**
* Creates a number node with no number.
*/
public NumberNode(){
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){
public NumberNode(NumberInterface newNumber) {
this.number = newNumber;
}
/**
* Gets the number value of this node.
*
* @return the number value of this node.
*/
public NumberInterface getNumber() {

View File

@@ -17,34 +17,35 @@ public class NumberReducer implements Reducer<NumberInterface> {
/**
* Creates a new number reducer.
*
* @param abacus the calculator instance.
*/
public NumberReducer(Abacus abacus){
public NumberReducer(Abacus abacus) {
this.abacus = abacus;
}
@Override
public NumberInterface reduceNode(TreeNode node, Object... children) {
if(node instanceof NumberNode) {
if (node instanceof NumberNode) {
return ((NumberNode) node).getNumber();
} else if(node instanceof BinaryInfixNode){
} else if (node instanceof BinaryInfixNode) {
NumberInterface left = (NumberInterface) children[0];
NumberInterface right = (NumberInterface) children[1];
Function function = abacus.getPluginManager().operatorFor(((BinaryInfixNode) node).getOperation()).getFunction();
if(function == null) return null;
if (function == null) return null;
return function.apply(left, right);
} else if(node instanceof UnaryPrefixNode) {
} else if (node instanceof UnaryPrefixNode) {
NumberInterface child = (NumberInterface) children[0];
Function functionn = abacus.getPluginManager().operatorFor(((UnaryPrefixNode) node).getOperation()).getFunction();
if(functionn == null) return null;
if (functionn == null) return null;
return functionn.apply(child);
} else if(node instanceof FunctionNode){
} else if (node instanceof FunctionNode) {
NumberInterface[] convertedChildren = new NumberInterface[children.length];
for(int i = 0; i < convertedChildren.length; i++){
for (int i = 0; i < convertedChildren.length; i++) {
convertedChildren[i] = (NumberInterface) children[i];
}
Function function = abacus.getPluginManager().functionFor(((FunctionNode) node).getFunction());
if(function == null) return null;
if (function == null) return null;
return function.apply(convertedChildren);
}
return null;

View File

@@ -2,16 +2,18 @@ package org.nwapw.abacus.tree;
/**
* Interface used to reduce a tree into a single value.
*
* @param <T> the value to reduce into.
*/
public interface Reducer<T> {
/**
* Reduces the given tree into a single value of type T.
* @param node the node being passed in to be reduced.
*
* @param node the node being passed in to be reduced.
* @param children the already-reduced children of this node.
* @return the resulting value from the reduce.
*/
public T reduceNode(TreeNode node, Object...children);
public T reduceNode(TreeNode node, Object... children);
}

View File

@@ -16,9 +16,10 @@ public enum TokenType {
/**
* Creates a new token type with the given priority.
*
* @param priority the priority of this token type.
*/
TokenType(int priority){
TokenType(int priority) {
this.priority = priority;
}

View File

@@ -1,11 +1,5 @@
package org.nwapw.abacus.tree;
import org.nwapw.abacus.function.OperatorAssociativity;
import org.nwapw.abacus.lexing.Lexer;
import org.nwapw.abacus.lexing.pattern.Match;
import java.util.*;
/**
* An abstract class that represents an expression tree node.
*/
@@ -13,8 +7,9 @@ public abstract class TreeNode {
/**
* The function that reduces a tree to a single vale.
*
* @param reducer the reducer used to reduce the tree.
* @param <T> the type the reducer produces.
* @param <T> the type the reducer produces.
* @return the result of the reduction, or null on error.
*/
public abstract <T> T reduce(Reducer<T> reducer);

View File

@@ -13,18 +13,20 @@ public class UnaryPrefixNode extends TreeNode {
/**
* Creates a new node with the given operation and no child.
*
* @param operation the operation for this node.
*/
public UnaryPrefixNode(String operation){
public UnaryPrefixNode(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.
* @param applyTo the node to apply the function to.
*/
public UnaryPrefixNode(String operation, TreeNode applyTo){
public UnaryPrefixNode(String operation, TreeNode applyTo) {
this.operation = operation;
this.applyTo = applyTo;
}
@@ -32,12 +34,13 @@ public class UnaryPrefixNode extends TreeNode {
@Override
public <T> T reduce(Reducer<T> reducer) {
Object reducedChild = applyTo.reduce(reducer);
if(reducedChild == null) return null;
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() {
@@ -46,6 +49,7 @@ public class UnaryPrefixNode extends TreeNode {
/**
* Gets the node to which this node's operation applies.
*
* @return the tree node to which the operation will be applied.
*/
public TreeNode getApplyTo() {