1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-11-04 18:08:31 -08:00

Implement reducing functions.

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

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;
}