From 51ca0a9ae2c927bc9211e914166585b882c1f69c Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 26 Jul 2017 17:26:26 -0700 Subject: [PATCH 1/2] Make some operator-related fields protected in TreeNode. This should allow for the implementation of toString in child nodes. --- src/org/nwapw/abacus/tree/TreeNode.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/org/nwapw/abacus/tree/TreeNode.java b/src/org/nwapw/abacus/tree/TreeNode.java index 8540677..accdefa 100644 --- a/src/org/nwapw/abacus/tree/TreeNode.java +++ b/src/org/nwapw/abacus/tree/TreeNode.java @@ -13,7 +13,7 @@ public abstract class TreeNode { /** * The lexer used to lex tokens. */ - private static Lexer lexer = new Lexer(){{ + protected static Lexer lexer = new Lexer(){{ register("\\+|-|\\*|/|^", TokenType.OP); register("[0-9]+(\\.[0-9]+)?", TokenType.NUM); register("[a-zA-Z]+", TokenType.WORD); @@ -23,7 +23,7 @@ public abstract class TreeNode { /** * A map that maps operations to their precedence. */ - private static HashMap precedenceMap = new HashMap(){{ + protected static HashMap precedenceMap = new HashMap(){{ put("+", 0); put("-", 0); put("*", 1); @@ -33,7 +33,7 @@ public abstract class TreeNode { /** * A map that maps operations to their associativity. */ - private static HashMap associativityMap = + protected static HashMap associativityMap = new HashMap() {{ put("+", OperatorAssociativity.LEFT); put("-", OperatorAssociativity.LEFT); @@ -45,7 +45,7 @@ public abstract class TreeNode { /** * Comparator used to sort token types. */ - private static Comparator tokenSorter = Comparator.comparingInt(e -> e.priority); + protected static Comparator tokenSorter = Comparator.comparingInt(e -> e.priority); /** * Tokenizes a string, converting it into matches From c88a0cbdc512d72f9311f40d02d6802000d3e8eb Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 26 Jul 2017 17:26:55 -0700 Subject: [PATCH 2/2] 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; + } }