mirror of
				https://github.com/DanilaFe/abacus
				synced 2025-11-03 18:33:41 -08:00 
			
		
		
		
	Switch the basic operators into individual classes.
This commit is contained in:
		
							parent
							
								
									4fd8f7badf
								
							
						
					
					
						commit
						81d0999c11
					
				@ -5,6 +5,10 @@ import org.nwapw.abacus.function.*;
 | 
				
			|||||||
import org.nwapw.abacus.number.NaiveNumber;
 | 
					import org.nwapw.abacus.number.NaiveNumber;
 | 
				
			||||||
import org.nwapw.abacus.number.NumberInterface;
 | 
					import org.nwapw.abacus.number.NumberInterface;
 | 
				
			||||||
import org.nwapw.abacus.number.PreciseNumber;
 | 
					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.TreeNode;
 | 
				
			||||||
import org.nwapw.abacus.tree.VariableNode;
 | 
					import org.nwapw.abacus.tree.VariableNode;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -53,32 +57,11 @@ public class StandardPlugin extends Plugin {
 | 
				
			|||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * The addition operator, +
 | 
					     * The addition operator, +
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public static final NumberOperator OP_ADD = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0) {
 | 
					    public static final NumberOperator OP_ADD = new OperatorAdd();
 | 
				
			||||||
        @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]);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * The subtraction operator, -
 | 
					     * The subtraction operator, -
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public static final NumberOperator OP_SUBTRACT = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0) {
 | 
					    public static final NumberOperator OP_SUBTRACT = new OperatorSubtract();
 | 
				
			||||||
        @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]);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * The negation operator, -
 | 
					     * The negation operator, -
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
@ -96,17 +79,7 @@ public class StandardPlugin extends Plugin {
 | 
				
			|||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * The multiplication operator, *
 | 
					     * The multiplication operator, *
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public static final NumberOperator OP_MULTIPLY = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1) {
 | 
					    public static final NumberOperator OP_MULTIPLY = new OperatorMultiply();
 | 
				
			||||||
        @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]);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * The implementation for double-based naive numbers.
 | 
					     * The implementation for double-based naive numbers.
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
@ -161,17 +134,7 @@ public class StandardPlugin extends Plugin {
 | 
				
			|||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * The division operator, /
 | 
					     * The division operator, /
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public static final NumberOperator OP_DIVIDE = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1) {
 | 
					    public static final NumberOperator OP_DIVIDE = new OperatorDivide();
 | 
				
			||||||
        @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]);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * The factorial operator, !
 | 
					     * The factorial operator, !
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
 | 
				
			|||||||
@ -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]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user