From 81d0999c1132a2a2b56eb351923585887ac62915 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 21 Sep 2017 13:39:38 -0700 Subject: [PATCH] Switch the basic operators into individual classes. --- .../nwapw/abacus/plugin/StandardPlugin.java | 53 +++---------------- .../abacus/plugin/standard/OperatorAdd.kt | 21 ++++++++ .../abacus/plugin/standard/OperatorDivide.kt | 21 ++++++++ .../plugin/standard/OperatorMultiply.kt | 21 ++++++++ .../plugin/standard/OperatorSubtract.kt | 21 ++++++++ 5 files changed, 92 insertions(+), 45 deletions(-) create mode 100644 core/src/main/kotlin/org/nwapw/abacus/plugin/standard/OperatorAdd.kt create mode 100644 core/src/main/kotlin/org/nwapw/abacus/plugin/standard/OperatorDivide.kt create mode 100644 core/src/main/kotlin/org/nwapw/abacus/plugin/standard/OperatorMultiply.kt create mode 100644 core/src/main/kotlin/org/nwapw/abacus/plugin/standard/OperatorSubtract.kt 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 9d95303..db962ba 100755 --- a/core/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/core/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java @@ -5,6 +5,10 @@ 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.plugin.standard.OperatorAdd; +import org.nwapw.abacus.plugin.standard.OperatorDivide; +import org.nwapw.abacus.plugin.standard.OperatorMultiply; +import org.nwapw.abacus.plugin.standard.OperatorSubtract; import org.nwapw.abacus.tree.TreeNode; import org.nwapw.abacus.tree.VariableNode; @@ -53,32 +57,11 @@ public class StandardPlugin extends Plugin { /** * The addition operator, + */ - public static final NumberOperator OP_ADD = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0) { - @Override - public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { - return params.length == 2; - } - - @Override - public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { - return params[0].add(params[1]); - } - }; + public static final NumberOperator OP_ADD = new OperatorAdd(); /** * The subtraction operator, - */ - public static final NumberOperator OP_SUBTRACT = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0) { - @Override - public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { - return params.length == 2; - } - - @Override - public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { - return params[0].subtract(params[1]); - - } - }; + public static final NumberOperator OP_SUBTRACT = new OperatorSubtract(); /** * The negation operator, - */ @@ -96,17 +79,7 @@ public class StandardPlugin extends Plugin { /** * The multiplication operator, * */ - public static final NumberOperator OP_MULTIPLY = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1) { - @Override - public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { - return params.length == 2; - } - - @Override - public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { - return params[0].multiply(params[1]); - } - }; + public static final NumberOperator OP_MULTIPLY = new OperatorMultiply(); /** * The implementation for double-based naive numbers. */ @@ -161,17 +134,7 @@ public class StandardPlugin extends Plugin { /** * The division operator, / */ - public static final NumberOperator OP_DIVIDE = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1) { - @Override - public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { - return params.length == 2 && params[1].compareTo(context.getInheritedNumberImplementation().instanceForString(Integer.toString(0))) != 0; - } - - @Override - public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { - return params[0].divide(params[1]); - } - }; + public static final NumberOperator OP_DIVIDE = new OperatorDivide(); /** * The factorial operator, ! */ diff --git a/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/OperatorAdd.kt b/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/OperatorAdd.kt new file mode 100644 index 0000000..0f1c7d4 --- /dev/null +++ b/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/OperatorAdd.kt @@ -0,0 +1,21 @@ +package org.nwapw.abacus.plugin.standard + +import org.nwapw.abacus.context.MutableEvaluationContext +import org.nwapw.abacus.function.NumberOperator +import org.nwapw.abacus.function.OperatorAssociativity +import org.nwapw.abacus.function.OperatorType +import org.nwapw.abacus.number.NumberInterface + +/** + * The addition operator. + * + * This is a standard operator that simply performs addition. + */ +class OperatorAdd: NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0) { + + override fun matchesParams(context: MutableEvaluationContext, params: Array) = + params.size == 2 + override fun applyInternal(context: MutableEvaluationContext, params: Array) = + params[0] + params[1] + +} \ No newline at end of file diff --git a/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/OperatorDivide.kt b/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/OperatorDivide.kt new file mode 100644 index 0000000..55eeb1e --- /dev/null +++ b/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/OperatorDivide.kt @@ -0,0 +1,21 @@ +package org.nwapw.abacus.plugin.standard + +import org.nwapw.abacus.context.MutableEvaluationContext +import org.nwapw.abacus.function.NumberOperator +import org.nwapw.abacus.function.OperatorAssociativity +import org.nwapw.abacus.function.OperatorType +import org.nwapw.abacus.number.NumberInterface + +/** + * The division operator. + * + * This is a standard operator that simply performs division. + */ +class OperatorDivide: NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1) { + + override fun matchesParams(context: MutableEvaluationContext, params: Array) = + params.size == 2 + override fun applyInternal(context: MutableEvaluationContext, params: Array) = + params[0] / params[1] + +} \ No newline at end of file diff --git a/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/OperatorMultiply.kt b/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/OperatorMultiply.kt new file mode 100644 index 0000000..98064fb --- /dev/null +++ b/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/OperatorMultiply.kt @@ -0,0 +1,21 @@ +package org.nwapw.abacus.plugin.standard + +import org.nwapw.abacus.context.MutableEvaluationContext +import org.nwapw.abacus.function.NumberOperator +import org.nwapw.abacus.function.OperatorAssociativity +import org.nwapw.abacus.function.OperatorType +import org.nwapw.abacus.number.NumberInterface + +/** + * The multiplication operator. + * + * This is a standard operator that simply performs multiplication. + */ +class OperatorMultiply: NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1) { + + override fun matchesParams(context: MutableEvaluationContext, params: Array) = + params.size == 2 + override fun applyInternal(context: MutableEvaluationContext, params: Array) = + params[0] * params[1] + +} \ No newline at end of file diff --git a/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/OperatorSubtract.kt b/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/OperatorSubtract.kt new file mode 100644 index 0000000..25d408c --- /dev/null +++ b/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/OperatorSubtract.kt @@ -0,0 +1,21 @@ +package org.nwapw.abacus.plugin.standard + +import org.nwapw.abacus.context.MutableEvaluationContext +import org.nwapw.abacus.function.NumberOperator +import org.nwapw.abacus.function.OperatorAssociativity +import org.nwapw.abacus.function.OperatorType +import org.nwapw.abacus.number.NumberInterface + +/** + * The subtraction operator. + * + * This is a standard operator that performs subtraction. + */ +class OperatorSubtract: NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0) { + + override fun matchesParams(context: MutableEvaluationContext, params: Array) = + params.size == 2 + override fun applyInternal(context: MutableEvaluationContext, params: Array) = + params[0] - params[1] + +} \ No newline at end of file