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

import java.util.HashMap;
import java.util.Map;
import java.util.function.BinaryOperator;

import org.apache.commons.lang3.StringUtils;

import bjc.utils.dice.BindingDiceExpression;
import bjc.utils.dice.ComplexDice;
import bjc.utils.dice.CompoundDice;
import bjc.utils.dice.CompoundDiceExpression;
import bjc.utils.dice.DiceExpressionType;
import bjc.utils.dice.IDiceExpression;
import bjc.utils.dice.ReferenceDiceExpression;
import bjc.utils.dice.ScalarDie;
import bjc.utils.parserutils.AST;

/**
 * Flatten an {@link AST} of {@link IDiceASTNode} into a
 * {@link IDiceExpression}
 * 
 * @author ben
 *
 */
public class DiceASTFlattener {
	/**
	 * Build the operations to use for tree flattening
	 * 
	 * @param env
	 *            The enviroment the tree will be flattened against
	 * @return The operations needed for tree flattening
	 */
	private static Map<IDiceASTNode, BinaryOperator<IDiceExpression>>
			buildOperations(Map<String, IDiceExpression> env) {
		Map<IDiceASTNode, BinaryOperator<IDiceExpression>> opCollapsers =
				new HashMap<>();
		opCollapsers.put(OperatorDiceNode.ADD, (left, right) -> {
			return new CompoundDiceExpression(right, left,
					DiceExpressionType.ADD);
		});
		opCollapsers.put(OperatorDiceNode.SUBTRACT, (left, right) -> {
			return new CompoundDiceExpression(right, left,
					DiceExpressionType.SUBTRACT);
		});
		opCollapsers.put(OperatorDiceNode.MULTIPLY, (left, right) -> {
			return new CompoundDiceExpression(right, left,
					DiceExpressionType.MULTIPLY);
		});
		opCollapsers.put(OperatorDiceNode.DIVIDE, (left, right) -> {
			return new CompoundDiceExpression(right, left,
					DiceExpressionType.DIVIDE);
		});
		opCollapsers.put(OperatorDiceNode.ASSIGN, (left, right) -> {
			return new BindingDiceExpression(left, right, env);
		});
		opCollapsers.put(OperatorDiceNode.COMPOUND, (left, right) -> {
			return new CompoundDice(left, right);
		});
		opCollapsers.put(OperatorDiceNode.GROUP, (left, right) -> {
			return new ComplexDice(left, right);
		});

		return opCollapsers;
	}

	/**
	 * Create a dice expression from a literal token
	 * 
	 * @param tok
	 *            The token to convert to an expression
	 * @return The dice expression represented by the token
	 */
	private static IDiceExpression expFromLiteral(LiteralDiceNode tok) {
		String data = tok.getData();

		if (StringUtils.countMatches(data, 'c') == 1
				&& !data.equalsIgnoreCase("c")) {
			String[] strangs = data.split("c");

			return new CompoundDice(ComplexDice.fromString(strangs[0]),
					ComplexDice.fromString(strangs[1]));
		} else if (StringUtils.countMatches(data, 'd') == 1
				&& !data.equalsIgnoreCase("d")) {
			return ComplexDice.fromString(data);
		} else {
			return new ScalarDie(Integer.parseInt(data));
		}
	}

	/**
	 * Flatten a AST into a dice expression
	 * 
	 * @param ast
	 *            The AST to flatten
	 * @param env
	 *            The enviroment to flatten against
	 * @return The AST, flattened into a dice expression
	 */
	public static IDiceExpression flatten(AST<IDiceASTNode> ast,
			Map<String, IDiceExpression> env) {
		Map<IDiceASTNode, BinaryOperator<IDiceExpression>> opCollapsers =
				buildOperations(env);

		return ast.collapse((nod) -> {
			if (nod instanceof LiteralDiceNode) {
				return expFromLiteral((LiteralDiceNode) nod);
			} else {
				return new ReferenceDiceExpression(
						((VariableDiceNode) nod).getVariable(), env);
			}
		} , opCollapsers::get, (r) -> r);
	}
}