summaryrefslogtreecommitdiff
path: root/base/src/bjc/dicelang
diff options
context:
space:
mode:
authorstudent <student@69.161.224.78>2018-02-12 17:17:37 -0500
committerstudent <student@69.161.224.78>2018-02-12 17:17:37 -0500
commite1480c4e706d4902f9865f6119e71e30b4173153 (patch)
tree01377a2021af9409f02b7bd43dba8072b216948d /base/src/bjc/dicelang
parent26b0cf727656b4d5984f04d73566661644c78fdd (diff)
Refactor EvaluatorResult
Diffstat (limited to 'base/src/bjc/dicelang')
-rw-r--r--base/src/bjc/dicelang/DiceLangEngine.java20
-rw-r--r--base/src/bjc/dicelang/EvaluatorResult.java223
-rw-r--r--base/src/bjc/dicelang/Node.java38
-rw-r--r--base/src/bjc/dicelang/eval/DiceEvaluatorResult.java32
-rw-r--r--base/src/bjc/dicelang/eval/Evaluator.java (renamed from base/src/bjc/dicelang/Evaluator.java)133
-rw-r--r--base/src/bjc/dicelang/eval/EvaluatorResult.java82
-rw-r--r--base/src/bjc/dicelang/eval/FailureEvaluatorResult.java55
-rw-r--r--base/src/bjc/dicelang/eval/FloatEvaluatorResult.java43
-rw-r--r--base/src/bjc/dicelang/eval/StringEvaluatorResult.java44
9 files changed, 365 insertions, 305 deletions
diff --git a/base/src/bjc/dicelang/DiceLangEngine.java b/base/src/bjc/dicelang/DiceLangEngine.java
index ebdb07f..e7ba4e8 100644
--- a/base/src/bjc/dicelang/DiceLangEngine.java
+++ b/base/src/bjc/dicelang/DiceLangEngine.java
@@ -8,6 +8,10 @@ import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import bjc.dicelang.eval.DiceEvaluatorResult;
+import bjc.dicelang.eval.Evaluator;
+import bjc.dicelang.eval.EvaluatorResult;
+import bjc.dicelang.eval.FailureEvaluatorResult;
import bjc.dicelang.scl.StreamEngine;
import bjc.utils.data.ITree;
import bjc.utils.funcdata.FunctionalList;
@@ -538,11 +542,15 @@ public class DiceLangEngine {
System.out.printf(" (result is %s", res);
if (res.type == EvaluatorResult.Type.DICE) {
- System.out.printf(" (sample roll %s)", res.diceVal.value());
+ String value = ((DiceEvaluatorResult) res).diceVal.value();
+
+ System.out.printf(" (sample roll %s)", value);
}
- if (res.origVal != null) {
- System.out.printf(" (original tree is %s)", res.origVal);
+ if (res.type == EvaluatorResult.Type.FAILURE) {
+ ITree<Node> otree = ((FailureEvaluatorResult) res).origVal;
+
+ System.out.printf(" (original tree is %s)", otree);
}
System.out.printf(")");
@@ -560,7 +568,9 @@ public class DiceLangEngine {
System.out.printf("\t\tEvaluates to %s", res);
if (res.type == EvaluatorResult.Type.DICE) {
- System.out.println("\t\t (sample roll " + res.diceVal.value() + ")");
+ String value = ((DiceEvaluatorResult) res).diceVal.value();
+
+ System.out.println("\t\t (sample roll " + value + ")");
}
}
}
@@ -653,7 +663,7 @@ public class DiceLangEngine {
}
/* Get a string literal from the string literal table. */
- String getStringLiteral(final int key) {
+ public String getStringLiteral(final int key) {
return stringLits.get(key);
}
diff --git a/base/src/bjc/dicelang/EvaluatorResult.java b/base/src/bjc/dicelang/EvaluatorResult.java
deleted file mode 100644
index 0d0706a..0000000
--- a/base/src/bjc/dicelang/EvaluatorResult.java
+++ /dev/null
@@ -1,223 +0,0 @@
-package bjc.dicelang;
-
-import bjc.dicelang.dice.Die;
-import bjc.dicelang.dice.DiceExpression;
-import bjc.dicelang.dice.DieList;
-import bjc.dicelang.dice.ListDiceExpression;
-import bjc.dicelang.dice.ScalarDiceExpression;
-import bjc.utils.data.ITree;
-import bjc.utils.data.Tree;
-
-/*
- * @TODO 10/09/17 Ben Culkin :EvalResultReorg
- *
- * Again, split it into separate classes based off of the type.
- */
-/**
- * The result from the evaluator.
- *
- * @author EVE
- *
- */
-public class EvaluatorResult {
- /**
- * The type of the result.
- *
- * @author EVE
- *
- */
- public static enum Type {
- /**
- * The type of a failure.
- */
- FAILURE,
- /**
- * The type of an integer.
- */
- INT,
- /**
- * The type of a float.
- */
- FLOAT,
- /**
- * The type of a dice.
- */
- DICE,
- /**
- * The type of a string.
- */
- STRING
- }
-
- /**
- * The type of the result.
- */
- public final EvaluatorResult.Type type;
-
- // These may or may not have values based
- // off of the result type
- /**
- * The integer value of the result.
- */
- public long intVal;
- /**
- * The float value of the result.
- */
- public double floatVal;
- /**
- * The dice value of the result.
- */
- public DiceExpression diceVal;
- /**
- * The string value of the result.
- */
- public String stringVal;
-
- /**
- * Original node data
- */
- public ITree<Node> origVal;
-
- /**
- * Create a new result.
- *
- * @param typ
- * The type of the result.
- */
- public EvaluatorResult(final EvaluatorResult.Type typ) {
- type = typ;
- }
-
- /**
- * Create a new result.
- *
- * @param typ
- * The type of the result.
- *
- * @param orig
- * The original value of the result.
- */
- public EvaluatorResult(final EvaluatorResult.Type typ, final ITree<Node> orig) {
- this(typ);
-
- origVal = orig;
- }
-
- /**
- * Create a new result.
- *
- * @param typ
- * The type of the result.
- *
- * @param orig
- * The original value of the result.
- */
- public EvaluatorResult(final EvaluatorResult.Type typ, final Node orig) {
- this(typ, new Tree<>(orig));
- }
-
- /**
- * Create a new result.
- *
- * @param typ
- * @param orig
- */
- public EvaluatorResult(final EvaluatorResult.Type typ, final EvaluatorResult orig) {
- this(typ, new Node(Node.Type.RESULT, orig));
- }
-
- /**
- * Create a new result.
- *
- * @param typ
- * @param iVal
- */
- public EvaluatorResult(final EvaluatorResult.Type typ, final long iVal) {
- this(typ);
-
- intVal = iVal;
- }
-
- /**
- * Create a new result.
- *
- * @param typ
- * @param dVal
- */
- public EvaluatorResult(final EvaluatorResult.Type typ, final double dVal) {
- this(typ);
-
- floatVal = dVal;
- }
-
- /**
- * Create a new result.
- *
- * @param typ
- * @param dVal
- */
- public EvaluatorResult(final EvaluatorResult.Type typ, final DiceExpression dVal) {
- this(typ);
-
- diceVal = dVal;
- }
-
- /**
- * Create a new result.
- *
- * @param typ
- * @param dVal
- */
- public EvaluatorResult(final EvaluatorResult.Type typ, final Die dVal) {
- this(typ);
-
- diceVal = new ScalarDiceExpression(dVal);
- }
-
- /**
- * Create a new result.
- *
- * @param typ
- * @param dVal
- */
- public EvaluatorResult(final EvaluatorResult.Type typ, final DieList dVal) {
- this(typ);
-
- diceVal = new ListDiceExpression(dVal);
- }
-
- /**
- * Create a new result.
- *
- * @param typ
- * @param strang
- */
- public EvaluatorResult(final EvaluatorResult.Type typ, final String strang) {
- this(typ);
-
- stringVal = strang;
- }
-
- @Override
- public String toString() {
- switch (type) {
- case INT:
- return type.toString() + "(" + intVal + ")";
-
- case FLOAT:
- return type.toString() + "(" + floatVal + ")";
-
- case DICE:
- return type.toString() + "(" + diceVal + ")";
-
- case STRING:
- return type.toString() + "(" + stringVal + ")";
-
- case FAILURE:
- return type.toString();
-
- default:
- return "Unknown result type " + type.toString();
- }
- }
-}
diff --git a/base/src/bjc/dicelang/Node.java b/base/src/bjc/dicelang/Node.java
index ff0c83d..7e97f9c 100644
--- a/base/src/bjc/dicelang/Node.java
+++ b/base/src/bjc/dicelang/Node.java
@@ -1,12 +1,14 @@
package bjc.dicelang;
-import static bjc.dicelang.EvaluatorResult.Type.FAILURE;
-
+import bjc.dicelang.eval.EvaluatorResult;
+import bjc.dicelang.eval.FailureEvaluatorResult;
import bjc.utils.data.ITree;
/*
- * @TODO 10/09/17 Ben Culkin :NodeReorg Same thing, different class. Split into
- * subclasses based off of the type values.
+ * @TODO 10/09/17 Ben Culkin :NodeReorg
+ *
+ * Same thing, different class. Split into subclasses based off of the type
+ * values.
*/
/**
* Represents a node in the AST.
@@ -106,24 +108,30 @@ public class Node {
return super.hashCode();
}
- static Node FAIL(final EvaluatorResult res) {
- EvaluatorResult eres = new EvaluatorResult(FAILURE, new Node(Type.RESULT, res));
+ public static Node FAIL(final EvaluatorResult res) {
+ Node nd = new Node(Type.RESULT, res);
+
+ EvaluatorResult eres = new FailureEvaluatorResult(nd);
+
return new Node(Type.RESULT, eres);
}
- static Node FAIL(final Node orig) {
- return new Node(Type.RESULT, new EvaluatorResult(FAILURE, orig));
+ public static Node FAIL(final Node orig) {
+ FailureEvaluatorResult res = new FailureEvaluatorResult(orig);
+
+ return new Node(Type.RESULT, res);
}
- static Node FAIL(final ITree<Node> orig) {
- return new Node(Type.RESULT, new EvaluatorResult(FAILURE, orig));
+ public static Node FAIL(final ITree<Node> orig) {
+ FailureEvaluatorResult res = new FailureEvaluatorResult(orig);
+
+ return new Node(Type.RESULT, res);
}
- /*
- * @TODO 10/09/17 Ben Culkin :NodeFAIL These methods should be moved to Node.
- */
/* Create a failing node. */
- static Node FAIL() {
- return new Node(Type.RESULT, new EvaluatorResult(FAILURE));
+ public static Node FAIL() {
+ FailureEvaluatorResult res = new FailureEvaluatorResult();
+
+ return new Node(Type.RESULT, res);
}
}
diff --git a/base/src/bjc/dicelang/eval/DiceEvaluatorResult.java b/base/src/bjc/dicelang/eval/DiceEvaluatorResult.java
new file mode 100644
index 0000000..8e50333
--- /dev/null
+++ b/base/src/bjc/dicelang/eval/DiceEvaluatorResult.java
@@ -0,0 +1,32 @@
+package bjc.dicelang.eval;
+
+import bjc.dicelang.dice.DiceExpression;
+import bjc.dicelang.dice.Die;
+import bjc.dicelang.dice.DieList;
+import bjc.dicelang.dice.ListDiceExpression;
+import bjc.dicelang.dice.ScalarDiceExpression;
+
+public class DiceEvaluatorResult extends EvaluatorResult {
+ /**
+ * The dice value of the result.
+ */
+ public DiceExpression diceVal;
+
+ public DiceEvaluatorResult(DiceExpression expr) {
+ super(Type.DICE);
+
+ diceVal = expr;
+ }
+
+ public DiceEvaluatorResult(Die die) {
+ this(new ScalarDiceExpression(die));
+ }
+
+ public DiceEvaluatorResult(DieList list) {
+ this(new ListDiceExpression(list));
+ }
+
+ public boolean isList() {
+ return diceVal.isList();
+ }
+}
diff --git a/base/src/bjc/dicelang/Evaluator.java b/base/src/bjc/dicelang/eval/Evaluator.java
index aa2d2a0..4cc2a1e 100644
--- a/base/src/bjc/dicelang/Evaluator.java
+++ b/base/src/bjc/dicelang/eval/Evaluator.java
@@ -1,10 +1,16 @@
-package bjc.dicelang;
+package bjc.dicelang.eval;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.function.Consumer;
+import bjc.dicelang.DiceLangEngine;
+import bjc.dicelang.DiceToken;
+import bjc.dicelang.Errors;
+import bjc.dicelang.FloatToken;
+import bjc.dicelang.Node;
+import bjc.dicelang.Token;
import bjc.dicelang.dice.CompoundDie;
import bjc.dicelang.dice.Die;
import bjc.dicelang.dice.MathDie;
@@ -20,11 +26,7 @@ import bjc.utils.data.TopDownTransformResult;
import bjc.utils.data.Tree;
import static bjc.dicelang.Errors.ErrorKey.*;
-import static bjc.dicelang.EvaluatorResult.Type.DICE;
-import static bjc.dicelang.EvaluatorResult.Type.FAILURE;
-import static bjc.dicelang.EvaluatorResult.Type.FLOAT;
-import static bjc.dicelang.EvaluatorResult.Type.INT;
-import static bjc.dicelang.EvaluatorResult.Type.STRING;
+import static bjc.dicelang.eval.EvaluatorResult.Type.*;
/*
* @TODO 10/09/17 Ben Culkin :EvaluatorSplit
@@ -221,7 +223,7 @@ public class Evaluator {
case INT:
/* Coerce ints to doubles if we need to. */
if (curLevel == CoerceSteps.DOUBLE) {
- nd.resultVal = new EvaluatorResult(FLOAT, (double) res.intVal);
+ nd.resultVal = new FloatEvaluatorResult((double) res.intVal);
}
default:
/* Do nothing */
@@ -239,7 +241,7 @@ public class Evaluator {
Errors.inst.printError(EK_EVAL_INVDCREATE, opr.type.toString());
}
- final EvaluatorResult sres = new EvaluatorResult(DICE, new ScalarDie(opr.intVal));
+ final EvaluatorResult sres = new DiceEvaluatorResult(new ScalarDie(opr.intVal));
return new Tree<>(new Node(Node.Type.RESULT, sres));
case DICEFUDGE:
final EvaluatorResult oprn = ast.getChild(0).getHead().resultVal;
@@ -248,7 +250,7 @@ public class Evaluator {
Errors.inst.printError(EK_EVAL_INVDCREATE, oprn.type.toString());
}
- final EvaluatorResult fres = new EvaluatorResult(DICE, new ScalarDie(oprn.intVal));
+ final EvaluatorResult fres = new DiceEvaluatorResult(new ScalarDie(oprn.intVal));
return new Tree<>(new Node(Node.Type.RESULT, fres));
default:
Errors.inst.printError(EK_EVAL_INVUNARY, ast.getHead().operatorType.toString());
@@ -301,7 +303,7 @@ public class Evaluator {
return new Tree<>(Node.FAIL(left));
}
- final String strang = left.stringVal;
+ final String strang = ((StringEvaluatorResult) left).stringVal;
switch (op) {
case STRCAT:
@@ -310,8 +312,8 @@ public class Evaluator {
return new Tree<>(Node.FAIL(right));
}
- final String strung = right.stringVal;
- final EvaluatorResult cres = new EvaluatorResult(STRING, strang + strung);
+ final String strung = ((StringEvaluatorResult) right).stringVal;
+ final EvaluatorResult cres = new StringEvaluatorResult(strang + strung);
return new Tree<>(new Node(Node.Type.RESULT, cres));
case STRREP:
@@ -327,7 +329,7 @@ public class Evaluator {
res += strang;
}
- return new Tree<>(new Node(Node.Type.RESULT, new EvaluatorResult(STRING, res)));
+ return new Tree<>(new Node(Node.Type.RESULT, new StringEvaluatorResult(res)));
default:
Errors.inst.printError(EK_EVAL_UNSTRING, op.toString());
return new Tree<>(Node.FAIL());
@@ -344,30 +346,32 @@ public class Evaluator {
* @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 && !left.diceVal.isList()) {
- Die lhs = ((ScalarDiceExpression) left.diceVal).scalar;
+ if (left.type == DICE && !((DiceEvaluatorResult) left).diceVal.isList()) {
+ Die lhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) left).diceVal).scalar;
- if (right.type == DICE && !right.diceVal.isList()) {
- Die rhs = ((ScalarDiceExpression) right.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 EvaluatorResult(DICE, simple);
+ res = new DiceEvaluatorResult(simple);
} else if (right.type == INT) {
- res = new EvaluatorResult(DICE, new SimpleDie(lhs, right.intVal));
+ res = new DiceEvaluatorResult(new SimpleDie(lhs, right.intVal));
} else {
Errors.inst.printError(EK_EVAL_INVDGROUP, right.type.toString());
return new Tree<>(Node.FAIL(right));
}
} else if (left.type == INT) {
- if (right.type == DICE && !right.diceVal.isList()) {
- Die rhs = ((ScalarDiceExpression) right.diceVal).scalar;
+ if (right.type == DICE && !((DiceEvaluatorResult) right).diceVal.isList()) {
+ Die rhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) right).diceVal).scalar;
- res = new EvaluatorResult(DICE, new SimpleDie(left.intVal, rhs));
+ res = new DiceEvaluatorResult(new SimpleDie(left.intVal, rhs));
} else if (right.type == INT) {
- res = new EvaluatorResult(DICE, new SimpleDie(left.intVal, right.intVal));
+ res = new DiceEvaluatorResult(new SimpleDie(left.intVal, right.intVal));
} else {
Errors.inst.printError(EK_EVAL_INVDGROUP, right.type.toString());
return new Tree<>(Node.FAIL(right));
@@ -378,33 +382,33 @@ public class Evaluator {
}
case DICECONCAT:
- if (left.type != DICE || left.diceVal.isList()) {
+ if (left.type != DICE || ((DiceEvaluatorResult) left).diceVal.isList()) {
Errors.inst.printError(EK_EVAL_INVDICE, left.type.toString());
return new Tree<>(Node.FAIL(left));
- } else if (right.type != DICE || right.diceVal.isList()) {
+ } else if (right.type != DICE || ((DiceEvaluatorResult) right).diceVal.isList()) {
Errors.inst.printError(EK_EVAL_INVDICE, right.type.toString());
return new Tree<>(Node.FAIL(right));
} else {
- Die lhs = ((ScalarDiceExpression) left.diceVal).scalar;
- Die rhs = ((ScalarDiceExpression) right.diceVal).scalar;
+ Die lhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) left).diceVal).scalar;
+ Die rhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) right).diceVal).scalar;
- res = new EvaluatorResult(DICE, new CompoundDie(lhs, rhs));
+ res = new DiceEvaluatorResult(new CompoundDie(lhs, rhs));
}
break;
case DICELIST:
- if (left.type != DICE || left.diceVal.isList()) {
+ if (left.type != DICE || ((DiceEvaluatorResult) left).diceVal.isList()) {
Errors.inst.printError(EK_EVAL_INVDICE, left.type.toString());
return new Tree<>(Node.FAIL(left));
- } else if (right.type != DICE || right.diceVal.isList()) {
+ } else if (right.type != DICE || ((DiceEvaluatorResult) right).diceVal.isList()) {
Errors.inst.printError(EK_EVAL_INVDICE, right.type.toString());
return new Tree<>(Node.FAIL(right));
} else {
- Die lhs = ((ScalarDiceExpression) left.diceVal).scalar;
- Die rhs = ((ScalarDiceExpression) right.diceVal).scalar;
+ Die lhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) left).diceVal).scalar;
+ Die rhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) right).diceVal).scalar;
- res = new EvaluatorResult(DICE, new SimpleDieList(lhs, rhs));
+ res = new DiceEvaluatorResult(new SimpleDieList(lhs, rhs));
}
break;
@@ -452,20 +456,21 @@ public class Evaluator {
if (left.type == INT) {
res = new EvaluatorResult(INT, left.intVal + right.intVal);
} else if (left.type == DICE) {
- if (left.diceVal.isList()) {
+ if (((DiceEvaluatorResult) left).diceVal.isList()) {
Errors.inst.printError(EK_EVAL_INVDICE, left.toString());
return new Tree<>(Node.FAIL(left));
- } else if (right.diceVal.isList()) {
+ } else if (((DiceEvaluatorResult) right).diceVal.isList()) {
Errors.inst.printError(EK_EVAL_INVDICE, right.toString());
return new Tree<>(Node.FAIL(right));
}
- Die lhs = ((ScalarDiceExpression) left.diceVal).scalar;
- Die rhs = ((ScalarDiceExpression) right.diceVal).scalar;
+ Die lhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) left).diceVal).scalar;
+ Die rhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) right).diceVal).scalar;
- res = new EvaluatorResult(DICE, new MathDie(MathDie.MathOp.ADD, lhs, rhs));
+ res = new DiceEvaluatorResult(new MathDie(MathDie.MathOp.ADD, lhs, rhs));
} else {
- res = new EvaluatorResult(FLOAT, left.floatVal + right.floatVal);
+ res = new FloatEvaluatorResult(
+ ((FloatEvaluatorResult) left).floatVal + ((FloatEvaluatorResult) right).floatVal);
}
break;
@@ -474,20 +479,21 @@ public class Evaluator {
if (left.type == INT) {
res = new EvaluatorResult(INT, left.intVal - right.intVal);
} else if (left.type == DICE) {
- if (left.diceVal.isList()) {
+ if (((DiceEvaluatorResult) left).diceVal.isList()) {
Errors.inst.printError(EK_EVAL_INVDICE, left.toString());
return new Tree<>(Node.FAIL(left));
- } else if (right.diceVal.isList()) {
+ } else if (((DiceEvaluatorResult) right).diceVal.isList()) {
Errors.inst.printError(EK_EVAL_INVDICE, right.toString());
return new Tree<>(Node.FAIL(right));
}
- Die lhs = ((ScalarDiceExpression) left.diceVal).scalar;
- Die rhs = ((ScalarDiceExpression) right.diceVal).scalar;
+ Die lhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) left).diceVal).scalar;
+ Die rhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) right).diceVal).scalar;
- res = new EvaluatorResult(DICE, new MathDie(MathDie.MathOp.SUBTRACT, lhs, rhs));
+ res = new DiceEvaluatorResult(new MathDie(MathDie.MathOp.SUBTRACT, lhs, rhs));
} else {
- res = new EvaluatorResult(FLOAT, left.floatVal - right.floatVal);
+ res = new FloatEvaluatorResult(
+ ((FloatEvaluatorResult) left).floatVal - ((FloatEvaluatorResult) right).floatVal);
}
break;
@@ -496,20 +502,21 @@ public class Evaluator {
if (left.type == INT) {
res = new EvaluatorResult(INT, left.intVal * right.intVal);
} else if (left.type == DICE) {
- if (left.diceVal.isList()) {
+ if (((DiceEvaluatorResult) left).diceVal.isList()) {
Errors.inst.printError(EK_EVAL_INVDICE, left.toString());
return new Tree<>(Node.FAIL(left));
- } else if (right.diceVal.isList()) {
+ } else if (((DiceEvaluatorResult) right).diceVal.isList()) {
Errors.inst.printError(EK_EVAL_INVDICE, right.toString());
return new Tree<>(Node.FAIL(right));
}
- Die lhs = ((ScalarDiceExpression) left.diceVal).scalar;
- Die rhs = ((ScalarDiceExpression) right.diceVal).scalar;
+ Die lhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) left).diceVal).scalar;
+ Die rhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) right).diceVal).scalar;
- res = new EvaluatorResult(DICE, new MathDie(MathDie.MathOp.MULTIPLY, lhs, rhs));
+ res = new DiceEvaluatorResult(new MathDie(MathDie.MathOp.MULTIPLY, lhs, rhs));
} else {
- res = new EvaluatorResult(FLOAT, left.floatVal * right.floatVal);
+ res = new FloatEvaluatorResult(
+ ((FloatEvaluatorResult) left).floatVal * ((FloatEvaluatorResult) right).floatVal);
}
break;
@@ -518,16 +525,17 @@ public class Evaluator {
if (left.type == INT) {
if (right.intVal == 0) {
Errors.inst.printError(EK_EVAL_DIVZERO);
- res = new EvaluatorResult(FAILURE, right);
+ res = new FailureEvaluatorResult(right);
} else {
res = new EvaluatorResult(FLOAT, left.intVal / right.intVal);
}
} else if (left.type == FLOAT) {
- if (right.floatVal == 0) {
+ if (((FloatEvaluatorResult) right).floatVal == 0) {
Errors.inst.printError(EK_EVAL_DIVZERO);
- res = new EvaluatorResult(FAILURE, right);
+ res = new FailureEvaluatorResult(right);
} else {
- res = new EvaluatorResult(FLOAT, left.floatVal / right.floatVal);
+ res = new FloatEvaluatorResult(
+ ((FloatEvaluatorResult) left).floatVal / ((FloatEvaluatorResult) right).floatVal);
}
} else {
Errors.inst.printError(EK_EVAL_DIVDICE);
@@ -540,16 +548,17 @@ public class Evaluator {
if (left.type == INT) {
if (right.intVal == 0) {
Errors.inst.printError(EK_EVAL_DIVZERO);
- res = new EvaluatorResult(FAILURE, right);
+ res = new FailureEvaluatorResult(right);
} else {
res = new EvaluatorResult(INT, (int) (left.intVal / right.intVal));
}
} else if (left.type == FLOAT) {
- if (right.floatVal == 0) {
+ if (((FloatEvaluatorResult) right).floatVal == 0) {
Errors.inst.printError(EK_EVAL_DIVZERO);
- res = new EvaluatorResult(FAILURE, right);
+ res = new FailureEvaluatorResult(right);
} else {
- res = new EvaluatorResult(INT, (int) (left.floatVal / right.floatVal));
+ res = new EvaluatorResult(INT,
+ (int) (((FloatEvaluatorResult) left).floatVal / ((FloatEvaluatorResult) right).floatVal));
}
} else {
Errors.inst.printError(EK_EVAL_DIVDICE);
@@ -575,13 +584,13 @@ public class Evaluator {
res = new EvaluatorResult(INT, tk.intValue);
break;
case FLOAT_LIT:
- res = new EvaluatorResult(FLOAT, ((FloatToken)tk).floatValue);
+ res = new FloatEvaluatorResult(((FloatToken) tk).floatValue);
break;
case DICE_LIT:
- res = new EvaluatorResult(DICE, ((DiceToken) tk).diceValue);
+ res = new DiceEvaluatorResult(((DiceToken) tk).diceValue);
break;
case STRING_LIT:
- res = new EvaluatorResult(STRING, eng.getStringLiteral((int) tk.intValue));
+ res = new StringEvaluatorResult(eng.getStringLiteral((int) tk.intValue));
break;
default:
Errors.inst.printError(EK_EVAL_UNTOK, tk.type.toString());
diff --git a/base/src/bjc/dicelang/eval/EvaluatorResult.java b/base/src/bjc/dicelang/eval/EvaluatorResult.java
new file mode 100644
index 0000000..3ac0202
--- /dev/null
+++ b/base/src/bjc/dicelang/eval/EvaluatorResult.java
@@ -0,0 +1,82 @@
+package bjc.dicelang.eval;
+
+/*
+ * @TODO 10/09/17 Ben Culkin :EvalResultReorg
+ *
+ * Again, split it into separate classes based off of the type.
+ */
+/**
+ * The result from the evaluator.
+ *
+ * @author EVE
+ *
+ */
+public class EvaluatorResult {
+ /**
+ * The type of the result.
+ *
+ * @author EVE
+ *
+ */
+ public static enum Type {
+ /**
+ * The type of a failure.
+ */
+ FAILURE,
+ /**
+ * The type of an integer.
+ */
+ INT,
+ /**
+ * The type of a float.
+ */
+ FLOAT,
+ /**
+ * The type of a dice.
+ */
+ DICE,
+ /**
+ * The type of a string.
+ */
+ STRING
+ }
+
+ /**
+ * The type of the result.
+ */
+ public final EvaluatorResult.Type type;
+
+ // These may or may not have values based
+ // off of the result type
+ /**
+ * The integer value of the result.
+ */
+ public long intVal;
+
+ /**
+ * Create a new result.
+ *
+ * @param typ
+ * The type of the result.
+ */
+ protected EvaluatorResult(final EvaluatorResult.Type typ) {
+ type = typ;
+ }
+
+ /**
+ * Create a new result.
+ *
+ * @param typ
+ * @param iVal
+ */
+ public EvaluatorResult(final EvaluatorResult.Type typ, final long iVal) {
+ this(typ);
+
+ intVal = iVal;
+ }
+
+ @Override
+ public String toString() {
+ return type.toString();
+ }
+}
diff --git a/base/src/bjc/dicelang/eval/FailureEvaluatorResult.java b/base/src/bjc/dicelang/eval/FailureEvaluatorResult.java
new file mode 100644
index 0000000..3b641db
--- /dev/null
+++ b/base/src/bjc/dicelang/eval/FailureEvaluatorResult.java
@@ -0,0 +1,55 @@
+package bjc.dicelang.eval;
+
+import bjc.dicelang.Node;
+import bjc.utils.data.ITree;
+import bjc.utils.data.Tree;
+
+public class FailureEvaluatorResult extends EvaluatorResult {
+ /**
+ * Original node data
+ */
+ public ITree<Node> origVal;
+
+ public FailureEvaluatorResult() {
+ super(Type.FAILURE);
+ }
+
+ public FailureEvaluatorResult(final ITree<Node> orig) {
+ super(Type.FAILURE);
+
+ origVal = orig;
+ }
+
+ public FailureEvaluatorResult(final Node orig) {
+ this(new Tree<>(orig));
+ }
+
+ public FailureEvaluatorResult(EvaluatorResult right) {
+ this(new Node(Node.Type.RESULT, right));
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((origVal == null) ? 0 : origVal.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ FailureEvaluatorResult other = (FailureEvaluatorResult) obj;
+ if (origVal == null) {
+ if (other.origVal != null)
+ return false;
+ } else if (!origVal.equals(other.origVal))
+ return false;
+ return true;
+ }
+}
diff --git a/base/src/bjc/dicelang/eval/FloatEvaluatorResult.java b/base/src/bjc/dicelang/eval/FloatEvaluatorResult.java
new file mode 100644
index 0000000..7fbbcdc
--- /dev/null
+++ b/base/src/bjc/dicelang/eval/FloatEvaluatorResult.java
@@ -0,0 +1,43 @@
+package bjc.dicelang.eval;
+
+public class FloatEvaluatorResult extends EvaluatorResult {
+ /**
+ * The float value of the result.
+ */
+ public double floatVal;
+
+ public FloatEvaluatorResult(double val) {
+ super(Type.FLOAT);
+
+ floatVal = val;
+ }
+
+ @Override
+ public String toString() {
+ return super.toString() + "(" + floatVal + ")";
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ long temp;
+ temp = Double.doubleToLongBits(floatVal);
+ result = prime * result + (int) (temp ^ (temp >>> 32));
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ FloatEvaluatorResult other = (FloatEvaluatorResult) obj;
+ if (Double.doubleToLongBits(floatVal) != Double.doubleToLongBits(other.floatVal))
+ return false;
+ return true;
+ }
+}
diff --git a/base/src/bjc/dicelang/eval/StringEvaluatorResult.java b/base/src/bjc/dicelang/eval/StringEvaluatorResult.java
new file mode 100644
index 0000000..870fd01
--- /dev/null
+++ b/base/src/bjc/dicelang/eval/StringEvaluatorResult.java
@@ -0,0 +1,44 @@
+package bjc.dicelang.eval;
+
+public class StringEvaluatorResult extends EvaluatorResult {
+ /**
+ * The string value of the result.
+ */
+ public String stringVal;
+
+ public StringEvaluatorResult(String strang) {
+ super(Type.STRING);
+
+ stringVal = strang;
+ }
+
+ @Override
+ public String toString() {
+ return super.toString() + "(" + stringVal + ")";
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((stringVal == null) ? 0 : stringVal.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ StringEvaluatorResult other = (StringEvaluatorResult) obj;
+ if (stringVal == null) {
+ if (other.stringVal != null)
+ return false;
+ } else if (!stringVal.equals(other.stringVal))
+ return false;
+ return true;
+ }
+}