package org.nwapw.abacus.tree;

import org.nwapw.abacus.Abacus;
import org.nwapw.abacus.function.NumberFunction;
import org.nwapw.abacus.function.NumberOperator;
import org.nwapw.abacus.function.Operator;
import org.nwapw.abacus.function.TreeValueFunction;
import org.nwapw.abacus.number.NumberInterface;

/**
 * A reducer implementation that turns a tree into a single number.
 * This is not always guaranteed to work.
 */
public class NumberReducer implements Reducer<NumberInterface> {

    /**
     * The plugin manager from which to draw the functions.
     */
    private Abacus abacus;

    /**
     * Creates a new number reducer.
     *
     * @param abacus the calculator instance.
     */
    public NumberReducer(Abacus abacus) {
        this.abacus = abacus;
    }

    @Override
    public NumberInterface reduceNode(TreeNode node, Object... children) {
        if (node instanceof NumberNode) {
            return abacus.numberFromString(((NumberNode) node).getNumber());
        } else if(node instanceof VariableNode) {
            return abacus.numberFromString("0");
        } else if (node instanceof NumberBinaryNode) {
            NumberInterface left = (NumberInterface) children[0];
            NumberInterface right = (NumberInterface) children[1];
            NumberOperator operator = abacus.getPluginManager().operatorFor(((BinaryNode) node).getOperation());
            return operator.apply(left, right);
        } else if (node instanceof NumberUnaryNode) {
            NumberInterface child = (NumberInterface) children[0];
            NumberOperator 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++) {
                convertedChildren[i] = (NumberInterface) children[i];
            }
            NumberFunction function = abacus.getPluginManager().functionFor(((FunctionNode) node).getCallTo());
            if (function == null) return null;
            return function.apply(convertedChildren);
        } else if (node instanceof TreeValueFunctionNode){
            CallNode callNode = (CallNode) node;
            TreeNode[] realChildren = new TreeNode[callNode.getChildren().size()];
            for(int i = 0; i < realChildren.length; i++){
                realChildren[i] = callNode.getChildren().get(i);
            }
            TreeValueFunction function =
                    abacus.getPluginManager().treeValueFunctionFor(callNode.getCallTo());
            if(function == null) return null;
            return function.applyWithReducer(this, realChildren);
        }
        return null;
    }

}

@@ -94,166 +94,9 @@
 ion.
-NumberFunction;%0Aimport org.nwapw.abacus.function.NumberOperator;%0Aimport org.nwapw.abacus.function.Operator;%0Aimport org.nwapw.abacus.function.TreeValueFunction
+*
 ;%0Aim
@@ -2610,32 +2610,791 @@
  realChildren);%0A
+        %7D else if (node instanceof TreeValueBinaryNode) %7B%0A            BinaryNode binaryNode = (BinaryNode) node;%0A            TreeValueOperator operator = abacus.getPluginManager()%0A                    .treeValueOperatorFor(binaryNode.getOperation());%0A            if(operator == null) return null;%0A            return operator.applyWithReducer(this, binaryNode.getLeft(), binaryNode.getRight());%0A        %7D else if(node instanceof TreeValueUnaryNode) %7B%0A            UnaryNode unaryNode = (UnaryNode) node;%0A            TreeValueOperator operator = abacus.getPluginManager()%0A                    .treeValueOperatorFor(unaryNode.getOperation());%0A            if(operator == null) return null;%0A            return operator.applyWithReducer(this, unaryNode.getApplyTo());%0A
         %7D%0A      

Add reduction of TreeValue operators.