From d54ec9e8fab99b2d0b6536e273f6f3554d6499dc Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 26 Jul 2017 18:44:30 -0700 Subject: [PATCH] Implement reducing functions. --- src/org/nwapw/abacus/tree/NumberReducer.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/org/nwapw/abacus/tree/NumberReducer.java b/src/org/nwapw/abacus/tree/NumberReducer.java index fed6b59..2f71347 100644 --- a/src/org/nwapw/abacus/tree/NumberReducer.java +++ b/src/org/nwapw/abacus/tree/NumberReducer.java @@ -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 { } 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; }