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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
|
package bjc.dicelang.eval;
import java.math.BigDecimal;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.function.Consumer;
import bjc.dicelang.DiceLangEngine;
import bjc.dicelang.Errors;
import bjc.dicelang.Node;
import bjc.dicelang.Node.Type;
import bjc.dicelang.dice.CompoundDie;
import bjc.dicelang.dice.Die;
import bjc.dicelang.dice.MathDie;
import bjc.dicelang.dice.ScalarDiceExpression;
import bjc.dicelang.dice.ScalarDie;
import bjc.dicelang.dice.SimpleDie;
import bjc.dicelang.dice.SimpleDieList;
import bjc.dicelang.tokens.DecimalToken;
import bjc.dicelang.tokens.DiceToken;
import bjc.dicelang.tokens.FloatToken;
import bjc.dicelang.tokens.Token;
import bjc.data.Tree;
import bjc.data.SingleIterator;
import bjc.data.TopDownTransformIterator;
import bjc.data.TopDownTransformResult;
import bjc.data.SimpleTree;
import static bjc.dicelang.Errors.ErrorKey.*;
import static bjc.dicelang.eval.EvaluatorResult.Type.*;
/*
* @TODO 10/09/17 Ben Culkin :EvaluatorSplit
*
* Type/sanity checking should be moved into a seperate stage, not part of
* evaluation.
*/
/**
* Evaluate DiceLang ASTs
*
* @author EVE
*
*/
public class Evaluator {
/* The steps of type coercion. */
private static enum CoerceSteps {
INTEGER, DOUBLE, DECIMAL;
}
/* The context during iteration. */
private static class Context {
public Consumer<Iterator<Tree<Node>>> thunk;
public boolean isDebug;
public Context() {
/* Empty block. */
}
}
/* The engine we are connected to. */
private final DiceLangEngine eng;
/**
* Create a new evaluator.
*
* @param en
* The engine.
*/
public Evaluator(final DiceLangEngine en) {
eng = en;
}
/**
* Evaluate a AST.
*
* @param comm
* The AST to evaluate.
*
* @return The result of the tree.
*/
public EvaluatorResult evaluate(final Tree<Node> comm) {
final Context ctx = new Context();
ctx.isDebug = false;
ctx.thunk = itr -> {
/*
* Deliberately finish the iterator, but ignore results. It's only for stepwise
* evaluation, but we don't know if stepping the iterator has side effects.
*/
while (itr.hasNext()) {
itr.next();
}
};
/* The result. */
final Tree<Node> res = comm.topDownTransform(this::pickEvaluationType, node -> this.evaluateNode(node, ctx));
return res.getHead().resultVal;
}
/*
* @NOTE
*
* This is broken until stepwise top-down transforms are fixed.
*
* Make it public once we know it works again.
*/
@SuppressWarnings("javadoc")
public Iterator<Tree<Node>> stepDebug(final Tree<Node> comm) {
final Context ctx = new Context();
ctx.isDebug = true;
return new TopDownTransformIterator<>(this::pickEvaluationType, (node, thnk) -> {
ctx.thunk = thnk;
return this.evaluateNode(node, ctx);
}, comm);
}
/* Pick the way to evaluate a node. */
private TopDownTransformResult pickEvaluationType(final Node nd) {
switch (nd.type) {
case UNARYOP:
switch (nd.operatorType) {
case COERCE:
/* Coerce does special things to the tree. */
return TopDownTransformResult.RTRANSFORM;
default:
return TopDownTransformResult.PUSHDOWN;
}
case GROUP:
return TopDownTransformResult.PASSTHROUGH;
default:
return TopDownTransformResult.PUSHDOWN;
}
}
/* Evaluate a node. */
private Tree<Node> evaluateNode(final Tree<Node> ast, final Context ctx) {
switch (ast.getHead().type) {
case UNARYOP:
return evaluateUnaryOp(ast, ctx);
case BINOP:
return evaluateBinaryOp(ast, ctx);
case TOKREF:
return evaluateTokenRef(ast.getHead().tokenVal, ctx);
case ROOT:
return ast.getChild(ast.getChildrenCount() - 1);
case RESULT:
return ast;
default:
Errors.inst.printError(EK_EVAL_INVNODE, ast.getHead().type.toString());
return new SimpleTree<>(Node.FAIL(ast));
}
}
/* Evaluate a unary operator. */
private Tree<Node> evaluateUnaryOp(final Tree<Node> ast, final Context ctx) {
/* Unary operators only take one operand. */
if (ast.getChildrenCount() != 1) {
Errors.inst.printError(EK_EVAL_UNUNARY, Integer.toString(ast.getChildrenCount()));
return new SimpleTree<>(Node.FAIL(ast));
}
switch (ast.getHead().operatorType) {
case COERCE:
return doTypeCoercion(ast, ctx);
case DICESCALAR: {
final EvaluatorResult opr = ast.getChild(0).getHead().resultVal;
if (opr.type != INT) {
Errors.inst.printError(EK_EVAL_INVDCREATE, opr.type.toString());
}
IntegerEvaluatorResult irs = (IntegerEvaluatorResult) opr;
ScalarDie die = new ScalarDie(irs.value);
final EvaluatorResult sres = new DiceEvaluatorResult(die);
return new SimpleTree<>(new Node(Node.Type.RESULT, sres));
}
case DICEFUDGE: {
final EvaluatorResult oprn = ast.getChild(0).getHead().resultVal;
if (oprn.type != INT) {
Errors.inst.printError(EK_EVAL_INVDCREATE, oprn.type.toString());
}
IntegerEvaluatorResult irs = (IntegerEvaluatorResult) oprn;
ScalarDie die = new ScalarDie(irs.value);
final EvaluatorResult fres = new DiceEvaluatorResult(die);
return new SimpleTree<>(new Node(Node.Type.RESULT, fres));
}
default: {
Errors.inst.printError(EK_EVAL_INVUNARY, ast.getHead().operatorType.toString());
return new SimpleTree<>(Node.FAIL(ast));
}
}
}
/*
* @TODO 10/09/17 Ben Culkin :CoerceRefactor
*
* :EvaluatorSplit
*
* Coercing should be moved to its own class, or at the very least its own
* method. When the evaluator splits, this node type'll be handled exclusively
* by the type-checker.
*
* Coerce also needs to be able to coerce things to dice and ratios (whenever
* they get added).
*/
private Tree<Node> doTypeCoercion(final Tree<Node> ast, final Context ctx) {
final Tree<Node> toCoerce = ast.getChild(0);
final Tree<Node> retVal = new SimpleTree<>(toCoerce.getHead());
final Deque<Tree<Node>> children = new LinkedList<>();
/* The current type we are coercing to. */
CoerceSteps curLevel = CoerceSteps.INTEGER;
for (int i = 0; i < toCoerce.getChildrenCount(); i++) {
final Tree<Node> child = toCoerce.getChild(i);
Tree<Node> nChild = null;
/* Tell our thunk we processed a node. */
if (ctx.isDebug) {
/* Evaluate each step of the child. */
final Iterator<Tree<Node>> nd = stepDebug(child);
for (; nd.hasNext(); nChild = nd.next()) {
ctx.thunk.accept(new SingleIterator<>(child));
}
} else {
/* Evaluate the child. */
nChild = new SimpleTree<>(new Node(Node.Type.RESULT, evaluate(child)));
ctx.thunk.accept(new SingleIterator<>(nChild));
}
if (nChild == null) {
Errors.inst.printError(EK_EVAL_INVNODE);
return new SimpleTree<>(Node.FAIL(ast));
}
final Node childNode = nChild.getHead();
final EvaluatorResult res = childNode.resultVal;
/* Move up to coercing to a float. */
if (res.type == FLOAT) {
curLevel = CoerceSteps.DOUBLE;
} else if (res.type == DEC) {
curLevel = CoerceSteps.DECIMAL;
}
children.add(nChild);
}
for (final Tree<Node> child : children) {
final Node nd = child.getHead();
final EvaluatorResult res = nd.resultVal;
switch (res.type) {
case INT:
/*
* Coerce ints to doubles if we need to.
*/
if (curLevel == CoerceSteps.DOUBLE) {
IntegerEvaluatorResult rs = (IntegerEvaluatorResult) res;
nd.resultVal = new FloatEvaluatorResult(rs.value);
} else if (curLevel == CoerceSteps.DECIMAL) {
IntegerEvaluatorResult rs = (IntegerEvaluatorResult) res;
nd.resultVal = new DecimalEvaluatorResult(new BigDecimal(rs.value));
}
break;
case FLOAT:
/*
* Coerce decimals to doubles if we need to.
*/
if (curLevel == CoerceSteps.DECIMAL) {
FloatEvaluatorResult rs = (FloatEvaluatorResult) res;
nd.resultVal = new DecimalEvaluatorResult(new BigDecimal(rs.floatVal));
}
default:
/* Do nothing */
break;
}
retVal.addChild(child);
}
return retVal;
}
/* Evaluate a binary operator. */
private static Tree<Node> evaluateBinaryOp(final Tree<Node> ast, final Context ctx) {
final Token.Type binOp = ast.getHead().operatorType;
/* Binary operators always have two children. */
if (ast.getChildrenCount() != 2) {
Errors.inst.printError(EK_EVAL_INVBIN, Integer.toString(ast.getChildrenCount()), ast.toString());
return new SimpleTree<>(Node.FAIL(ast));
}
final Tree<Node> left = ast.getChild(0);
final Tree<Node> right = ast.getChild(1);
final EvaluatorResult leftRes = left.getHead().resultVal;
final EvaluatorResult rightRes = right.getHead().resultVal;
switch (binOp) {
case ADD:
case SUBTRACT:
case MULTIPLY:
case DIVIDE:
case IDIVIDE:
return evaluateMathBinary(binOp, leftRes, rightRes, ctx);
case DICEGROUP:
case DICECONCAT:
case DICELIST:
return evaluateDiceBinary(binOp, leftRes, rightRes, ctx);
case STRCAT:
case STRREP:
return evaluateStringBinary(binOp, leftRes, rightRes, ctx);
default:
Errors.inst.printError(EK_EVAL_UNBIN, binOp.toString());
return new SimpleTree<>(Node.FAIL(ast));
}
}
/* Evaluate a binary operator on strings. */
private static Tree<Node> evaluateStringBinary(final Token.Type op, final EvaluatorResult left,
final EvaluatorResult right, final Context ctx) {
if (left.type != STRING) {
Errors.inst.printError(EK_EVAL_INVSTRING, left.type.toString());
return new SimpleTree<>(Node.FAIL(left));
}
final String strang = ((StringEvaluatorResult) left).stringVal;
switch (op) {
case STRCAT: {
if (right.type != STRING) {
Errors.inst.printError(EK_EVAL_UNSTRING, right.type.toString());
return new SimpleTree<>(Node.FAIL(right));
}
final String strung = ((StringEvaluatorResult) right).stringVal;
final EvaluatorResult cres = new StringEvaluatorResult(strang + strung);
return new SimpleTree<>(new Node(Node.Type.RESULT, cres));
}
case STRREP: {
if (right.type != INT) {
Errors.inst.printError(EK_EVAL_INVSTRING, right.type.toString());
return new SimpleTree<>(Node.FAIL(right));
}
String res = strang;
final long count = ((IntegerEvaluatorResult) right).value;
for (long i = 1; i < count; i++) {
res += strang;
}
return new SimpleTree<>(new Node(Node.Type.RESULT, new StringEvaluatorResult(res)));
}
default:
Errors.inst.printError(EK_EVAL_UNSTRING, op.toString());
return new SimpleTree<>(Node.FAIL());
}
}
/* Evaluate dice binary operators. */
private static Tree<Node> evaluateDiceBinary(final Token.Type op, final EvaluatorResult left,
final EvaluatorResult right, final Context ctx) {
EvaluatorResult res = null;
switch (op) {
/*
* @TODO 10/09/17 Ben Culkin :DiceSimplify
*
* Figure out some way to simplify this sort of thing.
*
* ADDENDA: Replace the .diceVal.isList() with .isList()
*/
case DICEGROUP: {
if (left.type == DICE && !((DiceEvaluatorResult) left).diceVal.isList()) {
Die lhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) left).diceVal).scalar;
if (right.type == DICE && !((DiceEvaluatorResult) right).diceVal.isList()) {
Die rhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) right).diceVal).scalar;
Die simple = new SimpleDie(lhs, rhs);
res = new DiceEvaluatorResult(simple);
} else if (right.type == INT) {
res = new DiceEvaluatorResult(new SimpleDie(lhs, ((IntegerEvaluatorResult) right).value));
} else {
Errors.inst.printError(EK_EVAL_INVDGROUP, right.type.toString());
return new SimpleTree<>(Node.FAIL(right));
}
} else if (left.type == INT) {
IntegerEvaluatorResult irs = (IntegerEvaluatorResult) left;
if (right.type == DICE && !((DiceEvaluatorResult) right).diceVal.isList()) {
Die rhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) right).diceVal).scalar;
res = new DiceEvaluatorResult(new SimpleDie(irs.value, rhs));
} else if (right.type == INT) {
res = new DiceEvaluatorResult(new SimpleDie(irs.value, ((IntegerEvaluatorResult) right).value));
} else {
Errors.inst.printError(EK_EVAL_INVDGROUP, right.type.toString());
return new SimpleTree<>(Node.FAIL(right));
}
} else {
Errors.inst.printError(EK_EVAL_INVDGROUP, left.type.toString());
return new SimpleTree<>(Node.FAIL(left));
}
}
case DICECONCAT: {
if (left.type != DICE || ((DiceEvaluatorResult) left).diceVal.isList()) {
Errors.inst.printError(EK_EVAL_INVDICE, left.type.toString());
return new SimpleTree<>(Node.FAIL(left));
} else if (right.type != DICE || ((DiceEvaluatorResult) right).diceVal.isList()) {
Errors.inst.printError(EK_EVAL_INVDICE, right.type.toString());
return new SimpleTree<>(Node.FAIL(right));
} else {
Die lhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) left).diceVal).scalar;
Die rhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) right).diceVal).scalar;
res = new DiceEvaluatorResult(new CompoundDie(lhs, rhs));
}
break;
}
case DICELIST: {
if (left.type != DICE || ((DiceEvaluatorResult) left).diceVal.isList()) {
Errors.inst.printError(EK_EVAL_INVDICE, left.type.toString());
return new SimpleTree<>(Node.FAIL(left));
} else if (right.type != DICE || ((DiceEvaluatorResult) right).diceVal.isList()) {
Errors.inst.printError(EK_EVAL_INVDICE, right.type.toString());
return new SimpleTree<>(Node.FAIL(right));
} else {
Die lhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) left).diceVal).scalar;
Die rhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) right).diceVal).scalar;
res = new DiceEvaluatorResult(new SimpleDieList(lhs, rhs));
}
break;
}
default:
Errors.inst.printError(EK_EVAL_UNDICE, op.toString());
return new SimpleTree<>(Node.FAIL());
}
return new SimpleTree<>(new Node(Node.Type.RESULT, res));
}
/* Evaluate a binary math operator. */
private static Tree<Node> evaluateMathBinary(final Token.Type op, final EvaluatorResult left,
final EvaluatorResult right, final Context ctx) {
if (left.type == STRING || right.type == STRING) {
Errors.inst.printError(EK_EVAL_STRINGMATH);
return new SimpleTree<>(Node.FAIL());
} else if (left.type == FAILURE || right.type == FAILURE) {
return new SimpleTree<>(Node.FAIL());
} else if (left.type == INT && right.type != INT) {
Errors.inst.printError(EK_EVAL_MISMATH);
return new SimpleTree<>(Node.FAIL(right));
} else if (left.type == DEC && right.type != DEC) {
Errors.inst.printError(EK_EVAL_MISMATH);
return new SimpleTree<>(Node.FAIL(right));
} else if (left.type == FLOAT && right.type != FLOAT) {
Errors.inst.printError(EK_EVAL_MISMATH);
return new SimpleTree<>(Node.FAIL(right));
} else if (left.type == DICE && right.type != DICE) {
Errors.inst.printError(EK_EVAL_MISMATH);
return new SimpleTree<>(Node.FAIL(right));
} else if (right.type == INT && left.type != INT) {
Errors.inst.printError(EK_EVAL_MISMATH);
return new SimpleTree<>(Node.FAIL(left));
} else if (right.type == FLOAT && left.type != FLOAT) {
Errors.inst.printError(EK_EVAL_MISMATH);
return new SimpleTree<>(Node.FAIL(left));
} else if (right.type == DEC && left.type != DEC) {
Errors.inst.printError(EK_EVAL_MISMATH);
return new SimpleTree<>(Node.FAIL(left));
} else if (right.type == DICE && left.type != DICE) {
Errors.inst.printError(EK_EVAL_MISMATH);
return new SimpleTree<>(Node.FAIL(left));
}
EvaluatorResult res = null;
switch (op) {
case ADD: {
if (left.type == INT) {
long lval = ((IntegerEvaluatorResult) left).value;
long rval = ((IntegerEvaluatorResult) right).value;
res = new IntegerEvaluatorResult(lval + rval);
} else if (left.type == DEC) {
BigDecimal lval = ((DecimalEvaluatorResult) left).val;
BigDecimal rval = ((DecimalEvaluatorResult) right).val;
res = new DecimalEvaluatorResult(lval.add(rval));
} else if (left.type == DICE) {
if (((DiceEvaluatorResult) left).diceVal.isList()) {
Errors.inst.printError(EK_EVAL_INVDICE, left.toString());
return new SimpleTree<>(Node.FAIL(left));
} else if (((DiceEvaluatorResult) right).diceVal.isList()) {
Errors.inst.printError(EK_EVAL_INVDICE, right.toString());
return new SimpleTree<>(Node.FAIL(right));
}
Die lhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) left).diceVal).scalar;
Die rhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) right).diceVal).scalar;
res = new DiceEvaluatorResult(new MathDie(MathDie.MathOp.ADD, lhs, rhs));
} else {
res = new FloatEvaluatorResult(
((FloatEvaluatorResult) left).floatVal + ((FloatEvaluatorResult) right).floatVal);
}
break;
}
case SUBTRACT: {
if (left.type == INT) {
long lval = ((IntegerEvaluatorResult) left).value;
long rval = ((IntegerEvaluatorResult) right).value;
res = new IntegerEvaluatorResult(lval - rval);
} else if (left.type == DEC) {
BigDecimal lval = ((DecimalEvaluatorResult) left).val;
BigDecimal rval = ((DecimalEvaluatorResult) right).val;
res = new DecimalEvaluatorResult(lval.subtract(rval));
} else if (left.type == DICE) {
if (((DiceEvaluatorResult) left).diceVal.isList()) {
Errors.inst.printError(EK_EVAL_INVDICE, left.toString());
return new SimpleTree<>(Node.FAIL(left));
} else if (((DiceEvaluatorResult) right).diceVal.isList()) {
Errors.inst.printError(EK_EVAL_INVDICE, right.toString());
return new SimpleTree<>(Node.FAIL(right));
}
Die lhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) left).diceVal).scalar;
Die rhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) right).diceVal).scalar;
res = new DiceEvaluatorResult(new MathDie(MathDie.MathOp.SUBTRACT, lhs, rhs));
} else {
res = new FloatEvaluatorResult(
((FloatEvaluatorResult) left).floatVal - ((FloatEvaluatorResult) right).floatVal);
}
break;
}
case MULTIPLY: {
if (left.type == INT) {
long lval = ((IntegerEvaluatorResult) left).value;
long rval = ((IntegerEvaluatorResult) right).value;
res = new IntegerEvaluatorResult(lval * rval);
} else if (left.type == DEC) {
BigDecimal lval = ((DecimalEvaluatorResult) left).val;
BigDecimal rval = ((DecimalEvaluatorResult) right).val;
res = new DecimalEvaluatorResult(lval.multiply(rval));
} else if (left.type == DICE) {
if (((DiceEvaluatorResult) left).diceVal.isList()) {
Errors.inst.printError(EK_EVAL_INVDICE, left.toString());
return new SimpleTree<>(Node.FAIL(left));
} else if (((DiceEvaluatorResult) right).diceVal.isList()) {
Errors.inst.printError(EK_EVAL_INVDICE, right.toString());
return new SimpleTree<>(Node.FAIL(right));
}
Die lhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) left).diceVal).scalar;
Die rhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) right).diceVal).scalar;
res = new DiceEvaluatorResult(new MathDie(MathDie.MathOp.MULTIPLY, lhs, rhs));
} else {
res = new FloatEvaluatorResult(
((FloatEvaluatorResult) left).floatVal * ((FloatEvaluatorResult) right).floatVal);
}
break;
}
case DIVIDE: {
if (left.type == INT) {
long lval = ((IntegerEvaluatorResult) left).value;
long rval = ((IntegerEvaluatorResult) right).value;
if (rval == 0) {
Errors.inst.printError(EK_EVAL_DIVZERO);
res = new FailureEvaluatorResult(right);
} else {
res = new FloatEvaluatorResult(lval / rval);
}
} else if (left.type == DEC) {
BigDecimal lval = ((DecimalEvaluatorResult) left).val;
BigDecimal rval = ((DecimalEvaluatorResult) right).val;
if (rval.equals(BigDecimal.ZERO)) {
Errors.inst.printError(EK_EVAL_DIVZERO);
res = new FailureEvaluatorResult(right);
}
try {
res = new DecimalEvaluatorResult(lval.divide(rval));
} catch (ArithmeticException aex) {
Errors.inst.printError(EK_EVAL_ARITH, aex.getMessage());
SimpleTree<Node> cause = new SimpleTree<Node>(new Node(Type.BINOP, op),
new SimpleTree<>(new Node(Type.RESULT, left)),
new SimpleTree<>(new Node(Type.RESULT, right)));
res = new FailureEvaluatorResult(cause);
}
} else if (left.type == FLOAT) {
if (((FloatEvaluatorResult) right).floatVal == 0) {
Errors.inst.printError(EK_EVAL_DIVZERO);
res = new FailureEvaluatorResult(right);
} else {
res = new FloatEvaluatorResult(
((FloatEvaluatorResult) left).floatVal / ((FloatEvaluatorResult) right).floatVal);
}
} else {
Errors.inst.printError(EK_EVAL_DIVDICE);
return new SimpleTree<>(Node.FAIL());
}
break;
}
case IDIVIDE: {
if (left.type == INT) {
long lval = ((IntegerEvaluatorResult) left).value;
long rval = ((IntegerEvaluatorResult) right).value;
if (rval == 0) {
Errors.inst.printError(EK_EVAL_DIVZERO);
res = new FailureEvaluatorResult(right);
} else {
res = new IntegerEvaluatorResult((int) (lval / rval));
}
} else if (left.type == DEC) {
BigDecimal lval = ((DecimalEvaluatorResult) left).val;
BigDecimal rval = ((DecimalEvaluatorResult) right).val;
if (rval.equals(BigDecimal.ZERO)) {
Errors.inst.printError(EK_EVAL_DIVZERO);
res = new FailureEvaluatorResult(right);
}
res = new DecimalEvaluatorResult(lval.divideToIntegralValue(rval));
} else if (left.type == FLOAT) {
if (((FloatEvaluatorResult) right).floatVal == 0) {
Errors.inst.printError(EK_EVAL_DIVZERO);
res = new FailureEvaluatorResult(right);
} else {
res = new IntegerEvaluatorResult(
(int) (((FloatEvaluatorResult) left).floatVal / ((FloatEvaluatorResult) right).floatVal));
}
} else {
Errors.inst.printError(EK_EVAL_DIVDICE);
return new SimpleTree<>(Node.FAIL());
}
break;
}
default:
Errors.inst.printError(EK_EVAL_UNMATH, op.toString());
return new SimpleTree<>(Node.FAIL());
}
return new SimpleTree<>(new Node(Node.Type.RESULT, res));
}
/* Evaluate a token reference. */
private Tree<Node> evaluateTokenRef(final Token tk, final Context ctx) {
EvaluatorResult res = null;
switch (tk.type) {
case INT_LIT:
res = new IntegerEvaluatorResult(tk.intValue);
break;
case FLOAT_LIT:
res = new FloatEvaluatorResult(((FloatToken) tk).floatValue);
break;
case DICE_LIT:
res = new DiceEvaluatorResult(((DiceToken) tk).diceValue);
break;
case DEC_LIT:
res = new DecimalEvaluatorResult(((DecimalToken) tk).val);
break;
case STRING_LIT:
res = new StringEvaluatorResult(eng.getStringLiteral((int) tk.intValue));
break;
default:
Errors.inst.printError(EK_EVAL_UNTOK, tk.type.toString());
res = new EvaluatorResult(FAILURE);
}
return new SimpleTree<>(new Node(Node.Type.RESULT, res));
}
}
|