summaryrefslogtreecommitdiff
path: root/dice-lang
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2017-02-23 11:34:42 -0500
committerbculkin2442 <bjculkin@mix.wvu.edu>2017-02-23 11:36:21 -0500
commita942114b89013732a2be3092cdb65cafc4566fe0 (patch)
tree6f6b34484addf0066deb3c97aee81df1384c9dac /dice-lang
parentf4845d197c043aaedfd7dc6ee2a00d5843e54eb3 (diff)
Fix bug in expander ('+' in 5+ was being eaten)
Also, pass around a general context instead of just the thunk in the evaluator
Diffstat (limited to 'dice-lang')
-rw-r--r--dice-lang/src/bjc/dicelang/v2/DiceLangEngine.java2
-rw-r--r--dice-lang/src/bjc/dicelang/v2/Evaluator.java62
-rw-r--r--dice-lang/useful-defines.txt0
3 files changed, 44 insertions, 20 deletions
diff --git a/dice-lang/src/bjc/dicelang/v2/DiceLangEngine.java b/dice-lang/src/bjc/dicelang/v2/DiceLangEngine.java
index aa6d519..d90ce9d 100644
--- a/dice-lang/src/bjc/dicelang/v2/DiceLangEngine.java
+++ b/dice-lang/src/bjc/dicelang/v2/DiceLangEngine.java
@@ -643,7 +643,7 @@ public class DiceLangEngine {
newWorking.add(tk.substring(startMatcher.end()));
} else if(endsWith) {
newWorking.add(tk.substring(0, endMatcher.start()));
- newWorking.add(tk.substring(endMatcher.end()));
+ newWorking.add(tk.substring(endMatcher.start()));
} else {
newWorking.add(tk);
}
diff --git a/dice-lang/src/bjc/dicelang/v2/Evaluator.java b/dice-lang/src/bjc/dicelang/v2/Evaluator.java
index 8658656..72d7779 100644
--- a/dice-lang/src/bjc/dicelang/v2/Evaluator.java
+++ b/dice-lang/src/bjc/dicelang/v2/Evaluator.java
@@ -27,7 +27,7 @@ public class Evaluator {
public DiceBox.DieExpression diceVal;
public String stringVal;
- // Original node this
+ // Original node data
public ITree<Node> origVal;
public Result(Type typ) {
@@ -102,6 +102,12 @@ public class Evaluator {
}
}
+ private static class Context {
+ public Consumer<Iterator<ITree<Node>>> thunk;
+
+ public boolean isDebug;
+ }
+
private static Node FAIL() {
return new Node(Node.Type.RESULT, new Result(Result.Type.FAILURE));
}
@@ -125,17 +131,29 @@ public class Evaluator {
}
public Result evaluate(ITree<Node> comm) {
- Consumer<Iterator<ITree<Node>>> thunk = (itr) -> {
+ 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 causes something to happen
while(itr.hasNext()) itr.next();
};
- return comm.topDownTransform(this::pickEvaluationType, (node) -> this.evaluateNode(node, thunk)).getHead().resultVal;
+ return comm.topDownTransform(this::pickEvaluationType,
+ (node) -> this.evaluateNode(node, ctx)).getHead().resultVal;
}
public Iterator<ITree<Node>> stepDebug(ITree<Node> comm) {
- return new TopDownTransformIterator<>(this::pickEvaluationType, this::evaluateNode, comm);
+ Context ctx = new Context();
+
+ ctx.isDebug = true;
+
+ return new TopDownTransformIterator<>(this::pickEvaluationType, (node, thnk) -> {
+ ctx.thunk = thnk;
+
+ return this.evaluateNode(node, ctx);
+ }, comm);
}
private TopDownTransformResult pickEvaluationType(Node nd) {
@@ -145,15 +163,15 @@ public class Evaluator {
}
}
- private ITree<Node> evaluateNode(ITree<Node> ast, Consumer<Iterator<ITree<Node>>> thunk) {
+ private ITree<Node> evaluateNode(ITree<Node> ast, Context ctx) {
switch(ast.getHead().type) {
case UNARYOP:
System.out.println("\tEVALUATOR ERROR: Unary operator evaluation isn't supported yet");
return new Tree<>(FAIL(ast));
case BINOP:
- return evaluateBinaryOp(ast, thunk);
+ return evaluateBinaryOp(ast, ctx);
case TOKREF:
- return evaluateTokenRef(ast.getHead().tokenVal, thunk);
+ return evaluateTokenRef(ast.getHead().tokenVal, ctx);
case ROOT:
return ast.getChild(ast.getChildrenCount() - 1);
default:
@@ -162,7 +180,7 @@ public class Evaluator {
}
}
- private ITree<Node> evaluateBinaryOp(ITree<Node> ast, Consumer<Iterator<ITree<Node>>> thunk) {
+ private ITree<Node> evaluateBinaryOp(ITree<Node> ast, Context ctx) {
Token.Type binOp = ast.getHead().operatorType;
if(ast.getChildrenCount() != 2) {
@@ -180,26 +198,31 @@ public class Evaluator {
case MULTIPLY:
case DIVIDE:
case IDIVIDE:
- return evaluateMathBinary(binOp, left.getHead().resultVal, right.getHead().resultVal, thunk);
+ return evaluateMathBinary(binOp,
+ left.getHead().resultVal, right.getHead().resultVal,
+ ctx);
case DICEGROUP:
case DICECONCAT:
case DICELIST:
- return evaluateDiceBinary(binOp, left.getHead().resultVal, right.getHead().resultVal, thunk);
+ return evaluateDiceBinary(binOp,
+ left.getHead().resultVal, right.getHead().resultVal,
+ ctx);
default:
Errors.inst.printError(EK_EVAL_UNBIN, binOp.toString());
return new Tree<>(FAIL(ast));
}
}
- private ITree<Node> evaluateDiceBinary(Token.Type op, Result left, Result right, Consumer<Iterator<ITree<Node>>> thunk) {
+ private ITree<Node> evaluateDiceBinary(Token.Type op,
+ Result left, Result right, Context ctx) {
Result res = null;
switch(op) {
case DICEGROUP:
if(left.type == DICE && !left.diceVal.isList) {
if(right.type == DICE && !right.diceVal.isList) {
- res = new Result(DICE, new DiceBox.SimpleDie(left.diceVal.scalar,
- right.diceVal.scalar));
+ res = new Result(DICE,
+ new DiceBox.SimpleDie(left.diceVal.scalar, right.diceVal.scalar));
} else if (right.type == INT) {
res = new Result(DICE, new DiceBox.SimpleDie(left.diceVal.scalar, right.intVal));
} else {
@@ -227,8 +250,8 @@ public class Evaluator {
Errors.inst.printError(EK_EVAL_INVDICE, right.type.toString());
return new Tree<>(FAIL(right));
} else {
- res = new Result(DICE, new DiceBox.CompoundDie(left.diceVal.scalar,
- right.diceVal.scalar));
+ res = new Result(DICE,
+ new DiceBox.CompoundDie(left.diceVal.scalar, right.diceVal.scalar));
}
break;
case DICELIST:
@@ -239,8 +262,8 @@ public class Evaluator {
Errors.inst.printError(EK_EVAL_INVDICE, right.type.toString());
return new Tree<>(FAIL(right));
} else {
- res = new Result(DICE, new DiceBox.SimpleDieList(left.diceVal.scalar,
- right.diceVal.scalar));
+ res = new Result(DICE,
+ new DiceBox.SimpleDieList(left.diceVal.scalar, right.diceVal.scalar));
}
break;
default:
@@ -251,7 +274,8 @@ public class Evaluator {
return new Tree<>(new Node(Node.Type.RESULT, res));
}
- private ITree<Node> evaluateMathBinary(Token.Type op, Result left, Result right, Consumer<Iterator<ITree<Node>>> thunk) {
+ private ITree<Node> evaluateMathBinary(Token.Type op,
+ Result left, Result right, Context ctx) {
if(left.type == Result.Type.DICE || right.type == Result.Type.DICE) {
System.out.println("\tEVALUATOR ERROR: Math on dice isn't supported yet");
return new Tree<>(FAIL());
@@ -340,7 +364,7 @@ public class Evaluator {
return new Tree<>(new Node(Node.Type.RESULT, res));
}
- private ITree<Node> evaluateTokenRef(Token tk, Consumer<Iterator<ITree<Node>>> thunk) {
+ private ITree<Node> evaluateTokenRef(Token tk, Context ctx) {
Result res = null;
switch(tk.type) {
diff --git a/dice-lang/useful-defines.txt b/dice-lang/useful-defines.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/dice-lang/useful-defines.txt