1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-11-18 08:29:33 -08:00
Abacus/src/org/nwapw/abacus/tree/NumberReducer.java

38 lines
1.4 KiB
Java
Raw Normal View History

package org.nwapw.abacus.tree;
2017-07-26 18:44:30 -07:00
import org.nwapw.abacus.function.Function;
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);
}
return null;
}
}