mirror of
https://github.com/DanilaFe/abacus
synced 2024-09-07 00:03:05 -07:00
Merge branch 'negatives'
This commit is contained in:
commit
ecc5f6d2bd
|
@ -4,5 +4,5 @@ package org.nwapw.abacus.function;
|
||||||
* The type of an operator, describing how it should behave.
|
* The type of an operator, describing how it should behave.
|
||||||
*/
|
*/
|
||||||
public enum OperatorType {
|
public enum OperatorType {
|
||||||
BINARY_INFIX, UNARY_POSTFIX
|
BINARY_INFIX, UNARY_POSTFIX, UNARY_PREFIX
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,9 +55,12 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
||||||
public List<Match<TokenType>> intoPostfix(List<Match<TokenType>> from) {
|
public List<Match<TokenType>> intoPostfix(List<Match<TokenType>> from) {
|
||||||
ArrayList<Match<TokenType>> output = new ArrayList<>();
|
ArrayList<Match<TokenType>> output = new ArrayList<>();
|
||||||
Stack<Match<TokenType>> tokenStack = new Stack<>();
|
Stack<Match<TokenType>> tokenStack = new Stack<>();
|
||||||
|
TokenType previousType;
|
||||||
|
TokenType matchType = null;
|
||||||
while (!from.isEmpty()) {
|
while (!from.isEmpty()) {
|
||||||
Match<TokenType> match = from.remove(0);
|
Match<TokenType> match = from.remove(0);
|
||||||
TokenType matchType = match.getType();
|
previousType = matchType;
|
||||||
|
matchType = match.getType();
|
||||||
if (matchType == TokenType.NUM) {
|
if (matchType == TokenType.NUM) {
|
||||||
output.add(match);
|
output.add(match);
|
||||||
} else if (matchType == TokenType.FUNCTION) {
|
} else if (matchType == TokenType.FUNCTION) {
|
||||||
|
@ -74,7 +77,13 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!tokenStack.empty()) {
|
if(tokenString.equals("-") && (previousType == null || previousType == TokenType.OP ||
|
||||||
|
previousType == TokenType.OPEN_PARENTH)){
|
||||||
|
from.add(0, new Match<>("`", TokenType.OP));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!tokenStack.empty() && type == OperatorType.BINARY_INFIX) {
|
||||||
Match<TokenType> otherMatch = tokenStack.peek();
|
Match<TokenType> otherMatch = tokenStack.peek();
|
||||||
TokenType otherMatchType = otherMatch.getType();
|
TokenType otherMatchType = otherMatch.getType();
|
||||||
if (!(otherMatchType == TokenType.OP || otherMatchType == TokenType.FUNCTION)) break;
|
if (!(otherMatchType == TokenType.OP || otherMatchType == TokenType.FUNCTION)) break;
|
||||||
|
@ -103,8 +112,8 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
||||||
}
|
}
|
||||||
while (!tokenStack.empty()) {
|
while (!tokenStack.empty()) {
|
||||||
Match<TokenType> match = tokenStack.peek();
|
Match<TokenType> match = tokenStack.peek();
|
||||||
TokenType matchType = match.getType();
|
TokenType newMatchType = match.getType();
|
||||||
if (!(matchType == TokenType.OP || matchType == TokenType.FUNCTION)) return null;
|
if (!(newMatchType == TokenType.OP || newMatchType == TokenType.FUNCTION)) return null;
|
||||||
output.add(tokenStack.pop());
|
output.add(tokenStack.pop());
|
||||||
}
|
}
|
||||||
return output;
|
return output;
|
||||||
|
@ -127,11 +136,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 BinaryInfixNode(operator, left, right);
|
else return new BinaryNode(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 UnaryPrefixNode(operator, applyTo);
|
else return new UnaryNode(operator, applyTo);
|
||||||
}
|
}
|
||||||
} else if (matchType == TokenType.NUM) {
|
} else if (matchType == TokenType.NUM) {
|
||||||
return new NumberNode(abacus.numberFromString(match.getContent()));
|
return new NumberNode(abacus.numberFromString(match.getContent()));
|
||||||
|
|
|
@ -52,6 +52,20 @@ public class StandardPlugin extends Plugin {
|
||||||
return params[0].subtract(params[1]);
|
return params[0].subtract(params[1]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
/**
|
||||||
|
* The negation operator, -
|
||||||
|
*/
|
||||||
|
public static final Operator OP_NEGATE = new Operator(OperatorAssociativity.LEFT, OperatorType.UNARY_PREFIX, 0, new Function() {
|
||||||
|
@Override
|
||||||
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
|
return params.length == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
return params[0].negate();
|
||||||
|
}
|
||||||
|
});
|
||||||
/**
|
/**
|
||||||
* The multiplication operator, *
|
* The multiplication operator, *
|
||||||
*/
|
*/
|
||||||
|
@ -313,6 +327,7 @@ public class StandardPlugin extends Plugin {
|
||||||
|
|
||||||
registerOperator("+", OP_ADD);
|
registerOperator("+", OP_ADD);
|
||||||
registerOperator("-", OP_SUBTRACT);
|
registerOperator("-", OP_SUBTRACT);
|
||||||
|
registerOperator("`", OP_NEGATE);
|
||||||
registerOperator("*", OP_MULTIPLY);
|
registerOperator("*", OP_MULTIPLY);
|
||||||
registerOperator("/", OP_DIVIDE);
|
registerOperator("/", OP_DIVIDE);
|
||||||
registerOperator("^", OP_CARET);
|
registerOperator("^", OP_CARET);
|
||||||
|
@ -331,7 +346,7 @@ public class StandardPlugin extends Plugin {
|
||||||
|
|
||||||
public static NumberInterface factorial(Class<? extends NumberInterface> numberClass, int n){
|
public static NumberInterface factorial(Class<? extends NumberInterface> numberClass, int n){
|
||||||
if(!factorialLists.containsKey(numberClass)){
|
if(!factorialLists.containsKey(numberClass)){
|
||||||
factorialLists.put(numberClass, new ArrayList<NumberInterface>());
|
factorialLists.put(numberClass, new ArrayList<>());
|
||||||
factorialLists.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
factorialLists.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
||||||
factorialLists.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
factorialLists.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ package org.nwapw.abacus.tree;
|
||||||
/**
|
/**
|
||||||
* A tree node that represents an operation being applied to two operands.
|
* A tree node that represents an operation being applied to two operands.
|
||||||
*/
|
*/
|
||||||
public class BinaryInfixNode extends TreeNode {
|
public class BinaryNode extends TreeNode {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The operation being applied.
|
* The operation being applied.
|
||||||
|
@ -18,7 +18,7 @@ public class BinaryInfixNode extends TreeNode {
|
||||||
*/
|
*/
|
||||||
private TreeNode right;
|
private TreeNode right;
|
||||||
|
|
||||||
private BinaryInfixNode() {
|
private BinaryNode() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -27,7 +27,7 @@ public class BinaryInfixNode extends TreeNode {
|
||||||
*
|
*
|
||||||
* @param operation the operation.
|
* @param operation the operation.
|
||||||
*/
|
*/
|
||||||
public BinaryInfixNode(String operation) {
|
public BinaryNode(String operation) {
|
||||||
this(operation, null, null);
|
this(operation, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ public class BinaryInfixNode extends TreeNode {
|
||||||
* @param left the left node of the expression.
|
* @param left the left node of the expression.
|
||||||
* @param right the right node of the expression.
|
* @param right the right node of the expression.
|
||||||
*/
|
*/
|
||||||
public BinaryInfixNode(String operation, TreeNode left, TreeNode right) {
|
public BinaryNode(String operation, TreeNode left, TreeNode right) {
|
||||||
this.operation = operation;
|
this.operation = operation;
|
||||||
this.left = left;
|
this.left = left;
|
||||||
this.right = right;
|
this.right = right;
|
|
@ -28,15 +28,15 @@ public class NumberReducer implements Reducer<NumberInterface> {
|
||||||
public NumberInterface reduceNode(TreeNode node, Object... children) {
|
public NumberInterface reduceNode(TreeNode node, Object... children) {
|
||||||
if (node instanceof NumberNode) {
|
if (node instanceof NumberNode) {
|
||||||
return ((NumberNode) node).getNumber();
|
return ((NumberNode) node).getNumber();
|
||||||
} else if (node instanceof BinaryInfixNode) {
|
} else if (node instanceof BinaryNode) {
|
||||||
NumberInterface left = (NumberInterface) children[0];
|
NumberInterface left = (NumberInterface) children[0];
|
||||||
NumberInterface right = (NumberInterface) children[1];
|
NumberInterface right = (NumberInterface) children[1];
|
||||||
Function function = abacus.getPluginManager().operatorFor(((BinaryInfixNode) node).getOperation()).getFunction();
|
Function function = abacus.getPluginManager().operatorFor(((BinaryNode) node).getOperation()).getFunction();
|
||||||
if (function == null) return null;
|
if (function == null) return null;
|
||||||
return function.apply(left, right);
|
return function.apply(left, right);
|
||||||
} else if (node instanceof UnaryPrefixNode) {
|
} else if (node instanceof UnaryNode) {
|
||||||
NumberInterface child = (NumberInterface) children[0];
|
NumberInterface child = (NumberInterface) children[0];
|
||||||
Function functionn = abacus.getPluginManager().operatorFor(((UnaryPrefixNode) node).getOperation()).getFunction();
|
Function functionn = abacus.getPluginManager().operatorFor(((UnaryNode) node).getOperation()).getFunction();
|
||||||
if (functionn == null) return null;
|
if (functionn == null) return null;
|
||||||
return functionn.apply(child);
|
return functionn.apply(child);
|
||||||
} else if (node instanceof FunctionNode) {
|
} else if (node instanceof FunctionNode) {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package org.nwapw.abacus.tree;
|
package org.nwapw.abacus.tree;
|
||||||
|
|
||||||
public class UnaryPrefixNode extends TreeNode {
|
public class UnaryNode extends TreeNode {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The operation this node will apply.
|
* The operation this node will apply.
|
||||||
|
@ -16,7 +16,7 @@ public class UnaryPrefixNode extends TreeNode {
|
||||||
*
|
*
|
||||||
* @param operation the operation for this node.
|
* @param operation the operation for this node.
|
||||||
*/
|
*/
|
||||||
public UnaryPrefixNode(String operation) {
|
public UnaryNode(String operation) {
|
||||||
this(operation, null);
|
this(operation, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ public class UnaryPrefixNode extends TreeNode {
|
||||||
* @param operation the operation for this node.
|
* @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 UnaryNode(String operation, TreeNode applyTo) {
|
||||||
this.operation = operation;
|
this.operation = operation;
|
||||||
this.applyTo = applyTo;
|
this.applyTo = applyTo;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user