From 71a2df5f38b9c1c055bdaad5725277c2ca7c1304 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 25 Aug 2017 14:55:05 -0700 Subject: [PATCH] Add Applicable to Operator, therby removing the need for Functions in it --- .../java/org/nwapw/abacus/plugin/Plugin.java | 5 ++- .../nwapw/abacus/plugin/PluginManager.java | 6 +-- .../nwapw/abacus/plugin/StandardPlugin.java | 43 ++++++++++--------- .../org/nwapw/abacus/tree/NumberReducer.java | 12 +++--- .../nwapw/abacus/function/NumberOperator.kt | 15 +++++++ .../org/nwapw/abacus/function/Operator.kt | 7 ++- .../nwapw/abacus/tests/TokenizerTests.java | 35 +++++++++++---- 7 files changed, 80 insertions(+), 43 deletions(-) create mode 100644 core/src/main/kotlin/org/nwapw/abacus/function/NumberOperator.kt diff --git a/core/src/main/java/org/nwapw/abacus/plugin/Plugin.java b/core/src/main/java/org/nwapw/abacus/plugin/Plugin.java index 4b71d56..1e6763f 100644 --- a/core/src/main/java/org/nwapw/abacus/plugin/Plugin.java +++ b/core/src/main/java/org/nwapw/abacus/plugin/Plugin.java @@ -1,6 +1,7 @@ package org.nwapw.abacus.plugin; import org.nwapw.abacus.function.*; +import org.nwapw.abacus.number.NaiveNumber; import org.nwapw.abacus.number.NumberInterface; /** @@ -85,7 +86,7 @@ public abstract class Plugin { * @param name the name of the operator. * @param operator the operator to register. */ - protected final void registerOperator(String name, Operator operator) { + protected final void registerOperator(String name, NumberOperator operator) { manager.registerOperator(name, operator); } @@ -142,7 +143,7 @@ public abstract class Plugin { * @param name the name for which to search * @return the resulting operator, or null if none was found for that name. */ - protected final Operator operatorFor(String name) { + protected final NumberOperator operatorFor(String name) { return manager.operatorFor(name); } diff --git a/core/src/main/java/org/nwapw/abacus/plugin/PluginManager.java b/core/src/main/java/org/nwapw/abacus/plugin/PluginManager.java index b69e0db..9d2952a 100644 --- a/core/src/main/java/org/nwapw/abacus/plugin/PluginManager.java +++ b/core/src/main/java/org/nwapw/abacus/plugin/PluginManager.java @@ -35,7 +35,7 @@ public class PluginManager { /** * The map of operators registered by the plugins */ - private Map registeredOperators; + private Map registeredOperators; /** * The map of number implementations registered by the plugins. */ @@ -108,7 +108,7 @@ public class PluginManager { * @param name the name of the operator. * @param operator the operator to register. */ - public void registerOperator(String name, Operator operator) { + public void registerOperator(String name, NumberOperator operator) { registeredOperators.put(name, operator); } @@ -158,7 +158,7 @@ public class PluginManager { * @param name the name of the operator. * @return the operator, or null if it was not found. */ - public Operator operatorFor(String name) { + public NumberOperator operatorFor(String name) { return registeredOperators.get(name); } diff --git a/core/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java b/core/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java index cc2db4b..1568b15 100755 --- a/core/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/core/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java @@ -4,6 +4,9 @@ import org.nwapw.abacus.function.*; import org.nwapw.abacus.number.NaiveNumber; import org.nwapw.abacus.number.NumberInterface; import org.nwapw.abacus.number.PreciseNumber; +import org.nwapw.abacus.tree.BinaryNode; +import org.nwapw.abacus.tree.Reducer; +import org.nwapw.abacus.tree.TreeNode; import java.util.ArrayList; import java.util.HashMap; @@ -18,7 +21,7 @@ public class StandardPlugin extends Plugin { /** * The addition operator, + */ - public static final Operator OP_ADD = new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0, new Function() { + public static final NumberOperator OP_ADD = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0) { @Override protected boolean matchesParams(NumberInterface[] params) { return params.length >= 1; @@ -32,11 +35,11 @@ public class StandardPlugin extends Plugin { } return sum; } - }); + }; /** * The subtraction operator, - */ - public static final Operator OP_SUBTRACT = new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0, new Function() { + public static final NumberOperator OP_SUBTRACT = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0) { @Override protected boolean matchesParams(NumberInterface[] params) { return params.length == 2; @@ -47,11 +50,11 @@ public class StandardPlugin extends Plugin { 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() { + public static final NumberOperator OP_NEGATE = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.UNARY_PREFIX, 0) { @Override protected boolean matchesParams(NumberInterface[] params) { return params.length == 1; @@ -61,11 +64,11 @@ public class StandardPlugin extends Plugin { protected NumberInterface applyInternal(NumberInterface[] params) { return params[0].negate(); } - }); + }; /** * The multiplication operator, * */ - public static final Operator OP_MULTIPLY = new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1, new Function() { + public static final NumberOperator OP_MULTIPLY = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1) { @Override protected boolean matchesParams(NumberInterface[] params) { return params.length >= 1; @@ -79,11 +82,11 @@ public class StandardPlugin extends Plugin { } return product; } - }); + }; /** * The combination operator. */ - public static final Operator OP_NCR = new Operator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 0, new Function() { + public static final NumberOperator OP_NCR = new NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 0) { @Override protected boolean matchesParams(NumberInterface[] params) { return params.length == 2 && params[0].fractionalPart().signum() == 0 @@ -92,9 +95,9 @@ public class StandardPlugin extends Plugin { @Override protected NumberInterface applyInternal(NumberInterface[] params) { - return OP_NPR.getFunction().apply(params).divide(OP_FACTORIAL.getFunction().apply(params[1])); + return OP_NPR.apply(params).divide(OP_FACTORIAL.apply(params[1])); } - }); + }; /** * The implementation for double-based naive numbers. */ @@ -152,7 +155,7 @@ public class StandardPlugin extends Plugin { /** * The division operator, / */ - public static final Operator OP_DIVIDE = new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1, new Function() { + public static final NumberOperator OP_DIVIDE = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1) { @Override protected boolean matchesParams(NumberInterface[] params) { return params.length == 2 && params[1].compareTo(fromInt(params[0].getClass(), 0)) != 0; @@ -162,11 +165,11 @@ public class StandardPlugin extends Plugin { protected NumberInterface applyInternal(NumberInterface[] params) { return params[0].divide(params[1]); } - }); + }; /** * The factorial operator, ! */ - public static final Operator OP_FACTORIAL = new Operator(OperatorAssociativity.RIGHT, OperatorType.UNARY_POSTFIX, 0, new Function() { + public static final NumberOperator OP_FACTORIAL = new NumberOperator(OperatorAssociativity.RIGHT, OperatorType.UNARY_POSTFIX, 0) { //private HashMap, ArrayList> storedList = new HashMap, ArrayList>(); @Override protected boolean matchesParams(NumberInterface[] params) { @@ -194,11 +197,11 @@ public class StandardPlugin extends Plugin { storedList.get(params[0].getClass()).add(NaiveNumber.ONE.promoteTo(params[0].getClass())); }*/ } - }); + }; /** * The permutation operator. */ - public static final Operator OP_NPR = new Operator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 0, new Function() { + public static final NumberOperator OP_NPR = new NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 0) { @Override protected boolean matchesParams(NumberInterface[] params) { return params.length == 2 && params[0].fractionalPart().signum() == 0 @@ -224,7 +227,7 @@ public class StandardPlugin extends Plugin { } return total; } - }); + }; /** * The absolute value function, abs(-3) = 3 */ @@ -336,7 +339,7 @@ public class StandardPlugin extends Plugin { /** * The caret / pow operator, ^ */ - public static final Operator OP_CARET = new Operator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 2, new Function() { + public static final NumberOperator OP_CARET = new NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 2) { @Override protected boolean matchesParams(NumberInterface[] params) { NumberInterface zero = fromInt(params[0].getClass(), 0); @@ -362,7 +365,7 @@ public class StandardPlugin extends Plugin { } return FUNCTION_EXP.apply(FUNCTION_LN.apply(FUNCTION_ABS.apply(params[0])).multiply(params[1])); } - }); + }; /** * The square root function. */ @@ -374,7 +377,7 @@ public class StandardPlugin extends Plugin { @Override protected NumberInterface applyInternal(NumberInterface[] params) { - return OP_CARET.getFunction().apply(params[0], ((new NaiveNumber(0.5)).promoteTo(params[0].getClass()))); + return OP_CARET.apply(params[0], ((new NaiveNumber(0.5)).promoteTo(params[0].getClass()))); } }; private static final HashMap, ArrayList> FACTORIAL_LISTS = new HashMap<>(); diff --git a/core/src/main/java/org/nwapw/abacus/tree/NumberReducer.java b/core/src/main/java/org/nwapw/abacus/tree/NumberReducer.java index 0a3c4ab..a62f395 100644 --- a/core/src/main/java/org/nwapw/abacus/tree/NumberReducer.java +++ b/core/src/main/java/org/nwapw/abacus/tree/NumberReducer.java @@ -2,8 +2,10 @@ package org.nwapw.abacus.tree; import org.nwapw.abacus.Abacus; import org.nwapw.abacus.function.Function; +import org.nwapw.abacus.function.Operator; import org.nwapw.abacus.function.TreeValueFunction; import org.nwapw.abacus.number.NumberInterface; +import org.nwapw.abacus.plugin.NumberImplementation; /** * A reducer implementation that turns a tree into a single number. @@ -34,14 +36,12 @@ public class NumberReducer implements Reducer { } else if (node instanceof BinaryNode) { NumberInterface left = (NumberInterface) children[0]; NumberInterface right = (NumberInterface) children[1]; - Function function = abacus.getPluginManager().operatorFor(((BinaryNode) node).getOperation()).getFunction(); - if (function == null) return null; - return function.apply(left, right); + Operator operator = abacus.getPluginManager().operatorFor(((BinaryNode) node).getOperation()); + return operator.apply(left, right); } else if (node instanceof UnaryNode) { NumberInterface child = (NumberInterface) children[0]; - Function functionn = abacus.getPluginManager().operatorFor(((UnaryNode) node).getOperation()).getFunction(); - if (functionn == null) return null; - return functionn.apply(child); + Operator operator = abacus.getPluginManager().operatorFor(((UnaryNode) node).getOperation()); + return operator.apply(child); } else if (node instanceof FunctionNode) { NumberInterface[] convertedChildren = new NumberInterface[children.length]; for (int i = 0; i < convertedChildren.length; i++) { diff --git a/core/src/main/kotlin/org/nwapw/abacus/function/NumberOperator.kt b/core/src/main/kotlin/org/nwapw/abacus/function/NumberOperator.kt new file mode 100644 index 0000000..cf7979d --- /dev/null +++ b/core/src/main/kotlin/org/nwapw/abacus/function/NumberOperator.kt @@ -0,0 +1,15 @@ +package org.nwapw.abacus.function + +import org.nwapw.abacus.number.NumberInterface + +/** + * An operator that operates on NumberImplementations. + * + * This is simply an alias for Operator. + * @param associativity the associativity of the operator. + * @param type the type of the operator (binary, unary, etc) + * @param precedence the precedence of the operator. + */ +abstract class NumberOperator(associativity: OperatorAssociativity, type: OperatorType, + precedence: Int) : + Operator(associativity, type, precedence) \ No newline at end of file diff --git a/core/src/main/kotlin/org/nwapw/abacus/function/Operator.kt b/core/src/main/kotlin/org/nwapw/abacus/function/Operator.kt index e0cb60f..0a70b68 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/function/Operator.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/function/Operator.kt @@ -3,12 +3,11 @@ package org.nwapw.abacus.function /** * A single operator that can be used by Abacus. * - * This is a data class that holds the information about a single operator, such as a plus or minus. + * This is a class that holds the information about a single operator, such as a plus or minus. * * @param associativity the associativity of this operator, used for order of operations;. * @param type the type of this operator, used for parsing (infix / prefix / postfix and binary / unary) * @param precedence the precedence of this operator, used for order of operations. - * @param function the function this operator applies to its arguments. */ -data class Operator(val associativity: OperatorAssociativity, val type: OperatorType, - val precedence: Int, val function: Function) \ No newline at end of file +abstract class Operator(val associativity: OperatorAssociativity, val type: OperatorType, + val precedence: Int) : Applicable() \ No newline at end of file diff --git a/core/src/test/java/org/nwapw/abacus/tests/TokenizerTests.java b/core/src/test/java/org/nwapw/abacus/tests/TokenizerTests.java index 56515e3..014bbbb 100644 --- a/core/src/test/java/org/nwapw/abacus/tests/TokenizerTests.java +++ b/core/src/test/java/org/nwapw/abacus/tests/TokenizerTests.java @@ -5,10 +5,7 @@ import org.junit.BeforeClass; import org.junit.Test; import org.nwapw.abacus.Abacus; import org.nwapw.abacus.config.Configuration; -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.function.*; import org.nwapw.abacus.lexing.pattern.Match; import org.nwapw.abacus.number.NumberInterface; import org.nwapw.abacus.parsing.LexerTokenizer; @@ -35,10 +32,32 @@ public class TokenizerTests { private static Plugin testPlugin = new Plugin(abacus.getPluginManager()) { @Override public void onEnable() { - registerOperator("+", new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, - 0, subtractFunction)); - registerOperator("-", new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, - 0, subtractFunction)); + registerOperator("+", new NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, + 0) { + + @Override + protected boolean matchesParams(NumberInterface[] params) { + return true; + } + + @Override + protected NumberInterface applyInternal(NumberInterface[] params) { + return subtractFunction.apply(params); + } + }); + registerOperator("-", new NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, + 0) { + + @Override + protected boolean matchesParams(NumberInterface[] params) { + return true; + } + + @Override + protected NumberInterface applyInternal(NumberInterface[] params) { + return subtractFunction.apply(params); + } + }); registerFunction("subtract", subtractFunction); }