mirror of
https://github.com/DanilaFe/abacus
synced 2024-12-23 16:00:09 -08:00
Implement the negation operator.
This commit is contained in:
parent
31a4234d9b
commit
046764eeca
|
@ -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;
|
||||||
|
|
|
@ -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, *
|
||||||
*/
|
*/
|
||||||
|
@ -317,6 +331,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);
|
||||||
|
@ -335,7 +350,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));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user