diff options
| author | student <student@localhost> | 2018-03-05 16:48:38 -0500 |
|---|---|---|
| committer | student <student@localhost> | 2018-03-05 16:48:38 -0500 |
| commit | 806cba069576e48477d4660a019a49c405cb0195 (patch) | |
| tree | c1e718b18e660c02a12b5a466a222daf8a435bf4 /base/src/bjc/dicelang/expr/Parser.java | |
| parent | f9d9bd4bbf7dd6a297e1daf5ee7b4263d706d9cd (diff) | |
Update
Diffstat (limited to 'base/src/bjc/dicelang/expr/Parser.java')
| -rw-r--r-- | base/src/bjc/dicelang/expr/Parser.java | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/base/src/bjc/dicelang/expr/Parser.java b/base/src/bjc/dicelang/expr/Parser.java index 020467a..db6d547 100644 --- a/base/src/bjc/dicelang/expr/Parser.java +++ b/base/src/bjc/dicelang/expr/Parser.java @@ -8,14 +8,18 @@ import bjc.utils.data.ITree; * @author Ben Culkin */ public class Parser { - /* - * Convert an expression to one that uses the smallest necessary amount - * of parens. + /** + * Convert an expression to one that uses the smallest necessary amount of + * parens. + * + * @param ast + * The AST to canonicalize. + * @return The canonicalized AST. */ public static String toCanonicalExpr(final ITree<Token> ast) { final Token data = ast.getHead(); - if(ast.getChildrenCount() == 0) { + if (ast.getChildrenCount() == 0) { /* Handle leaf nodes. */ return data.toExpr(); } @@ -29,21 +33,21 @@ public class Parser { String rightExpr = toCanonicalExpr(right); /* Add parens if the left was higher priority. */ - if(left.getChildrenCount() == 0) { + if (left.getChildrenCount() == 0) { int leftPriority = left.getHead().typ.operatorPriority; int dataPriority = data.typ.operatorPriority; - if(leftPriority >= dataPriority) { + if (leftPriority >= dataPriority) { leftExpr = String.format("(%s)", leftExpr); } } /* Add parens if the right was higher priority. */ - if(right.getChildrenCount() == 0) { + if (right.getChildrenCount() == 0) { int rightPriority = right.getHead().typ.operatorPriority; int dataPriority = data.typ.operatorPriority; - if(rightPriority >= dataPriority) { + if (rightPriority >= dataPriority) { rightExpr = String.format("(%s)", rightExpr); } } |
