mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-26 00:25:20 +00:00
Compare commits
1 Commits
unary
...
big-decima
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c184b55738 |
@@ -9,10 +9,6 @@ public class Operator {
|
|||||||
* The associativity of the operator.
|
* The associativity of the operator.
|
||||||
*/
|
*/
|
||||||
private OperatorAssociativity associativity;
|
private OperatorAssociativity associativity;
|
||||||
/**
|
|
||||||
* The type of this operator.
|
|
||||||
*/
|
|
||||||
private OperatorType type;
|
|
||||||
/**
|
/**
|
||||||
* The precedence of the operator.
|
* The precedence of the operator.
|
||||||
*/
|
*/
|
||||||
@@ -28,9 +24,8 @@ public class Operator {
|
|||||||
* @param precedence the precedence of the operator.
|
* @param precedence the precedence of the operator.
|
||||||
* @param function the function that the operator calls.
|
* @param function the function that the operator calls.
|
||||||
*/
|
*/
|
||||||
public Operator(OperatorAssociativity associativity, OperatorType operatorType, int precedence, Function function){
|
public Operator(OperatorAssociativity associativity, int precedence, Function function){
|
||||||
this.associativity = associativity;
|
this.associativity = associativity;
|
||||||
this.type = operatorType;
|
|
||||||
this.precedence = precedence;
|
this.precedence = precedence;
|
||||||
this.function = function;
|
this.function = function;
|
||||||
}
|
}
|
||||||
@@ -43,14 +38,6 @@ public class Operator {
|
|||||||
return associativity;
|
return associativity;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the operator's type.
|
|
||||||
* @return the type.
|
|
||||||
*/
|
|
||||||
public OperatorType getType() {
|
|
||||||
return type;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the operator's precedence.
|
* Gets the operator's precedence.
|
||||||
* @return the precedence.
|
* @return the precedence.
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
package org.nwapw.abacus.function;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The type of an operator, describing how it should behave.
|
|
||||||
*/
|
|
||||||
public enum OperatorType {
|
|
||||||
BINARY_INFIX, UNARY_POSTFIX
|
|
||||||
}
|
|
||||||
@@ -88,6 +88,9 @@ public class NaiveNumber implements NumberInterface {
|
|||||||
@Override
|
@Override
|
||||||
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
|
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
|
||||||
if(toClass == this.getClass()) return this;
|
if(toClass == this.getClass()) return this;
|
||||||
|
else if(toClass == PreciseNumber.class){
|
||||||
|
return new PreciseNumber(Double.toString(value));
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
112
src/org/nwapw/abacus/number/PreciseNumber.java
Executable file
112
src/org/nwapw/abacus/number/PreciseNumber.java
Executable file
@@ -0,0 +1,112 @@
|
|||||||
|
package org.nwapw.abacus.number;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.math.BigInteger;
|
||||||
|
import java.math.MathContext;
|
||||||
|
import java.math.RoundingMode;
|
||||||
|
|
||||||
|
public class PreciseNumber implements NumberInterface{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The number one.
|
||||||
|
*/
|
||||||
|
static final PreciseNumber ONE = new PreciseNumber(BigDecimal.ONE);
|
||||||
|
/**
|
||||||
|
* The number zero.
|
||||||
|
*/
|
||||||
|
static final PreciseNumber ZERO = new PreciseNumber(BigDecimal.ZERO);
|
||||||
|
/**
|
||||||
|
* The number ten.
|
||||||
|
*/
|
||||||
|
static final PreciseNumber TEN = new PreciseNumber(BigDecimal.TEN);
|
||||||
|
|
||||||
|
BigDecimal value = new BigDecimal("0");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a precise number from the given string.
|
||||||
|
* @param string a string representation of the number meeting the same conditions
|
||||||
|
* as the BidDecimal(String) constructor.
|
||||||
|
*/
|
||||||
|
public PreciseNumber(String string){
|
||||||
|
value = new BigDecimal(string);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a precise number from the given BigDecimal.
|
||||||
|
* @param value a BigDecimal object representing the value of the number.
|
||||||
|
*/
|
||||||
|
public PreciseNumber(BigDecimal value){
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int precision() {
|
||||||
|
return 54;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface multiply(NumberInterface multiplier) {
|
||||||
|
return new PreciseNumber(value.multiply(((PreciseNumber) multiplier).value));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface divide(NumberInterface divisor) {
|
||||||
|
return new PreciseNumber(value.divide(((PreciseNumber) divisor).value, this.precision(), RoundingMode.HALF_UP));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface add(NumberInterface summand) {
|
||||||
|
return new PreciseNumber(value.add(((PreciseNumber) summand).value));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface subtract(NumberInterface subtrahend) {
|
||||||
|
return new PreciseNumber(value.subtract(((PreciseNumber) subtrahend).value));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface intPow(int exponent) {
|
||||||
|
if(exponent == 0){
|
||||||
|
return PreciseNumber.ONE;
|
||||||
|
}
|
||||||
|
boolean takeReciprocal = exponent < 0;
|
||||||
|
exponent = Math.abs(exponent);
|
||||||
|
NumberInterface power = this;
|
||||||
|
for(int currentExponent = 1; currentExponent < exponent; currentExponent++){
|
||||||
|
power = power.multiply(this);
|
||||||
|
}
|
||||||
|
if(takeReciprocal){
|
||||||
|
power = PreciseNumber.ONE.divide(power);
|
||||||
|
}
|
||||||
|
return power;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(NumberInterface number) {
|
||||||
|
return value.compareTo(((PreciseNumber) number).value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int signum() {
|
||||||
|
return value.signum();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface negate(){
|
||||||
|
return new PreciseNumber(value.negate());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
|
||||||
|
if(toClass == this.getClass()){
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
BigDecimal rounded = value.setScale(precision() - 4, RoundingMode.HALF_UP);
|
||||||
|
return rounded.stripTrailingZeros().toPlainString();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,6 @@ package org.nwapw.abacus.plugin;
|
|||||||
import org.nwapw.abacus.function.Function;
|
import org.nwapw.abacus.function.Function;
|
||||||
import org.nwapw.abacus.function.Operator;
|
import org.nwapw.abacus.function.Operator;
|
||||||
import org.nwapw.abacus.function.OperatorAssociativity;
|
import org.nwapw.abacus.function.OperatorAssociativity;
|
||||||
import org.nwapw.abacus.function.OperatorType;
|
|
||||||
import org.nwapw.abacus.number.NaiveNumber;
|
import org.nwapw.abacus.number.NaiveNumber;
|
||||||
import org.nwapw.abacus.number.NumberInterface;
|
import org.nwapw.abacus.number.NumberInterface;
|
||||||
|
|
||||||
@@ -21,7 +20,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
registerOperator("+", new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0, new Function() {
|
registerOperator("+", new Operator(OperatorAssociativity.LEFT, 0, new Function() {
|
||||||
@Override
|
@Override
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
return params.length >= 1;
|
return params.length >= 1;
|
||||||
@@ -37,7 +36,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
registerOperator("-", new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0, new Function() {
|
registerOperator("-", new Operator(OperatorAssociativity.LEFT, 0, new Function() {
|
||||||
@Override
|
@Override
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
return params.length == 2;
|
return params.length == 2;
|
||||||
@@ -49,7 +48,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
registerOperator("*", new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX,1, new Function() {
|
registerOperator("*", new Operator(OperatorAssociativity.LEFT, 1, new Function() {
|
||||||
@Override
|
@Override
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
return params.length >= 1;
|
return params.length >= 1;
|
||||||
@@ -65,7 +64,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
registerOperator("/", new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX,1, new Function() {
|
registerOperator("/", new Operator(OperatorAssociativity.LEFT, 1, new Function() {
|
||||||
@Override
|
@Override
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
return params.length == 2;
|
return params.length == 2;
|
||||||
@@ -77,7 +76,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
registerOperator("^", new Operator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 2, new Function() {
|
registerOperator("^", new Operator(OperatorAssociativity.RIGHT, 2, new Function() {
|
||||||
@Override
|
@Override
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
return params.length == 2;
|
return params.length == 2;
|
||||||
@@ -89,7 +88,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
registerOperator("!", new Operator(OperatorAssociativity.RIGHT, OperatorType.UNARY_POSTFIX, 0, new Function() {
|
registerFunction("!", new Function() {
|
||||||
//private HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>> storedList = new HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>>();
|
//private HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>> storedList = new HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>>();
|
||||||
@Override
|
@Override
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
@@ -114,7 +113,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
storedList.get(params[0].getClass()).add(NaiveNumber.ONE.promoteTo(params[0].getClass()));
|
storedList.get(params[0].getClass()).add(NaiveNumber.ONE.promoteTo(params[0].getClass()));
|
||||||
}*/
|
}*/
|
||||||
}
|
}
|
||||||
}));
|
});
|
||||||
|
|
||||||
registerFunction("abs", new Function() {
|
registerFunction("abs", new Function() {
|
||||||
@Override
|
@Override
|
||||||
@@ -229,7 +228,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
return StandardPlugin.this.getOperator("^").getFunction().apply(params[0], (new NaiveNumber(0.5)));
|
return StandardPlugin.this.getOperator("^").getFunction().apply(params[0], ((new NaiveNumber(0.5)).promoteTo(params[0].getClass())));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -246,7 +245,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
* @return the nth term of the series.
|
* @return the nth term of the series.
|
||||||
*/
|
*/
|
||||||
private NumberInterface getExpSeriesTerm(int n, NumberInterface x){
|
private NumberInterface getExpSeriesTerm(int n, NumberInterface x){
|
||||||
return x.intPow(n).divide(this.getOperator("!").getFunction().apply((new NaiveNumber(n)).promoteTo(x.getClass())));
|
return x.intPow(n).divide(this.getFunction("!").apply((new NaiveNumber(n)).promoteTo(x.getClass())));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -27,17 +27,12 @@ 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 OpNode){
|
||||||
NumberInterface left = (NumberInterface) children[0];
|
NumberInterface left = (NumberInterface) children[0];
|
||||||
NumberInterface right = (NumberInterface) children[1];
|
NumberInterface right = (NumberInterface) children[1];
|
||||||
Function function = manager.operatorFor(((BinaryInfixNode) node).getOperation()).getFunction();
|
Function function = manager.operatorFor(((OpNode) 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) {
|
|
||||||
NumberInterface child = (NumberInterface) children[0];
|
|
||||||
Function functionn = manager.operatorFor(((UnaryPrefixNode) node).getOperation()).getFunction();
|
|
||||||
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];
|
NumberInterface[] convertedChildren = new NumberInterface[children.length];
|
||||||
for(int i = 0; i < convertedChildren.length; i++){
|
for(int i = 0; i < convertedChildren.length; i++){
|
||||||
|
|||||||
@@ -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 OpNode extends TreeNode {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The operation being applied.
|
* The operation being applied.
|
||||||
@@ -18,14 +18,14 @@ public class BinaryInfixNode extends TreeNode {
|
|||||||
*/
|
*/
|
||||||
private TreeNode right;
|
private TreeNode right;
|
||||||
|
|
||||||
private BinaryInfixNode() {}
|
private OpNode() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new operation node with the given operation
|
* Creates a new operation node with the given operation
|
||||||
* and no child nodes.
|
* and no child nodes.
|
||||||
* @param operation the operation.
|
* @param operation the operation.
|
||||||
*/
|
*/
|
||||||
public BinaryInfixNode(String operation){
|
public OpNode(String operation){
|
||||||
this(operation, null, null);
|
this(operation, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,7 +36,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 OpNode(String operation, TreeNode left, TreeNode right){
|
||||||
this.operation = operation;
|
this.operation = operation;
|
||||||
this.left = left;
|
this.left = left;
|
||||||
this.right = right;
|
this.right = right;
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package org.nwapw.abacus.tree;
|
package org.nwapw.abacus.tree;
|
||||||
|
|
||||||
import org.nwapw.abacus.function.OperatorAssociativity;
|
import org.nwapw.abacus.function.OperatorAssociativity;
|
||||||
import org.nwapw.abacus.function.OperatorType;
|
|
||||||
import org.nwapw.abacus.lexing.Lexer;
|
import org.nwapw.abacus.lexing.Lexer;
|
||||||
import org.nwapw.abacus.lexing.pattern.Match;
|
import org.nwapw.abacus.lexing.pattern.Match;
|
||||||
import org.nwapw.abacus.lexing.pattern.Pattern;
|
import org.nwapw.abacus.lexing.pattern.Pattern;
|
||||||
@@ -25,10 +24,6 @@ public class TreeBuilder {
|
|||||||
* The map of operator associativity.
|
* The map of operator associativity.
|
||||||
*/
|
*/
|
||||||
private Map<String, OperatorAssociativity> associativityMap;
|
private Map<String, OperatorAssociativity> associativityMap;
|
||||||
/**
|
|
||||||
* The map of operator types.
|
|
||||||
*/
|
|
||||||
private Map<String, OperatorType> typeMap;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Comparator used to sort token types.
|
* Comparator used to sort token types.
|
||||||
@@ -48,7 +43,6 @@ public class TreeBuilder {
|
|||||||
}};
|
}};
|
||||||
precedenceMap = new HashMap<>();
|
precedenceMap = new HashMap<>();
|
||||||
associativityMap = new HashMap<>();
|
associativityMap = new HashMap<>();
|
||||||
typeMap = new HashMap<>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -65,12 +59,10 @@ public class TreeBuilder {
|
|||||||
* @param precedence the precedence of the operator.
|
* @param precedence the precedence of the operator.
|
||||||
* @param associativity the associativity of the operator.
|
* @param associativity the associativity of the operator.
|
||||||
*/
|
*/
|
||||||
public void registerOperator(String operator, OperatorAssociativity associativity,
|
public void registerOperator(String operator, int precedence, OperatorAssociativity associativity){
|
||||||
OperatorType operatorType, int precedence){
|
|
||||||
lexer.register(Pattern.sanitize(operator), TokenType.OP);
|
lexer.register(Pattern.sanitize(operator), TokenType.OP);
|
||||||
precedenceMap.put(operator, precedence);
|
precedenceMap.put(operator, precedence);
|
||||||
associativityMap.put(operator, associativity);
|
associativityMap.put(operator, associativity);
|
||||||
typeMap.put(operator, operatorType);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -101,15 +93,9 @@ public class TreeBuilder {
|
|||||||
tokenStack.push(match);
|
tokenStack.push(match);
|
||||||
} else if(matchType == TokenType.OP){
|
} else if(matchType == TokenType.OP){
|
||||||
String tokenString = source.substring(match.getFrom(), match.getTo());
|
String tokenString = source.substring(match.getFrom(), match.getTo());
|
||||||
OperatorType type = typeMap.get(tokenString);
|
|
||||||
int precedence = precedenceMap.get(tokenString);
|
int precedence = precedenceMap.get(tokenString);
|
||||||
OperatorAssociativity associativity = associativityMap.get(tokenString);
|
OperatorAssociativity associativity = associativityMap.get(tokenString);
|
||||||
|
|
||||||
if(type == OperatorType.UNARY_POSTFIX){
|
|
||||||
output.add(match);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
while(!tokenStack.empty()) {
|
while(!tokenStack.empty()) {
|
||||||
Match<TokenType> otherMatch = tokenStack.peek();
|
Match<TokenType> otherMatch = tokenStack.peek();
|
||||||
TokenType otherMatchType = otherMatch.getType();
|
TokenType otherMatchType = otherMatch.getType();
|
||||||
@@ -157,18 +143,10 @@ public class TreeBuilder {
|
|||||||
Match<TokenType> match = matches.remove(0);
|
Match<TokenType> match = matches.remove(0);
|
||||||
TokenType matchType = match.getType();
|
TokenType matchType = match.getType();
|
||||||
if(matchType == TokenType.OP){
|
if(matchType == TokenType.OP){
|
||||||
String operator = source.substring(match.getFrom(), match.getTo());
|
TreeNode right = fromStringRecursive(source, matches);
|
||||||
OperatorType type = typeMap.get(operator);
|
TreeNode left = fromStringRecursive(source, matches);
|
||||||
if(type == OperatorType.BINARY_INFIX){
|
if(left == null || right == null) return null;
|
||||||
TreeNode right = fromStringRecursive(source, matches);
|
else return new OpNode(source.substring(match.getFrom(), match.getTo()), left, right);
|
||||||
TreeNode left = fromStringRecursive(source, matches);
|
|
||||||
if(left == null || right == null) return null;
|
|
||||||
else return new BinaryInfixNode(operator, left, right);
|
|
||||||
} else {
|
|
||||||
TreeNode applyTo = fromStringRecursive(source, matches);
|
|
||||||
if(applyTo == null) return null;
|
|
||||||
else return new UnaryPrefixNode(operator, applyTo);
|
|
||||||
}
|
|
||||||
} else if(matchType == TokenType.NUM){
|
} else if(matchType == TokenType.NUM){
|
||||||
return new NumberNode(Double.parseDouble(source.substring(match.getFrom(), match.getTo())));
|
return new NumberNode(Double.parseDouble(source.substring(match.getFrom(), match.getTo())));
|
||||||
} else if(matchType == TokenType.FUNCTION){
|
} else if(matchType == TokenType.FUNCTION){
|
||||||
|
|||||||
@@ -1,54 +0,0 @@
|
|||||||
package org.nwapw.abacus.tree;
|
|
||||||
|
|
||||||
public class UnaryPrefixNode extends TreeNode {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The operation this node will apply.
|
|
||||||
*/
|
|
||||||
private String operation;
|
|
||||||
/**
|
|
||||||
* The tree node to apply the operation to.
|
|
||||||
*/
|
|
||||||
private TreeNode applyTo;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new node with the given operation and no child.
|
|
||||||
* @param operation the operation for this node.
|
|
||||||
*/
|
|
||||||
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.
|
|
||||||
*/
|
|
||||||
public UnaryPrefixNode(String operation, TreeNode applyTo){
|
|
||||||
this.operation = operation;
|
|
||||||
this.applyTo = applyTo;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T> T reduce(Reducer<T> reducer) {
|
|
||||||
Object reducedChild = applyTo.reduce(reducer);
|
|
||||||
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() {
|
|
||||||
return operation;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the node to which this node's operation applies.
|
|
||||||
* @return the tree node to which the operation will be applied.
|
|
||||||
*/
|
|
||||||
public TreeNode getApplyTo() {
|
|
||||||
return applyTo;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -269,10 +269,7 @@ public class Window extends JFrame implements PluginListener {
|
|||||||
}
|
}
|
||||||
for(String operator : manager.getAllOperators()){
|
for(String operator : manager.getAllOperators()){
|
||||||
Operator operatorObject = manager.operatorFor(operator);
|
Operator operatorObject = manager.operatorFor(operator);
|
||||||
builder.registerOperator(operator,
|
builder.registerOperator(operator, operatorObject.getPrecedence(), operatorObject.getAssociativity());
|
||||||
operatorObject.getAssociativity(),
|
|
||||||
operatorObject.getType(),
|
|
||||||
operatorObject.getPrecedence());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user