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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
package bjc.dicelang.old.ast;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import org.apache.commons.lang3.StringUtils;
import bjc.dicelang.BindingDiceExpression;
import bjc.dicelang.ComplexDice;
import bjc.dicelang.CompoundDice;
import bjc.dicelang.OperatorDiceExpression;
import bjc.dicelang.DiceExpressionType;
import bjc.dicelang.IDiceExpression;
import bjc.dicelang.ReferenceDiceExpression;
import bjc.dicelang.ScalarDie;
import bjc.dicelang.old.ast.nodes.DiceASTType;
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.parserutils.AST;
/**
* Flatten an {@link AST} of {@link IDiceASTNode} into a
* {@link IDiceExpression}
*
* @author ben
*
*/
public class DiceASTFlattener {
private static final class NodeCollapser
implements Function<IDiceASTNode, IDiceExpression> {
private Map<String, IDiceExpression> enviroment;
public NodeCollapser(Map<String, IDiceExpression> env) {
this.enviroment = env;
}
@Override
public IDiceExpression apply(IDiceASTNode nod) {
if (nod.getType() == DiceASTType.LITERAL) {
return expFromLiteral((LiteralDiceNode) nod);
} else if (nod.getType() == DiceASTType.VARIABLE) {
String varName = ((VariableDiceNode) nod).getVariable();
return new ReferenceDiceExpression(varName, enviroment);
} else {
throw new UnsupportedOperationException(
"Attempted to flatten something that can't be"
+ " flattened. The culprit is " + nod);
}
}
/**
* 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 (data.equals("")) {
throw new UnsupportedOperationException(
"Can't convert a blank token into a literal");
}
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));
}
}
}
/**
* 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 OperatorDiceExpression(right, left,
DiceExpressionType.ADD);
});
opCollapsers.put(OperatorDiceNode.SUBTRACT, (left, right) -> {
return new OperatorDiceExpression(right, left,
DiceExpressionType.SUBTRACT);
});
opCollapsers.put(OperatorDiceNode.MULTIPLY, (left, right) -> {
return new OperatorDiceExpression(right, left,
DiceExpressionType.MULTIPLY);
});
opCollapsers.put(OperatorDiceNode.DIVIDE, (left, right) -> {
return new OperatorDiceExpression(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;
}
/**
* 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(new NodeCollapser(env), opCollapsers::get,
(r) -> r);
}
}
|