summaryrefslogtreecommitdiff
path: root/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTParser.java
blob: c9b48c8137c505084ec575f1a8a84ebe1710cb41 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package bjc.dicelang.old.ast;

import java.util.Deque;
import java.util.LinkedList;
import java.util.function.Function;

import bjc.dicelang.old.ast.nodes.IDiceASTNode;
import bjc.dicelang.old.ast.nodes.LiteralDiceNode;
import bjc.dicelang.old.ast.nodes.OperatorDiceNode;
import bjc.dicelang.old.ast.nodes.VariableDiceNode;
import bjc.utils.data.IPair;
import bjc.utils.data.Pair;
import bjc.utils.funcdata.FunctionalStringTokenizer;
import bjc.utils.funcdata.IFunctionalList;
import bjc.utils.funcutils.ListUtils;
import bjc.utils.parserutils.AST;
import bjc.utils.parserutils.ShuntingYard;
import bjc.utils.parserutils.TreeConstructor;

/**
 * Create an AST from a string expression
 * 
 * @author ben
 *
 */
public class DiceASTParser {
	private static final class NodeBaker
			implements Function<String, IDiceASTNode> {
		@Override
		public IDiceASTNode apply(String tok) {
			if (isOperator(tok)) {
				return OperatorDiceNode.fromString(tok);
			} else if (LiteralDiceNode.isLiteral(tok)) {
				return new LiteralDiceNode(tok);
			} else {
				return new VariableDiceNode(tok);
			}
		}
	}

	/**
	 * The yard to use for shunting expressions
	 */
	private static ShuntingYard<String> yard;

	static {
		yard = new ShuntingYard<>();

		yard.addOp("d", 5); // dice operator: use for creating variable
		// size dice groups
		yard.addOp("c", 6); // compound operator: use for creating compound
		// dice from expressions
		yard.addOp(":=", 0); // binding operator: Bind a name to a variable
		// expression
	}

	/**
	 * Build an AST from a string expression
	 * 
	 * @param exp
	 *            The string to build from
	 * @return An AST built from the passed in string
	 */
	public static AST<IDiceASTNode> buildAST(String exp) {
		IFunctionalList<String> tokens = FunctionalStringTokenizer
				.fromString(exp).toList();

		Deque<IPair<String, String>> ops = new LinkedList<>();

		ops.add(new Pair<>("+", "\\+"));
		ops.add(new Pair<>("-", "-"));
		ops.add(new Pair<>("*", "\\*"));
		ops.add(new Pair<>("/", "/"));
		ops.add(new Pair<>(":=", ":="));

		IFunctionalList<String> semiExpandedTokens = ListUtils
				.splitTokens(tokens, ops);

		ops = new LinkedList<>();

		ops.add(new Pair<>("(", "\\("));
		ops.add(new Pair<>(")", "\\)"));

		IFunctionalList<String> fullyExpandedTokens = ListUtils
				.deAffixTokens(semiExpandedTokens, ops);

		IFunctionalList<String> shunted = yard.postfix(fullyExpandedTokens,
				(s) -> s);

		AST<String> rawAST = TreeConstructor.constructTree(shunted,
				DiceASTParser::isOperator);

		AST<IDiceASTNode> bakedAST = rawAST.transmuteAST(new NodeBaker());

		return bakedAST;
	}

	/**
	 * Check if a token represents an operator
	 * 
	 * @param tok
	 *            The token to check if it represents an operator
	 * @return Whether or not the token represents an operator
	 */
	private static boolean isOperator(String tok) {
		switch (tok) {
			case ":=":
			case "+":
			case "-":
			case "*":
			case "/":
			case "c":
			case "d":
				return true;
			default:
				return false;
		}
	}
}