2017-07-26 13:31:05 -07:00
|
|
|
package org.nwapw.abacus.tree;
|
|
|
|
|
2017-07-26 18:44:30 -07:00
|
|
|
import org.nwapw.abacus.function.Function;
|
2017-07-26 13:31:05 -07:00
|
|
|
import org.nwapw.abacus.number.NumberInterface;
|
|
|
|
import org.nwapw.abacus.plugin.PluginManager;
|
|
|
|
|
|
|
|
public class NumberReducer implements Reducer<NumberInterface> {
|
|
|
|
|
|
|
|
private PluginManager manager;
|
|
|
|
|
|
|
|
public NumberReducer(PluginManager manager){
|
|
|
|
this.manager = manager;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public NumberInterface reduceNode(TreeNode node, Object... children) {
|
|
|
|
if(node instanceof NumberNode) {
|
|
|
|
return ((NumberNode) node).getNumber();
|
|
|
|
} else if(node instanceof OpNode){
|
|
|
|
NumberInterface left = (NumberInterface) children[0];
|
|
|
|
NumberInterface right = (NumberInterface) children[1];
|
2017-07-26 18:44:30 -07:00
|
|
|
Function function = manager.functionFor(((OpNode) node).getOperation());
|
|
|
|
if(function == null) return null;
|
|
|
|
return function.apply(left, right);
|
|
|
|
} else if(node instanceof FunctionNode){
|
|
|
|
NumberInterface[] convertedChildren = new NumberInterface[children.length];
|
|
|
|
for(int i = 0; i < convertedChildren.length; i++){
|
|
|
|
convertedChildren[i] = (NumberInterface) children[i];
|
|
|
|
}
|
|
|
|
Function function = manager.functionFor(((FunctionNode) node).getFunction());
|
|
|
|
if(function == null) return null;
|
|
|
|
return function.apply(convertedChildren);
|
2017-07-26 13:31:05 -07:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|