1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-12-23 16:00:09 -08:00

Implement reducing functions.

This commit is contained in:
Danila Fedorin 2017-07-26 18:44:30 -07:00
parent e8d9836431
commit d54ec9e8fa

View File

@ -1,5 +1,6 @@
package org.nwapw.abacus.tree;
import org.nwapw.abacus.function.Function;
import org.nwapw.abacus.number.NumberInterface;
import org.nwapw.abacus.plugin.PluginManager;
@ -18,7 +19,17 @@ public class NumberReducer implements Reducer<NumberInterface> {
} else if(node instanceof OpNode){
NumberInterface left = (NumberInterface) children[0];
NumberInterface right = (NumberInterface) children[1];
return manager.functionFor(((OpNode) node).getOperation()).apply(left, right);
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;
}