From 082b2f87834d6b678c3b75606566a6109d82884e Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 28 Jul 2017 10:26:25 -0700 Subject: [PATCH] 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()); } }