From c88a0cbdc512d72f9311f40d02d6802000d3e8eb Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 26 Jul 2017 17:26:55 -0700 Subject: [PATCH] Implement toString in child nodes of TreeNode. --- src/org/nwapw/abacus/tree/NumberNode.java | 5 +++++ src/org/nwapw/abacus/tree/OpNode.java | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/org/nwapw/abacus/tree/NumberNode.java b/src/org/nwapw/abacus/tree/NumberNode.java index f88ef0a..41112e7 100644 --- a/src/org/nwapw/abacus/tree/NumberNode.java +++ b/src/org/nwapw/abacus/tree/NumberNode.java @@ -49,4 +49,9 @@ public class NumberNode extends TreeNode { public T reduce(Reducer reducer) { return reducer.reduceNode(this); } + + @Override + public String toString() { + return number != null ? number.toString() : "null"; + } } diff --git a/src/org/nwapw/abacus/tree/OpNode.java b/src/org/nwapw/abacus/tree/OpNode.java index 33ba805..8a399f7 100644 --- a/src/org/nwapw/abacus/tree/OpNode.java +++ b/src/org/nwapw/abacus/tree/OpNode.java @@ -88,4 +88,19 @@ public class OpNode extends TreeNode { T rightReduce = right.reduce(reducer); return reducer.reduceNode(this, leftReduce, rightReduce); } + + @Override + public String toString() { + String leftString = left != null ? left.toString() : "null"; + String rightString = right != null ? right.toString() : "null"; + + if(right != null && right instanceof OpNode){ + if(TreeNode.precedenceMap.get(((OpNode) right).getOperation()) > + TreeNode.precedenceMap.get(operation)) { + rightString = "(" + rightString + ")"; + } + } + + return leftString + operation + rightString; + } }