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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
package bjc.dicelang;
import java.util.Map;
import java.util.Stack;
import org.apache.commons.lang3.StringUtils;
import bjc.utils.funcdata.FunctionalStringTokenizer;
import bjc.utils.funcdata.IFunctionalList;
import bjc.utils.parserutils.ShuntingYard;
/**
* Parse a dice expression from a string
*
* @author ben
*
*/
public class DiceExpressionParser {
/**
* Parse a dice expression from a string
*
* @param expression
* The string to parse an expression from
* @param enviroment
* The enviroment to use when parsing expressions
* @return The parsed dice expression
*/
public static IDiceExpression parse(String expression,
Map<String, IDiceExpression> enviroment) {
/*
* Create a tokenizer over the strings
*/
FunctionalStringTokenizer tokenizer =
new FunctionalStringTokenizer(expression);
/*
* Create a shunter to rewrite the expression
*/
ShuntingYard<String> yard = new ShuntingYard<>(true);
/*
* Add our custom operators to the yard
*/
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
/*
* Shunt the expression to postfix form
*/
IFunctionalList<String> list =
yard.postfix(tokenizer.toList(), s -> s);
/*
* Create a stack for building an expression from parts
*/
Stack<IDiceExpression> expressions = new Stack<>();
/*
* Create the expression from parts
*/
list.forEach((expressionPart) -> {
/*
* Handle compound dice
*/
if (StringUtils.countMatches(expressionPart, 'c') == 1
&& !expressionPart.equalsIgnoreCase("c")) {
String[] strangs = expressionPart.split("c");
expressions.push(new CompoundDice(strangs));
} else if (StringUtils.countMatches(expressionPart, 'd') == 1
&& !expressionPart.equalsIgnoreCase("d")) {
/*
* Handle dice groups
*/
expressions.push(ComplexDice.fromString(expressionPart));
} else {
try {
/*
* Handle scalar numbers
*/
expressions.push(new ScalarDie(
Integer.parseInt(expressionPart)));
} catch (@SuppressWarnings("unused") NumberFormatException nfex) {
// We don't care about details, just that it failed
if (expressions.size() >= 2) {
/*
* Apply an operation to two dice
*/
IDiceExpression rightExpression =
expressions.pop();
IDiceExpression leftExpression = expressions.pop();
switch (expressionPart) {
case ":=":
expressions.push(new BindingDiceExpression(
leftExpression, rightExpression,
enviroment));
break;
case "+":
expressions
.push(new OperatorDiceExpression(
rightExpression,
leftExpression,
DiceExpressionType.ADD));
break;
case "-":
expressions
.push(new OperatorDiceExpression(
rightExpression,
leftExpression,
DiceExpressionType.SUBTRACT));
break;
case "*":
expressions
.push(new OperatorDiceExpression(
rightExpression,
leftExpression,
DiceExpressionType.MULTIPLY));
break;
case "/":
expressions
.push(new OperatorDiceExpression(
rightExpression,
leftExpression,
DiceExpressionType.DIVIDE));
break;
case "c":
expressions.push(new CompoundDice(
leftExpression, rightExpression));
break;
case "d":
expressions.push(new ComplexDice(
leftExpression, rightExpression));
break;
default:
/*
* Parse it as a variable reference
*
* Make sure to restore popped variables
*/
expressions.push(leftExpression);
expressions.push(rightExpression);
expressions
.push(new ReferenceDiceExpression(
expressionPart,
enviroment));
}
} else {
/*
* Parse it as a variable reference
*/
expressions.push(new ReferenceDiceExpression(
expressionPart, enviroment));
}
}
}
});
if (expressions.size() != 1) {
System.err.println(
"WARNING: Leftovers found on dice expression stack. Remember, := is assignment.");
}
/*
* Return the built expression
*/
return expressions.pop();
}
}
|