From 12e34230ec77724d1a25d47dd10d5d4c6571230f Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 26 Jul 2017 18:41:21 -0700 Subject: [PATCH] Add correct handling of failed reduces to both OpNode and Window. --- src/org/nwapw/abacus/tree/OpNode.java | 1 + src/org/nwapw/abacus/window/Window.java | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/org/nwapw/abacus/tree/OpNode.java b/src/org/nwapw/abacus/tree/OpNode.java index 8a399f7..e6565f7 100644 --- a/src/org/nwapw/abacus/tree/OpNode.java +++ b/src/org/nwapw/abacus/tree/OpNode.java @@ -86,6 +86,7 @@ public class OpNode extends TreeNode { public T reduce(Reducer reducer) { T leftReduce = left.reduce(reducer); T rightReduce = right.reduce(reducer); + if(leftReduce == null || rightReduce == null) return null; return reducer.reduceNode(this, leftReduce, rightReduce); } diff --git a/src/org/nwapw/abacus/window/Window.java b/src/org/nwapw/abacus/window/Window.java index 319ddbd..8d35f20 100644 --- a/src/org/nwapw/abacus/window/Window.java +++ b/src/org/nwapw/abacus/window/Window.java @@ -1,5 +1,6 @@ package org.nwapw.abacus.window; +import org.nwapw.abacus.number.NumberInterface; import org.nwapw.abacus.plugin.PluginManager; import org.nwapw.abacus.tree.NumberReducer; import org.nwapw.abacus.tree.TreeNode; @@ -19,6 +20,7 @@ public class Window extends JFrame { private static final String CALC_STRING = "Calculate"; private static final String SELECT_STRING = "Select"; private static final String SYNTAX_ERR_STRING = "Syntax Error"; + private static final String EVAL_ERR_STRING = "Evaluation Error"; private static final String NUMBER_SYSTEM_LABEL = "Number Type:"; private static final String FUNCTION_LABEL = "Functions:"; @@ -117,7 +119,12 @@ public class Window extends JFrame { lastOutputArea.setText(SYNTAX_ERR_STRING); return; } - lastOutput = parsedExpression.reduce(reducer).toString(); + NumberInterface numberInterface = parsedExpression.reduce(reducer); + if(numberInterface == null){ + lastOutputArea.setText(EVAL_ERR_STRING);; + return; + } + lastOutput = numberInterface.toString(); historyModel.addEntry(new HistoryTableModel.HistoryEntry(inputField.getText(), parsedExpression, lastOutput)); historyTable.invalidate(); lastOutputArea.setText(lastOutput);