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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
package bjc.dicelang.v2;
import bjc.utils.data.ITree;
import bjc.utils.data.Tree;
import bjc.utils.data.TopDownTransformResult;
public class Evaluator {
public static class Result {
public static enum Type {
FAILURE,
INT, FLOAT, DICE
}
public final Type type;
// These may or may not have values based
// off of the result type
public long intVal;
public double floatVal;
public DiceBox.DieExpression diceVal;
public Result(Type typ) {
type = typ;
}
public Result(Type typ, long iVal) {
this(typ);
intVal = iVal;
}
public Result(Type typ, double dVal) {
this(typ);
floatVal = dVal;
}
public Result(Type typ, DiceBox.DieExpression dVal) {
this(typ);
diceVal = dVal;
}
public String toString() {
switch(type) {
case INT:
return type.toString() + "(" + intVal + ")";
case FLOAT:
return type.toString() + "(" + floatVal + ")";
case DICE:
return type.toString() + "(" + diceVal + ")";
case FAILURE:
return type.toString();
default:
return "Unknown result type " + type.toString();
}
}
}
private DiceLangEngine eng;
public Evaluator(DiceLangEngine en) {
eng = en;
}
public Result evaluate(ITree<Node> comm) {
return comm.topDownTransform(this::pickEvaluationType, this::evaluateNode).getHead().resultVal;
}
private TopDownTransformResult pickEvaluationType(Node nd) {
switch(nd.type) {
default:
return TopDownTransformResult.PUSHDOWN;
}
}
private ITree<Node> evaluateNode(ITree<Node> ast) {
switch(ast.getHead().type) {
case UNARYOP:
System.out.println("\tEVALUATOR ERROR: Unary operator evaluation isn't supported yet");
return new Tree<>(new Node(Node.Type.RESULT, new Result(Result.Type.FAILURE)));
case BINOP:
return evaluateBinaryOp(ast);
case TOKREF:
return evaluateTokenRef(ast.getHead().tokenVal);
default:
System.out.println("\tERROR: Unknown node in evaluator: " + ast.getHead().type);
return new Tree<>(new Node(Node.Type.RESULT, new Result(Result.Type.FAILURE)));
}
}
private ITree<Node> evaluateBinaryOp(ITree<Node> ast) {
Token.Type binOp = ast.getHead().operatorType;
if(ast.getChildrenCount() != 2) {
System.out.println("\tERROR: Binary operators only take two operands");
return new Tree<>(new Node(Node.Type.RESULT, new Result(Result.Type.FAILURE)));
}
ITree<Node> left = ast.getChild(0);
ITree<Node> right = ast.getChild(1);
switch(binOp) {
case ADD:
case SUBTRACT:
case MULTIPLY:
case DIVIDE:
case IDIVIDE:
return evaluateMathBinary(binOp, left.getHead().resultVal, right.getHead().resultVal);
default:
System.out.println("\tERROR: Unknown binary operator: " + binOp);
return new Tree<>(new Node(Node.Type.RESULT, new Result(Result.Type.FAILURE)));
}
}
private ITree<Node> evaluateMathBinary(Token.Type op, Result left, Result right) {
Result.Type resultType;
if(left.type == Result.Type.DICE || right.type == Result.Type.DICE) {
System.out.println("\tEVALUATOR ERROR: Math on dice isn't supported yet");
}
Result res = null;
switch(op) {
case ADD:
if(left.type == Result.Type.INT) {
if(right.type == Result.Type.INT) {
res = new Result(Result.Type.INT, left.intVal + right.intVal);
} else {
res = new Result(Result.Type.FLOAT, left.intVal + right.floatVal);
}
} else {
if(right.type == Result.Type.INT) {
res = new Result(Result.Type.FLOAT, left.floatVal + right.intVal);
} else {
res = new Result(Result.Type.FLOAT, left.floatVal + right.floatVal);
}
}
break;
case SUBTRACT:
if(left.type == Result.Type.INT) {
if(right.type == Result.Type.INT) {
res = new Result(Result.Type.INT, left.intVal - right.intVal);
} else {
res = new Result(Result.Type.FLOAT, left.intVal - right.floatVal);
}
} else {
if(right.type == Result.Type.INT) {
res = new Result(Result.Type.FLOAT, left.floatVal - right.intVal);
} else {
res = new Result(Result.Type.FLOAT, left.floatVal - right.floatVal);
}
}
break;
case MULTIPLY:
if(left.type == Result.Type.INT) {
if(right.type == Result.Type.INT) {
res = new Result(Result.Type.INT, left.intVal * right.intVal);
} else {
res = new Result(Result.Type.FLOAT, left.intVal * right.floatVal);
}
} else {
if(right.type == Result.Type.INT) {
res = new Result(Result.Type.FLOAT, left.floatVal * right.intVal);
} else {
res = new Result(Result.Type.FLOAT, left.floatVal * right.floatVal);
}
}
break;
case DIVIDE:
if(left.type == Result.Type.INT) {
if(right.type == Result.Type.INT) {
if(right.intVal == 0) {
System.out.println("\tERROR: Attempted divide by zero");
res = new Result(Result.Type.FAILURE);
} else {
res = new Result(Result.Type.FLOAT, left.intVal / right.intVal);
}
} else {
if(right.floatVal == 0) {
System.out.println("\tERROR: Attempted divide by zero");
res = new Result(Result.Type.FAILURE);
} else {
res = new Result(Result.Type.FLOAT, left.intVal / right.floatVal);
}
}
} else {
if(right.type == Result.Type.INT) {
if(right.intVal == 0) {
System.out.println("\tERROR: Attempted divide by zero");
res = new Result(Result.Type.FAILURE);
} else {
res = new Result(Result.Type.FLOAT, left.floatVal / right.intVal);
}
} else {
if(right.floatVal == 0) {
System.out.println("\tERROR: Attempted divide by zero");
res = new Result(Result.Type.FAILURE);
} else {
res = new Result(Result.Type.FLOAT, left.floatVal / right.floatVal);
}
}
}
break;
case IDIVIDE:
if(left.type == Result.Type.INT) {
if(right.type == Result.Type.INT) {
if(right.intVal == 0) {
System.out.println("\tERROR: Attempted divide by zero");
res = new Result(Result.Type.FAILURE);
} else {
res = new Result(Result.Type.INT, (int) (left.intVal / right.intVal));
}
} else {
if(right.floatVal == 0) {
System.out.println("\tERROR: Attempted divide by zero");
res = new Result(Result.Type.FAILURE);
} else {
res = new Result(Result.Type.INT, (int) (left.intVal / right.floatVal));
}
}
} else {
if(right.type == Result.Type.INT) {
if(right.intVal == 0) {
System.out.println("\tERROR: Attempted divide by zero");
res = new Result(Result.Type.FAILURE);
} else {
res = new Result(Result.Type.INT, (int) (left.floatVal / right.intVal));
}
} else {
if(right.floatVal == 0) {
System.out.println("\tERROR: Attempted divide by zero");
res = new Result(Result.Type.FAILURE);
} else {
res = new Result(Result.Type.INT, (int) (left.floatVal / right.floatVal));
}
}
}
break;
default:
System.out.println("\tERROR: Unknown math binary operator: " + op);
return new Tree<>(new Node(Node.Type.RESULT, new Result(Result.Type.FAILURE)));
}
return new Tree<>(new Node(Node.Type.RESULT, res));
}
private ITree<Node> evaluateTokenRef(Token tk) {
switch(tk.type) {
case INT_LIT:
return new Tree<>(new Node(Node.Type.RESULT, new Result(Result.Type.INT, tk.intValue)));
case FLOAT_LIT:
return new Tree<>(new Node(Node.Type.RESULT, new Result(Result.Type.FLOAT, tk.floatValue)));
case DICE_LIT:
return new Tree<>(new Node(Node.Type.RESULT, new Result(Result.Type.DICE, tk.diceValue)));
default:
System.out.println("\tERROR: Unknown token ref: " + tk.type);
return new Tree<>(new Node(Node.Type.RESULT, new Result(Result.Type.FAILURE)));
}
}
}
|