summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTParser.java
blob: d56ad0e28239565e4ec8c1e148479818774dd03f (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
package bjc.utils.dice.ast;

import org.apache.commons.lang3.StringUtils;

import bjc.utils.funcdata.FunctionalList;
import bjc.utils.funcdata.FunctionalStringTokenizer;
import bjc.utils.parserutils.AST;
import bjc.utils.parserutils.ShuntingYard;
import bjc.utils.parserutils.TreeConstructor;

public class DiceASTParser {
	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
	}

	public AST<IDiceASTNode> buildAST(String exp) {
		FunctionalList<String> tokens = FunctionalStringTokenizer
				.fromString(exp).toList((s) -> s);

		FunctionalList<String> shunted = yard.postfix(tokens, (s) -> s);

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

		AST<IDiceASTNode> bakedAST = rawAST.transmuteAST((tok) -> {
			if (isOperator(tok)) {
				return OperatorDiceNode.fromString(tok);
			} else if (isLiteral(tok)) {
				return new LiteralDiceNode(tok);
			} else {
				return new VariableDiceNode(tok);
			}
		});

		return bakedAST;
	}

	private boolean isOperator(String tok) {
		switch (tok) {
			case ":=":
			case "+":
			case "-":
			case "*":
			case "/":
			case "c":
			case "d":
				return true;
			default:
				return false;
		}
	}

	private boolean isLiteral(String tok) {
		if (StringUtils.countMatches(tok, 'c') == 1
				&& !tok.equalsIgnoreCase("c")) {
			return true;
		} else if (StringUtils.countMatches(tok, 'd') == 1
				&& !tok.equalsIgnoreCase("d")) {
			return true;
		} else {
			try {
				Integer.parseInt(tok);
				return true;
			} catch (NumberFormatException nfx) {
				return false;
			}
		}
	}
}