1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-11-17 08:03:09 -08:00

Switch the basic operators into individual classes.

This commit is contained in:
Danila Fedorin 2017-09-21 13:39:38 -07:00
parent 4fd8f7badf
commit 81d0999c11
5 changed files with 92 additions and 45 deletions

View File

@ -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, !
*/

View File

@ -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<out NumberInterface>) =
params.size == 2
override fun applyInternal(context: MutableEvaluationContext, params: Array<out NumberInterface>) =
params[0] + params[1]
}

View File

@ -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<out NumberInterface>) =
params.size == 2
override fun applyInternal(context: MutableEvaluationContext, params: Array<out NumberInterface>) =
params[0] / params[1]
}

View File

@ -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<out NumberInterface>) =
params.size == 2
override fun applyInternal(context: MutableEvaluationContext, params: Array<out NumberInterface>) =
params[0] * params[1]
}

View File

@ -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<out NumberInterface>) =
params.size == 2
override fun applyInternal(context: MutableEvaluationContext, params: Array<out NumberInterface>) =
params[0] - params[1]
}