mirror of
https://github.com/DanilaFe/abacus
synced 2026-09-22 03:12:33 +00:00
Switch the basic operators into individual classes.
This commit is contained in:
@@ -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]
|
||||
|
||||
}
|
||||
@@ -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]
|
||||
|
||||
}
|
||||
@@ -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]
|
||||
|
||||
}
|
||||
@@ -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]
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user