summaryrefslogtreecommitdiff
path: root/base/src/bjc/dicelang/expr/Parser.java
blob: 8f02853b677083cade71c50c3103170a095e280e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package bjc.dicelang.expr;

import bjc.data.ITree;

/**
 * Parser for simple math expressions.
 *
 * @author Ben Culkin
 */
public class Parser {
	/**
	 * 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) {
			/* Handle leaf nodes. */
			return data.toExpr();
		}

		/* The left/right children. */
		final ITree<Token> left = ast.getChild(0);
		final ITree<Token> right = ast.getChild(1);

		/* Recursively canonicalize them. */
		String leftExpr = toCanonicalExpr(left);
		String rightExpr = toCanonicalExpr(right);

		/* Add parens if the left was higher priority. */
		if (left.getChildrenCount() == 0) {
			int leftPriority = left.getHead().typ.operatorPriority;
			int dataPriority = data.typ.operatorPriority;

			if (leftPriority >= dataPriority) {
				leftExpr = String.format("(%s)", leftExpr);
			}
		}

		/* Add parens if the right was higher priority. */
		if (right.getChildrenCount() == 0) {
			int rightPriority = right.getHead().typ.operatorPriority;
			int dataPriority = data.typ.operatorPriority;

			if (rightPriority >= dataPriority) {
				rightExpr = String.format("(%s)", rightExpr);
			}
		}

		return String.format("%s %s %s", leftExpr, data.toExpr(), rightExpr);
	}
}