blob: 1a6d2bfcfa070cfef70760bbe679de04b0393fcf (
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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
package bjc.dicelang.ast.nodes;
import org.apache.commons.lang3.StringUtils;
import bjc.dicelang.ComplexDice;
import bjc.dicelang.CompoundDice;
import bjc.dicelang.IDiceExpression;
import bjc.dicelang.ScalarDie;
import bjc.utils.data.Pair;
import bjc.utils.parserutils.AST;
/**
* A AST node that represents a literal value
*
* @author ben
*
*/
public class LiteralDiceNode implements IDiceASTNode {
private static boolean isValidInfixOperator(String dat, String op) {
return StringUtils.countMatches(dat, op) == 1
&& !dat.equalsIgnoreCase(op) && !dat.startsWith(op);
}
/**
* The value contained by this node
*/
private String value;
/**
* Create a new node with the given value
*
* @param data
* The value to be in this node
*/
public LiteralDiceNode(String data) {
this.value = data;
}
/**
* Create a new node with the given value
*
* @param val
* The value for this node
*/
public LiteralDiceNode(int val) {
this(Integer.toString(val));
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
} else if (obj == null) {
return false;
} else if (getClass() != obj.getClass()) {
return false;
} else {
LiteralDiceNode other = (LiteralDiceNode) obj;
if (value == null) {
if (other.value != null) {
return false;
}
} else if (!value.equals(other.value)) {
return false;
}
return true;
}
}
/**
* Get the data stored in this AST node
*
* @return the data stored in this AST node
*/
public String getData() {
return value;
}
@Override
public DiceASTType getType() {
return DiceASTType.LITERAL;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
@Override
public boolean isOperator() {
return false;
}
/**
* Parse this node into an expression
*
* @return The node in expression form
*/
public IDiceExpression toExpression() {
String literalData = this.getData();
if (LiteralDiceNode.isValidInfixOperator(literalData, "c")) {
String[] strangs = literalData.split("c");
return new CompoundDice(strangs);
} else if (LiteralDiceNode.isValidInfixOperator(literalData,
"d")) {
/*
* Handle dice groups
*/
return ComplexDice.fromString(literalData);
} else {
try {
return new ScalarDie(Integer.parseInt(literalData));
} catch (NumberFormatException nfex) {
UnsupportedOperationException usex = new UnsupportedOperationException(
"Found malformed leaf token " + this);
usex.initCause(nfex);
throw usex;
}
}
}
/**
* Parse this node into an expression
*
* @return The node as a pair of a sample value and the AST it
* represents
*/
public Pair<Integer, AST<IDiceASTNode>> toParseValue() {
AST<IDiceASTNode> returnedAST = new AST<>(this);
IDiceExpression expression = toExpression();
return new Pair<>(expression.roll(), returnedAST);
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return value;
}
/**
* Check if this node represents a constant value
*
* @return Whether or not this node represents a constant value
*/
public boolean isConstant() {
try {
Integer.parseInt(value);
return true;
} catch (@SuppressWarnings("unused") NumberFormatException nfex) {
// We don't care about details
return false;
}
}
/**
* Return the constant value this node represents
*
* @return The constant value of this node
*
* @throws NumberFormatException
* if you call this on a node that doesn't represent a
* constant value
*/
public int toConstant() {
return Integer.parseInt(value);
}
}
|