summaryrefslogtreecommitdiff
path: root/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTEvaluator.java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-04-18 08:34:32 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-04-18 08:34:32 -0400
commit9ce39956fa1702f157c347dc4b8807d9b5dd2185 (patch)
treed981c0010a92660a1f0501431c4a3bc02d94e56d /dice-lang/src/main/java/bjc/dicelang/ast/DiceASTEvaluator.java
parent7c222f25d4b2d9f3b149d880f0e1acf8d673e4f5 (diff)
Reimplemented basic optimization.
Diffstat (limited to 'dice-lang/src/main/java/bjc/dicelang/ast/DiceASTEvaluator.java')
-rw-r--r--dice-lang/src/main/java/bjc/dicelang/ast/DiceASTEvaluator.java50
1 files changed, 8 insertions, 42 deletions
diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTEvaluator.java b/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTEvaluator.java
index 016fa8a..411212e 100644
--- a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTEvaluator.java
+++ b/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTEvaluator.java
@@ -1,7 +1,6 @@
package bjc.dicelang.ast;
import bjc.dicelang.ComplexDice;
-import bjc.dicelang.ast.nodes.DiceASTType;
import bjc.dicelang.ast.nodes.DiceLiteralNode;
import bjc.dicelang.ast.nodes.DiceLiteralType;
import bjc.dicelang.ast.nodes.IDiceASTNode;
@@ -59,7 +58,11 @@ public class DiceASTEvaluator {
});
operatorCollapsers.put(OperatorDiceNode.COMPOUND,
- DiceASTEvaluator::parseCompound);
+ new ArithmeticCollapser(OperatorDiceNode.COMPOUND,
+ (left, right) -> {
+ return Integer.parseInt(Integer.toString(left)
+ + Integer.toString(right));
+ }));
operatorCollapsers.put(OperatorDiceNode.GROUP,
DiceASTEvaluator::parseGroup);
@@ -128,7 +131,7 @@ public class DiceASTEvaluator {
switch (literalType) {
case DICE:
- return ((DiceLiteralNode) leafNode).getValue();
+ return ((DiceLiteralNode) leafNode).getValue().roll();
case INTEGER:
return ((IntegerLiteralNode) leafNode).getValue();
default:
@@ -156,7 +159,7 @@ public class DiceASTEvaluator {
return nameNode.bindRight((nameTree) -> {
return valueNode.bind((valueValue, valueTree) -> {
- if (containsSimpleVariable(nameTree)) {
+ if (DiceASTUtils.containsSimpleVariable(nameTree)) {
String varName = nameTree.transformHead((nameNod) -> {
return ((VariableDiceNode) nameNod).getVariable();
});
@@ -166,46 +169,9 @@ public class DiceASTEvaluator {
return new Pair<>(valueValue, nameTree);
}
- throw new IllegalStateException(
- "Statement that shouldn't be hit was hit.");
- });
- });
- }
-
- private static boolean
- containsSimpleVariable(ITree<IDiceASTNode> nameTree) {
- return nameTree.transformHead((nameNod) -> {
- if (nameNod.getType() != DiceASTType.VARIABLE) {
throw new UnsupportedOperationException(
"Assigning to complex variables isn't supported. Problem node is "
- + nameNod);
- }
-
- return true;
- });
- }
-
- private static IPair<Integer, ITree<IDiceASTNode>> parseCompound(
- IFunctionalList<IPair<Integer, ITree<IDiceASTNode>>> nodes) {
- if (nodes.getSize() != 2) {
- throw new UnsupportedOperationException(
- "Can only form a group from two dice");
- }
-
- IPair<Integer, ITree<IDiceASTNode>> leftDiceNode =
- nodes.getByIndex(0);
- IPair<Integer, ITree<IDiceASTNode>> rightDiceNode =
- nodes.getByIndex(1);
-
- return leftDiceNode.bind((leftDiceValue, leftDiceTree) -> {
- return rightDiceNode.bind((rightDiceValue, rightDiceTree) -> {
- Integer result =
- Integer.parseInt(Integer.toString(leftDiceValue)
- + Integer.toString(rightDiceValue));
-
- return new Pair<>(result,
- new Tree<>(OperatorDiceNode.GROUP, leftDiceTree,
- rightDiceTree));
+ + nameNode.getRight());
});
});
}