From 082b2f87834d6b678c3b75606566a6109d82884e Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 28 Jul 2017 10:26:25 -0700 Subject: [PATCH 1/4] Add operator types. --- src/org/nwapw/abacus/function/Operator.java | 14 +++++++++++++- src/org/nwapw/abacus/function/OperatorType.java | 8 ++++++++ src/org/nwapw/abacus/plugin/StandardPlugin.java | 11 ++++++----- src/org/nwapw/abacus/tree/TreeBuilder.java | 10 +++++++++- src/org/nwapw/abacus/window/Window.java | 5 ++++- 5 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 src/org/nwapw/abacus/function/OperatorType.java diff --git a/src/org/nwapw/abacus/function/Operator.java b/src/org/nwapw/abacus/function/Operator.java index 6d6f7ff..c69f9d2 100644 --- a/src/org/nwapw/abacus/function/Operator.java +++ b/src/org/nwapw/abacus/function/Operator.java @@ -9,6 +9,10 @@ public class Operator { * The associativity of the operator. */ private OperatorAssociativity associativity; + /** + * The type of this operator. + */ + private OperatorType type; /** * The precedence of the operator. */ @@ -24,7 +28,7 @@ public class Operator { * @param precedence the precedence of the operator. * @param function the function that the operator calls. */ - public Operator(OperatorAssociativity associativity, int precedence, Function function){ + public Operator(OperatorAssociativity associativity, OperatorType operatorType, int precedence, Function function){ this.associativity = associativity; this.precedence = precedence; this.function = function; @@ -38,6 +42,14 @@ public class Operator { return associativity; } + /** + * Gets the operator's type. + * @return the type. + */ + public OperatorType getType() { + return type; + } + /** * Gets the operator's precedence. * @return the precedence. diff --git a/src/org/nwapw/abacus/function/OperatorType.java b/src/org/nwapw/abacus/function/OperatorType.java new file mode 100644 index 0000000..d003c07 --- /dev/null +++ b/src/org/nwapw/abacus/function/OperatorType.java @@ -0,0 +1,8 @@ +package org.nwapw.abacus.function; + +/** + * The type of an operator, describing how it should behave. + */ +public enum OperatorType { + BINARY_INFIX, UNARY_POSTFIX +} diff --git a/src/org/nwapw/abacus/plugin/StandardPlugin.java b/src/org/nwapw/abacus/plugin/StandardPlugin.java index 01b988e..4cd13b5 100755 --- a/src/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/src/org/nwapw/abacus/plugin/StandardPlugin.java @@ -3,6 +3,7 @@ package org.nwapw.abacus.plugin; import org.nwapw.abacus.function.Function; import org.nwapw.abacus.function.Operator; import org.nwapw.abacus.function.OperatorAssociativity; +import org.nwapw.abacus.function.OperatorType; import org.nwapw.abacus.number.NaiveNumber; import org.nwapw.abacus.number.NumberInterface; @@ -20,7 +21,7 @@ public class StandardPlugin extends Plugin { @Override public void onEnable() { - registerOperator("+", new Operator(OperatorAssociativity.LEFT, 0, new Function() { + registerOperator("+", new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0, new Function() { @Override protected boolean matchesParams(NumberInterface[] params) { return params.length >= 1; @@ -36,7 +37,7 @@ public class StandardPlugin extends Plugin { } })); - registerOperator("-", new Operator(OperatorAssociativity.LEFT, 0, new Function() { + registerOperator("-", new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0, new Function() { @Override protected boolean matchesParams(NumberInterface[] params) { return params.length == 2; @@ -48,7 +49,7 @@ public class StandardPlugin extends Plugin { } })); - registerOperator("*", new Operator(OperatorAssociativity.LEFT, 1, new Function() { + registerOperator("*", new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX,1, new Function() { @Override protected boolean matchesParams(NumberInterface[] params) { return params.length >= 1; @@ -64,7 +65,7 @@ public class StandardPlugin extends Plugin { } })); - registerOperator("/", new Operator(OperatorAssociativity.LEFT, 1, new Function() { + registerOperator("/", new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX,1, new Function() { @Override protected boolean matchesParams(NumberInterface[] params) { return params.length == 2; @@ -76,7 +77,7 @@ public class StandardPlugin extends Plugin { } })); - registerOperator("^", new Operator(OperatorAssociativity.RIGHT, 2, new Function() { + registerOperator("^", new Operator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 2, new Function() { @Override protected boolean matchesParams(NumberInterface[] params) { return params.length == 2; diff --git a/src/org/nwapw/abacus/tree/TreeBuilder.java b/src/org/nwapw/abacus/tree/TreeBuilder.java index 9c57741..520d3e9 100644 --- a/src/org/nwapw/abacus/tree/TreeBuilder.java +++ b/src/org/nwapw/abacus/tree/TreeBuilder.java @@ -1,6 +1,7 @@ package org.nwapw.abacus.tree; import org.nwapw.abacus.function.OperatorAssociativity; +import org.nwapw.abacus.function.OperatorType; import org.nwapw.abacus.lexing.Lexer; import org.nwapw.abacus.lexing.pattern.Match; import org.nwapw.abacus.lexing.pattern.Pattern; @@ -24,6 +25,10 @@ public class TreeBuilder { * The map of operator associativity. */ private Map associativityMap; + /** + * The map of operator types. + */ + private Map typeMap; /** * Comparator used to sort token types. @@ -43,6 +48,7 @@ public class TreeBuilder { }}; precedenceMap = new HashMap<>(); associativityMap = new HashMap<>(); + typeMap = new HashMap<>(); } /** @@ -59,10 +65,12 @@ public class TreeBuilder { * @param precedence the precedence of the operator. * @param associativity the associativity of the operator. */ - public void registerOperator(String operator, int precedence, OperatorAssociativity associativity){ + public void registerOperator(String operator, OperatorAssociativity associativity, + OperatorType operatorType, int precedence){ lexer.register(Pattern.sanitize(operator), TokenType.OP); precedenceMap.put(operator, precedence); associativityMap.put(operator, associativity); + typeMap.put(operator, operatorType); } /** diff --git a/src/org/nwapw/abacus/window/Window.java b/src/org/nwapw/abacus/window/Window.java index a682e22..0e7a95e 100644 --- a/src/org/nwapw/abacus/window/Window.java +++ b/src/org/nwapw/abacus/window/Window.java @@ -269,7 +269,10 @@ public class Window extends JFrame implements PluginListener { } for(String operator : manager.getAllOperators()){ Operator operatorObject = manager.operatorFor(operator); - builder.registerOperator(operator, operatorObject.getPrecedence(), operatorObject.getAssociativity()); + builder.registerOperator(operator, + operatorObject.getAssociativity(), + operatorObject.getType(), + operatorObject.getPrecedence()); } } From 51f2c15542ef85d02907ed3c8d2678ba36713ed0 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 28 Jul 2017 11:14:45 -0700 Subject: [PATCH 2/4] Implement unary operators. --- src/org/nwapw/abacus/function/Operator.java | 1 + .../nwapw/abacus/plugin/StandardPlugin.java | 4 +- src/org/nwapw/abacus/tree/NumberReducer.java | 5 ++ src/org/nwapw/abacus/tree/TreeBuilder.java | 22 ++++++-- .../nwapw/abacus/tree/UnaryPrefixNode.java | 54 +++++++++++++++++++ 5 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 src/org/nwapw/abacus/tree/UnaryPrefixNode.java diff --git a/src/org/nwapw/abacus/function/Operator.java b/src/org/nwapw/abacus/function/Operator.java index c69f9d2..5b1d2b2 100644 --- a/src/org/nwapw/abacus/function/Operator.java +++ b/src/org/nwapw/abacus/function/Operator.java @@ -30,6 +30,7 @@ public class Operator { */ public Operator(OperatorAssociativity associativity, OperatorType operatorType, int precedence, Function function){ this.associativity = associativity; + this.type = operatorType; this.precedence = precedence; this.function = function; } diff --git a/src/org/nwapw/abacus/plugin/StandardPlugin.java b/src/org/nwapw/abacus/plugin/StandardPlugin.java index 4cd13b5..68a733e 100755 --- a/src/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/src/org/nwapw/abacus/plugin/StandardPlugin.java @@ -89,7 +89,7 @@ public class StandardPlugin extends Plugin { } })); - registerFunction("!", new Function() { + registerOperator("!", new Operator(OperatorAssociativity.RIGHT, OperatorType.UNARY_POSTFIX, 0, new Function() { //private HashMap, ArrayList> storedList = new HashMap, ArrayList>(); @Override protected boolean matchesParams(NumberInterface[] params) { @@ -114,7 +114,7 @@ public class StandardPlugin extends Plugin { storedList.get(params[0].getClass()).add(NaiveNumber.ONE.promoteTo(params[0].getClass())); }*/ } - }); + })); registerFunction("abs", new Function() { @Override diff --git a/src/org/nwapw/abacus/tree/NumberReducer.java b/src/org/nwapw/abacus/tree/NumberReducer.java index 6e4bee8..f447589 100644 --- a/src/org/nwapw/abacus/tree/NumberReducer.java +++ b/src/org/nwapw/abacus/tree/NumberReducer.java @@ -33,6 +33,11 @@ public class NumberReducer implements Reducer { Function function = manager.operatorFor(((OpNode) node).getOperation()).getFunction(); if(function == null) return null; 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){ NumberInterface[] convertedChildren = new NumberInterface[children.length]; for(int i = 0; i < convertedChildren.length; i++){ diff --git a/src/org/nwapw/abacus/tree/TreeBuilder.java b/src/org/nwapw/abacus/tree/TreeBuilder.java index 520d3e9..eabc565 100644 --- a/src/org/nwapw/abacus/tree/TreeBuilder.java +++ b/src/org/nwapw/abacus/tree/TreeBuilder.java @@ -101,9 +101,15 @@ public class TreeBuilder { tokenStack.push(match); } else if(matchType == TokenType.OP){ String tokenString = source.substring(match.getFrom(), match.getTo()); + OperatorType type = typeMap.get(tokenString); int precedence = precedenceMap.get(tokenString); OperatorAssociativity associativity = associativityMap.get(tokenString); + if(type == OperatorType.UNARY_POSTFIX){ + output.add(match); + continue; + } + while(!tokenStack.empty()) { Match otherMatch = tokenStack.peek(); TokenType otherMatchType = otherMatch.getType(); @@ -151,10 +157,18 @@ public class TreeBuilder { Match match = matches.remove(0); TokenType matchType = match.getType(); if(matchType == TokenType.OP){ - TreeNode right = fromStringRecursive(source, matches); - TreeNode left = fromStringRecursive(source, matches); - if(left == null || right == null) return null; - else return new OpNode(source.substring(match.getFrom(), match.getTo()), left, right); + String operator = source.substring(match.getFrom(), match.getTo()); + OperatorType type = typeMap.get(operator); + if(type == OperatorType.BINARY_INFIX){ + TreeNode right = fromStringRecursive(source, matches); + TreeNode left = fromStringRecursive(source, matches); + if(left == null || right == null) return null; + else return new OpNode(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){ return new NumberNode(Double.parseDouble(source.substring(match.getFrom(), match.getTo()))); } else if(matchType == TokenType.FUNCTION){ diff --git a/src/org/nwapw/abacus/tree/UnaryPrefixNode.java b/src/org/nwapw/abacus/tree/UnaryPrefixNode.java new file mode 100644 index 0000000..944e339 --- /dev/null +++ b/src/org/nwapw/abacus/tree/UnaryPrefixNode.java @@ -0,0 +1,54 @@ +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 reduce(Reducer 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; + } +} From c1bf308d6dd1a6cd68fdb117a143270598a65f29 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 28 Jul 2017 11:15:36 -0700 Subject: [PATCH 3/4] Rename OpNode to BinaryInfixNode. --- .../abacus/tree/{OpNode.java => BinaryInfixNode.java} | 8 ++++---- src/org/nwapw/abacus/tree/NumberReducer.java | 4 ++-- src/org/nwapw/abacus/tree/TreeBuilder.java | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) rename src/org/nwapw/abacus/tree/{OpNode.java => BinaryInfixNode.java} (91%) diff --git a/src/org/nwapw/abacus/tree/OpNode.java b/src/org/nwapw/abacus/tree/BinaryInfixNode.java similarity index 91% rename from src/org/nwapw/abacus/tree/OpNode.java rename to src/org/nwapw/abacus/tree/BinaryInfixNode.java index 475affc..f10913b 100644 --- a/src/org/nwapw/abacus/tree/OpNode.java +++ b/src/org/nwapw/abacus/tree/BinaryInfixNode.java @@ -3,7 +3,7 @@ package org.nwapw.abacus.tree; /** * A tree node that represents an operation being applied to two operands. */ -public class OpNode extends TreeNode { +public class BinaryInfixNode extends TreeNode { /** * The operation being applied. @@ -18,14 +18,14 @@ public class OpNode extends TreeNode { */ private TreeNode right; - private OpNode() {} + private BinaryInfixNode() {} /** * Creates a new operation node with the given operation * and no child nodes. * @param operation the operation. */ - public OpNode(String operation){ + public BinaryInfixNode(String operation){ this(operation, null, null); } @@ -36,7 +36,7 @@ public class OpNode extends TreeNode { * @param left the left node of the expression. * @param right the right node of the expression. */ - public OpNode(String operation, TreeNode left, TreeNode right){ + public BinaryInfixNode(String operation, TreeNode left, TreeNode right){ this.operation = operation; this.left = left; this.right = right; diff --git a/src/org/nwapw/abacus/tree/NumberReducer.java b/src/org/nwapw/abacus/tree/NumberReducer.java index f447589..bb10a5e 100644 --- a/src/org/nwapw/abacus/tree/NumberReducer.java +++ b/src/org/nwapw/abacus/tree/NumberReducer.java @@ -27,10 +27,10 @@ public class NumberReducer implements Reducer { public NumberInterface reduceNode(TreeNode node, Object... children) { if(node instanceof NumberNode) { return ((NumberNode) node).getNumber(); - } else if(node instanceof OpNode){ + } else if(node instanceof BinaryInfixNode){ NumberInterface left = (NumberInterface) children[0]; NumberInterface right = (NumberInterface) children[1]; - Function function = manager.operatorFor(((OpNode) node).getOperation()).getFunction(); + Function function = manager.operatorFor(((BinaryInfixNode) node).getOperation()).getFunction(); if(function == null) return null; return function.apply(left, right); } else if(node instanceof UnaryPrefixNode) { diff --git a/src/org/nwapw/abacus/tree/TreeBuilder.java b/src/org/nwapw/abacus/tree/TreeBuilder.java index eabc565..10663d7 100644 --- a/src/org/nwapw/abacus/tree/TreeBuilder.java +++ b/src/org/nwapw/abacus/tree/TreeBuilder.java @@ -163,7 +163,7 @@ public class TreeBuilder { TreeNode right = fromStringRecursive(source, matches); TreeNode left = fromStringRecursive(source, matches); if(left == null || right == null) return null; - else return new OpNode(operator, left, right); + else return new BinaryInfixNode(operator, left, right); } else { TreeNode applyTo = fromStringRecursive(source, matches); if(applyTo == null) return null; From 2fa51fc99737118060a04ba5078f36b3c9a2adca Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 28 Jul 2017 11:19:41 -0700 Subject: [PATCH 4/4] Fix null pointer exceptions associated with turning ! into an operator. --- src/org/nwapw/abacus/plugin/StandardPlugin.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/nwapw/abacus/plugin/StandardPlugin.java b/src/org/nwapw/abacus/plugin/StandardPlugin.java index 68a733e..a6b6766 100755 --- a/src/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/src/org/nwapw/abacus/plugin/StandardPlugin.java @@ -246,7 +246,7 @@ public class StandardPlugin extends Plugin { * @return the nth term of the series. */ private NumberInterface getExpSeriesTerm(int n, NumberInterface x){ - return x.intPow(n).divide(this.getFunction("!").apply((new NaiveNumber(n)).promoteTo(x.getClass()))); + return x.intPow(n).divide(this.getOperator("!").getFunction().apply((new NaiveNumber(n)).promoteTo(x.getClass()))); } /**