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