summaryrefslogtreecommitdiff
path: root/dice-lang/src/bjc/dicelang/v1/ast
diff options
context:
space:
mode:
authorEVE <EVE@EVE-PC>2017-03-13 16:41:45 -0400
committerEVE <EVE@EVE-PC>2017-03-13 16:41:45 -0400
commit01136c6796e21f023713e026674576d8e623462d (patch)
treee77886fe0e0adaf3c0430fba9ce248ef83f74fe4 /dice-lang/src/bjc/dicelang/v1/ast
parent870d769cfc152171d27b2331a7c590d0b307ad48 (diff)
Formatting
Diffstat (limited to 'dice-lang/src/bjc/dicelang/v1/ast')
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/ArithmeticCollapser.java150
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/ArrayResult.java2
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/DiceASTEvaluator.java156
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/DiceASTInliner.java42
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/DiceASTOptimizer.java26
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/DiceASTParser.java221
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceChecker.java17
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceSanitizer.java105
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/DiceASTUtils.java39
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/DummyResult.java2
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/IOperatorCollapser.java4
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/IntegerResult.java2
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceLiteralNode.java2
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceOperatorType.java4
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/nodes/ILiteralDiceNode.java11
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/nodes/IntegerLiteralNode.java2
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/nodes/OperatorDiceNode.java47
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/nodes/VariableDiceNode.java5
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/optimization/ArithmeticCollapser.java18
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/optimization/ConstantCollapser.java78
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/optimization/IOptimizationPass.java9
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/optimization/OperationCondenser.java58
22 files changed, 432 insertions, 568 deletions
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/ArithmeticCollapser.java b/dice-lang/src/bjc/dicelang/v1/ast/ArithmeticCollapser.java
index 5a69bf1..1a41ce6 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/ArithmeticCollapser.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/ArithmeticCollapser.java
@@ -18,28 +18,25 @@ import bjc.utils.data.Tree;
*/
final class ArithmeticCollapser implements IOperatorCollapser {
// The type of operator we're collapsing
- private OperatorDiceNode type;
+ private OperatorDiceNode type;
// The operator to use to collapse operators
- private BinaryOperator<Integer> valueOp;
+ private BinaryOperator<Integer> valueOp;
- private int initialValue;
+ private int initialValue;
- public ArithmeticCollapser(OperatorDiceNode type,
- BinaryOperator<Integer> valueOp, int initVal) {
+ public ArithmeticCollapser(OperatorDiceNode type, BinaryOperator<Integer> valueOp, int initVal) {
this.type = type;
this.valueOp = valueOp;
this.initialValue = initVal;
}
@Override
- public IPair<IResult, ITree<IDiceASTNode>> apply(
- IList<IPair<IResult, ITree<IDiceASTNode>>> nodes) {
- IPair<IResult, ITree<IDiceASTNode>> initialState = new Pair<>(
- new IntegerResult(initialValue), new Tree<>(type));
+ public IPair<IResult, ITree<IDiceASTNode>> apply(IList<IPair<IResult, ITree<IDiceASTNode>>> nodes) {
+ IPair<IResult, ITree<IDiceASTNode>> initialState = new Pair<>(new IntegerResult(initialValue),
+ new Tree<>(type));
- BinaryOperator<IPair<IResult, ITree<IDiceASTNode>>> reducer = (
- currentState, accumulatedState) -> {
+ BinaryOperator<IPair<IResult, ITree<IDiceASTNode>>> reducer = (currentState, accumulatedState) -> {
// Force evaluation of accumulated state to prevent
// certain bugs from occuring
// accumulatedState.merge((l, r) -> null);
@@ -47,90 +44,66 @@ final class ArithmeticCollapser implements IOperatorCollapser {
return reduceStates(accumulatedState, currentState);
};
- IPair<IResult, ITree<IDiceASTNode>> reducedState = nodes
- .reduceAux(initialState, reducer, (state) -> state);
+ IPair<IResult, ITree<IDiceASTNode>> reducedState = nodes.reduceAux(initialState, reducer,
+ (state) -> state);
return reducedState;
}
- private IList<IResult> combineArrayResults(IResult accumulatedValue,
- IResult currentValue) {
- IList<IResult> currentList = ((ArrayResult) currentValue)
- .getValue();
- IList<IResult> accumulatedList = ((ArrayResult) accumulatedValue)
- .getValue();
+ private IList<IResult> combineArrayResults(IResult accumulatedValue, IResult currentValue) {
+ IList<IResult> currentList = ((ArrayResult) currentValue).getValue();
+ IList<IResult> accumulatedList = ((ArrayResult) accumulatedValue).getValue();
if (currentList.getSize() != accumulatedList.getSize()) {
- throw new UnsupportedOperationException(
- "Can only apply operations to equal-length arrays");
+ throw new UnsupportedOperationException("Can only apply operations to equal-length arrays");
}
- IList<IResult> resultList = currentList.combineWith(
- accumulatedList, (currentNode, accumulatedNode) -> {
- boolean currentNotInt = currentNode
- .getType() != ResultType.INTEGER;
- boolean accumulatedNotInt = accumulatedNode
- .getType() != ResultType.INTEGER;
-
- if (currentNotInt || accumulatedNotInt) {
- throw new UnsupportedOperationException(
- "Nesting of array operations isn't allowed");
- }
-
- int accumulatedInt = ((IntegerResult) accumulatedNode)
- .getValue();
- int currentInt = ((IntegerResult) currentNode)
- .getValue();
-
- IResult combinedValue = new IntegerResult(
- valueOp.apply(accumulatedInt, currentInt));
- return combinedValue;
- });
+ IList<IResult> resultList = currentList.combineWith(accumulatedList, (currentNode, accumulatedNode) -> {
+ boolean currentNotInt = currentNode.getType() != ResultType.INTEGER;
+ boolean accumulatedNotInt = accumulatedNode.getType() != ResultType.INTEGER;
+
+ if (currentNotInt || accumulatedNotInt) {
+ throw new UnsupportedOperationException("Nesting of array operations isn't allowed");
+ }
+
+ int accumulatedInt = ((IntegerResult) accumulatedNode).getValue();
+ int currentInt = ((IntegerResult) currentNode).getValue();
+
+ IResult combinedValue = new IntegerResult(valueOp.apply(accumulatedInt, currentInt));
+ return combinedValue;
+ });
return resultList;
}
- private IPair<IResult, ITree<IDiceASTNode>> doArithmeticCollapse(
- IResult accumulatedValue, ITree<IDiceASTNode> accumulatedTree,
- IResult currentValue) {
- if (accumulatedValue.getType() == ResultType.DUMMY
- || currentValue.getType() == ResultType.DUMMY) {
- DummyResult result = new DummyResult(
- "Found dummy result with either accumulated dummy ("
- + ((DummyResult) accumulatedValue).getData()
- + ") or current dummy ("
- + ((DummyResult) currentValue).getData()
- + ").");
+ private IPair<IResult, ITree<IDiceASTNode>> doArithmeticCollapse(IResult accumulatedValue,
+ ITree<IDiceASTNode> accumulatedTree, IResult currentValue) {
+ if (accumulatedValue.getType() == ResultType.DUMMY || currentValue.getType() == ResultType.DUMMY) {
+ DummyResult result = new DummyResult("Found dummy result with either accumulated dummy ("
+ + ((DummyResult) accumulatedValue).getData() + ") or current dummy ("
+ + ((DummyResult) currentValue).getData() + ").");
return new Pair<>(result, accumulatedTree);
}
- boolean currentIsInt = currentValue
- .getType() == ResultType.INTEGER;
- boolean accumulatedIsInt = accumulatedValue
- .getType() == ResultType.INTEGER;
+ boolean currentIsInt = currentValue.getType() == ResultType.INTEGER;
+ boolean accumulatedIsInt = accumulatedValue.getType() == ResultType.INTEGER;
if (!currentIsInt) {
if (!accumulatedIsInt) {
- IList<IResult> resultList = combineArrayResults(
- accumulatedValue, currentValue);
+ IList<IResult> resultList = combineArrayResults(accumulatedValue, currentValue);
- return new Pair<>(new ArrayResult(resultList),
- accumulatedTree);
+ return new Pair<>(new ArrayResult(resultList), accumulatedTree);
}
- IList<IResult> resultList = halfCombineLists(
- ((ArrayResult) currentValue).getValue(),
+ IList<IResult> resultList = halfCombineLists(((ArrayResult) currentValue).getValue(),
accumulatedValue, true);
- return new Pair<>(new ArrayResult(resultList),
- accumulatedTree);
+ return new Pair<>(new ArrayResult(resultList), accumulatedTree);
} else if (!accumulatedIsInt) {
- IList<IResult> resultList = halfCombineLists(
- ((ArrayResult) accumulatedValue).getValue(),
+ IList<IResult> resultList = halfCombineLists(((ArrayResult) accumulatedValue).getValue(),
currentValue, false);
- return new Pair<>(new ArrayResult(resultList),
- accumulatedTree);
+ return new Pair<>(new ArrayResult(resultList), accumulatedTree);
}
int accumulatedInt = ((IntegerResult) accumulatedValue).getValue();
@@ -138,23 +111,19 @@ final class ArithmeticCollapser implements IOperatorCollapser {
int combinedValue = valueOp.apply(accumulatedInt, currentInt);
- return new Pair<>(new IntegerResult(combinedValue),
- accumulatedTree);
+ return new Pair<>(new IntegerResult(combinedValue), accumulatedTree);
}
- private IList<IResult> halfCombineLists(IList<IResult> list,
- IResult scalar, boolean scalarLeft) {
+ private IList<IResult> halfCombineLists(IList<IResult> list, IResult scalar, boolean scalarLeft) {
if (scalar.getType() != ResultType.INTEGER) {
- throw new UnsupportedOperationException(
- "Nested array operations not supported");
+ throw new UnsupportedOperationException("Nested array operations not supported");
}
int scalarInt = ((IntegerResult) scalar).getValue();
return list.map((element) -> {
if (element.getType() != ResultType.INTEGER) {
- throw new UnsupportedOperationException(
- "Nested array operations not supported");
+ throw new UnsupportedOperationException("Nested array operations not supported");
}
int elementInt = ((IntegerResult) element).getValue();
@@ -162,30 +131,23 @@ final class ArithmeticCollapser implements IOperatorCollapser {
IResult combinedValue;
if (scalarLeft) {
- combinedValue = new IntegerResult(
- valueOp.apply(scalarInt, elementInt));
+ combinedValue = new IntegerResult(valueOp.apply(scalarInt, elementInt));
} else {
- combinedValue = new IntegerResult(
- valueOp.apply(elementInt, scalarInt));
+ combinedValue = new IntegerResult(valueOp.apply(elementInt, scalarInt));
}
return combinedValue;
});
}
- private IPair<IResult, ITree<IDiceASTNode>> reduceStates(
- IPair<IResult, ITree<IDiceASTNode>> accumulatedState,
+ private IPair<IResult, ITree<IDiceASTNode>> reduceStates(IPair<IResult, ITree<IDiceASTNode>> accumulatedState,
IPair<IResult, ITree<IDiceASTNode>> currentState) {
- return accumulatedState
- .bind((accumulatedValue, accumulatedTree) -> {
- return currentState
- .bind((currentValue, currentTree) -> {
- accumulatedTree.addChild(currentTree);
-
- return doArithmeticCollapse(
- accumulatedValue, accumulatedTree,
- currentValue);
- });
- });
+ return accumulatedState.bind((accumulatedValue, accumulatedTree) -> {
+ return currentState.bind((currentValue, currentTree) -> {
+ accumulatedTree.addChild(currentTree);
+
+ return doArithmeticCollapse(accumulatedValue, accumulatedTree, currentValue);
+ });
+ });
}
} \ No newline at end of file
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/ArrayResult.java b/dice-lang/src/bjc/dicelang/v1/ast/ArrayResult.java
index 6cb9216..c8a35a6 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/ArrayResult.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/ArrayResult.java
@@ -16,7 +16,7 @@ public class ArrayResult implements IResult {
* Create a new array-valued result
*
* @param results
- * The results in the array
+ * The results in the array
*/
public ArrayResult(IList<IResult> results) {
this.arrayContents = results;
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTEvaluator.java b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTEvaluator.java
index c8c0032..af31ad7 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTEvaluator.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTEvaluator.java
@@ -30,27 +30,24 @@ import bjc.utils.data.Tree;
*
*/
public class DiceASTEvaluator {
- private static IResult bindLiteralValue(IDiceASTNode leafNode,
- IMap<String, ITree<IDiceASTNode>> enviroment) {
+ private static IResult bindLiteralValue(IDiceASTNode leafNode, IMap<String, ITree<IDiceASTNode>> enviroment) {
String variableName = ((VariableDiceNode) leafNode).getVariable();
if (enviroment.containsKey(variableName)) {
- IResult result = evaluateAST(enviroment.get(variableName),
- enviroment);
+ IResult result = evaluateAST(enviroment.get(variableName), enviroment);
return result;
}
// Return a DummyResult to handle lets properly
- return new DummyResult(
- "Attempted to deref unbound variable " + variableName);
+ return new DummyResult("Attempted to deref unbound variable " + variableName);
}
/**
* Build the map of operations to use when collapsing the AST
*
* @param enviroment
- * The enviroment to evaluate bindings and such against
+ * The enviroment to evaluate bindings and such against
* @return The operations to use when collapsing the AST
*/
private static IMap<IDiceASTNode, IOperatorCollapser> buildOperations(
@@ -58,34 +55,27 @@ public class DiceASTEvaluator {
IMap<IDiceASTNode, IOperatorCollapser> operatorCollapsers = new FunctionalMap<>();
operatorCollapsers.put(OperatorDiceNode.ADD,
- new ArithmeticCollapser(OperatorDiceNode.ADD,
- (left, right) -> left + right, 0));
+ new ArithmeticCollapser(OperatorDiceNode.ADD, (left, right) -> left + right, 0));
operatorCollapsers.put(OperatorDiceNode.SUBTRACT,
- new ArithmeticCollapser(OperatorDiceNode.SUBTRACT,
- (left, right) -> left - right, 0));
+ new ArithmeticCollapser(OperatorDiceNode.SUBTRACT, (left, right) -> left - right, 0));
operatorCollapsers.put(OperatorDiceNode.MULTIPLY,
- new ArithmeticCollapser(OperatorDiceNode.MULTIPLY,
- (left, right) -> left * right, 1));
+ new ArithmeticCollapser(OperatorDiceNode.MULTIPLY, (left, right) -> left * right, 1));
operatorCollapsers.put(OperatorDiceNode.DIVIDE,
- new ArithmeticCollapser(OperatorDiceNode.DIVIDE,
- (left, right) -> left / right, 1));
+ new ArithmeticCollapser(OperatorDiceNode.DIVIDE, (left, right) -> left / right, 1));
operatorCollapsers.put(OperatorDiceNode.ASSIGN, (nodes) -> {
return parseBinding(enviroment, nodes);
});
operatorCollapsers.put(OperatorDiceNode.COMPOUND,
- new ArithmeticCollapser(OperatorDiceNode.COMPOUND,
- (left, right) -> {
- return Integer.parseInt(Integer.toString(left)
- + Integer.toString(right));
- }, 0));
+ new ArithmeticCollapser(OperatorDiceNode.COMPOUND, (left, right) -> {
+ return Integer.parseInt(Integer.toString(left) + Integer.toString(right));
+ }, 0));
- operatorCollapsers.put(OperatorDiceNode.GROUP,
- DiceASTEvaluator::parseGroup);
+ operatorCollapsers.put(OperatorDiceNode.GROUP, DiceASTEvaluator::parseGroup);
operatorCollapsers.put(OperatorDiceNode.LET, (nodes) -> {
// @TODO Fix lets prematurely evaluating things
@@ -106,8 +96,7 @@ public class DiceASTEvaluator {
};
Supplier<ITree<IDiceASTNode>> treeSupplier = () -> {
- ITree<IDiceASTNode> returnedTree = new Tree<>(
- OperatorDiceNode.ARRAY);
+ ITree<IDiceASTNode> returnedTree = new Tree<>(OperatorDiceNode.ARRAY);
nodes.forEach((element) -> {
returnedTree.addChild(element.getRight());
@@ -122,11 +111,9 @@ public class DiceASTEvaluator {
return operatorCollapsers;
}
- private static void doArrayAssign(
- IMap<String, ITree<IDiceASTNode>> enviroment,
- IPair<IResult, ITree<IDiceASTNode>> nameNode,
- ITree<IDiceASTNode> nameTree, ITree<IDiceASTNode> valueTree,
- IHolder<Integer> childCount, ITree<IDiceASTNode> child) {
+ private static void doArrayAssign(IMap<String, ITree<IDiceASTNode>> enviroment,
+ IPair<IResult, ITree<IDiceASTNode>> nameNode, ITree<IDiceASTNode> nameTree,
+ ITree<IDiceASTNode> valueTree, IHolder<Integer> childCount, ITree<IDiceASTNode> child) {
if (nameTree.getHead().getType() != DiceASTType.VARIABLE) {
throw new UnsupportedOperationException(
"Assigning to complex variables isn't supported. Problem node is "
@@ -146,75 +133,65 @@ public class DiceASTEvaluator {
* Evaluate the provided AST to a numeric value
*
* @param expression
- * The expression to evaluate
+ * The expression to evaluate
* @param enviroment
- * The enviroment to look up variables in
+ * The enviroment to look up variables in
* @return The integer value of the expression
*/
public static IResult evaluateAST(ITree<IDiceASTNode> expression,
IMap<String, ITree<IDiceASTNode>> enviroment) {
- IMap<IDiceASTNode, IOperatorCollapser> collapsers = buildOperations(
- enviroment);
+ IMap<IDiceASTNode, IOperatorCollapser> collapsers = buildOperations(enviroment);
- return expression.collapse(
- (node) -> evaluateLeaf(node, enviroment), collapsers::get,
+ return expression.collapse((node) -> evaluateLeaf(node, enviroment), collapsers::get,
(pair) -> pair.getLeft());
}
- private static IPair<IResult, ITree<IDiceASTNode>> evaluateLeaf(
- IDiceASTNode leafNode,
+ private static IPair<IResult, ITree<IDiceASTNode>> evaluateLeaf(IDiceASTNode leafNode,
IMap<String, ITree<IDiceASTNode>> enviroment) {
ITree<IDiceASTNode> returnedAST = new Tree<>(leafNode);
switch (leafNode.getType()) {
- case LITERAL:
- return new Pair<>(evaluateLiteral(leafNode), returnedAST);
+ case LITERAL:
+ return new Pair<>(evaluateLiteral(leafNode), returnedAST);
- case VARIABLE:
- return new LazyPair<>(() -> {
- return bindLiteralValue(leafNode, enviroment);
- }, () -> returnedAST);
+ case VARIABLE:
+ return new LazyPair<>(() -> {
+ return bindLiteralValue(leafNode, enviroment);
+ }, () -> returnedAST);
- case OPERATOR:
- default:
- throw new UnsupportedOperationException(
- "Node '" + leafNode + "' cannot be a leaf.");
+ case OPERATOR:
+ default:
+ throw new UnsupportedOperationException("Node '" + leafNode + "' cannot be a leaf.");
}
}
private static IResult evaluateLiteral(IDiceASTNode leafNode) {
- DiceLiteralType literalType = ((ILiteralDiceNode) leafNode)
- .getLiteralType();
+ DiceLiteralType literalType = ((ILiteralDiceNode) leafNode).getLiteralType();
switch (literalType) {
- case DICE:
- int diceRoll = ((DiceLiteralNode) leafNode).getValue()
- .roll();
-
- return new IntegerResult(diceRoll);
- case INTEGER:
- int val = ((IntegerLiteralNode) leafNode).getValue();
-
- return new IntegerResult(val);
- default:
- throw new UnsupportedOperationException("Literal value '"
- + leafNode + "' is of a type (" + literalType
- + ") not currently supported.");
+ case DICE:
+ int diceRoll = ((DiceLiteralNode) leafNode).getValue().roll();
+
+ return new IntegerResult(diceRoll);
+ case INTEGER:
+ int val = ((IntegerLiteralNode) leafNode).getValue();
+
+ return new IntegerResult(val);
+ default:
+ throw new UnsupportedOperationException("Literal value '" + leafNode + "' is of a type ("
+ + literalType + ") not currently supported.");
}
}
- private static IPair<IResult, ITree<IDiceASTNode>> parseBinding(
- IMap<String, ITree<IDiceASTNode>> enviroment,
+ private static IPair<IResult, ITree<IDiceASTNode>> parseBinding(IMap<String, ITree<IDiceASTNode>> enviroment,
IList<IPair<IResult, ITree<IDiceASTNode>>> nodes) {
if (nodes.getSize() != 2) {
throw new UnsupportedOperationException(
- "Can only bind nodes with two children. Problem children are "
- + nodes);
+ "Can only bind nodes with two children. Problem children are " + nodes);
}
IPair<IResult, ITree<IDiceASTNode>> nameNode = nodes.getByIndex(0);
- IPair<IResult, ITree<IDiceASTNode>> valueNode = nodes
- .getByIndex(1);
+ IPair<IResult, ITree<IDiceASTNode>> valueNode = nodes.getByIndex(1);
return nameNode.bindRight((nameTree) -> {
return valueNode.bind((valueValue, valueTree) -> {
@@ -228,8 +205,7 @@ public class DiceASTEvaluator {
return new Pair<>(valueValue, nameTree);
} else if (nameTree.getHead() == OperatorDiceNode.ARRAY) {
if (valueTree.getHead() == OperatorDiceNode.ARRAY) {
- if (nameTree.getChildrenCount() != valueTree
- .getChildrenCount()) {
+ if (nameTree.getChildrenCount() != valueTree.getChildrenCount()) {
throw new UnsupportedOperationException(
"Array assignment must be between two equal length arrays");
}
@@ -237,8 +213,8 @@ public class DiceASTEvaluator {
IHolder<Integer> childCount = new Identity<>(0);
nameTree.doForChildren((child) -> {
- doArrayAssign(enviroment, nameNode, nameTree,
- valueTree, childCount, child);
+ doArrayAssign(enviroment, nameNode, nameTree, valueTree,
+ childCount, child);
childCount.transform(val -> val + 1);
});
@@ -248,8 +224,7 @@ public class DiceASTEvaluator {
nameTree.doForChildren((child) -> {
String varName = child.transformHead((nameNod) -> {
- return ((VariableDiceNode) nameNod)
- .getVariable();
+ return ((VariableDiceNode) nameNod).getVariable();
});
enviroment.put(varName, valueTree);
@@ -268,42 +243,33 @@ public class DiceASTEvaluator {
private static IPair<IResult, ITree<IDiceASTNode>> parseGroup(
IList<IPair<IResult, ITree<IDiceASTNode>>> nodes) {
if (nodes.getSize() != 2) {
- throw new UnsupportedOperationException(
- "Can only form a group from two dice");
+ throw new UnsupportedOperationException("Can only form a group from two dice");
}
- IPair<IResult, ITree<IDiceASTNode>> numberDiceNode = nodes
- .getByIndex(0);
- IPair<IResult, ITree<IDiceASTNode>> diceTypeNode = nodes
- .getByIndex(1);
+ IPair<IResult, ITree<IDiceASTNode>> numberDiceNode = nodes.getByIndex(0);
+ IPair<IResult, ITree<IDiceASTNode>> diceTypeNode = nodes.getByIndex(1);
return numberDiceNode.bind((numberDiceValue, numberDiceTree) -> {
return diceTypeNode.bind((diceTypeValue, diceTypeTree) -> {
- ComplexDice cDice = new ComplexDice(
- ((IntegerResult) numberDiceValue).getValue(),
+ ComplexDice cDice = new ComplexDice(((IntegerResult) numberDiceValue).getValue(),
((IntegerResult) diceTypeValue).getValue());
return new Pair<>(new IntegerResult(cDice.roll()),
- new Tree<>(OperatorDiceNode.GROUP, numberDiceTree,
- diceTypeTree));
+ new Tree<>(OperatorDiceNode.GROUP, numberDiceTree, diceTypeTree));
});
});
}
- private static IPair<IResult, ITree<IDiceASTNode>> parseLet(
- IMap<String, ITree<IDiceASTNode>> enviroment,
+ private static IPair<IResult, ITree<IDiceASTNode>> parseLet(IMap<String, ITree<IDiceASTNode>> enviroment,
IList<IPair<IResult, ITree<IDiceASTNode>>> nodes) {
if (nodes.getSize() != 2) {
- throw new UnsupportedOperationException(
- "Can only use let with two expressions.");
+ throw new UnsupportedOperationException("Can only use let with two expressions.");
}
ITree<IDiceASTNode> bindTree = nodes.getByIndex(0).getRight();
- ITree<IDiceASTNode> expressionTree = nodes.getByIndex(1)
- .getRight();
+ ITree<IDiceASTNode> expressionTree = nodes.getByIndex(1).getRight();
- IMap<String, ITree<IDiceASTNode>> letEnviroment = enviroment
- .extend();
+ IMap<String, ITree<IDiceASTNode>> letEnviroment = enviroment.extend();
System.out.println("Evaluating tree for bound values");
@@ -311,10 +277,8 @@ public class DiceASTEvaluator {
IResult exprResult = evaluateAST(expressionTree, letEnviroment);
- IList<ITree<IDiceASTNode>> childrn = nodes
- .map((pair) -> pair.getRight());
+ IList<ITree<IDiceASTNode>> childrn = nodes.map((pair) -> pair.getRight());
- return new Pair<>(exprResult,
- new Tree<>(OperatorDiceNode.LET, childrn));
+ return new Pair<>(exprResult, new Tree<>(OperatorDiceNode.LET, childrn));
}
}
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTInliner.java b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTInliner.java
index 51aba03..38e1361 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTInliner.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTInliner.java
@@ -20,9 +20,9 @@ public class DiceASTInliner {
* Inline all the variables in the AST
*
* @param ast
- * The AST to inline variables into
+ * The AST to inline variables into
* @param enviroment
- * The enviroment to inline from
+ * The enviroment to inline from
* @return The inlined AST
*/
public static ITree<IDiceASTNode> inlineAll(ITree<IDiceASTNode> ast,
@@ -32,8 +32,7 @@ public class DiceASTInliner {
return selectiveInline(ast, enviroment, (String[]) null);
}
- private static ITree<IDiceASTNode> inlineNode(IDiceASTNode node,
- IMap<String, ITree<IDiceASTNode>> enviroment,
+ private static ITree<IDiceASTNode> inlineNode(IDiceASTNode node, IMap<String, ITree<IDiceASTNode>> enviroment,
boolean specificInline, IList<String> variableNames) {
// Only variables get inlined
if (node.getType() != DiceASTType.VARIABLE) {
@@ -50,8 +49,7 @@ public class DiceASTInliner {
// You can't inline non-existent variables
if (!enviroment.containsKey(variableName)) {
throw new UnsupportedOperationException(
- "Attempted to inline non-existant variable "
- + variableName);
+ "Attempted to inline non-existant variable " + variableName);
}
// Return the tree for the variable
@@ -61,12 +59,11 @@ public class DiceASTInliner {
// We're not inlining this particular variable
return new Tree<>(node);
}
-
+
// You can't inline non-existent variables
if (!enviroment.containsKey(variableName)) {
throw new UnsupportedOperationException(
- "Attempted to inline non-existant variable "
- + variableName);
+ "Attempted to inline non-existant variable " + variableName);
}
// Return the tree for the variable
@@ -77,37 +74,32 @@ public class DiceASTInliner {
* Inline the specified variables in the AST
*
* @param ast
- * The AST to inline variables into
+ * The AST to inline variables into
* @param enviroment
- * The enviroment to inline from
+ * The enviroment to inline from
* @param variables
- * The variables to inline
+ * The variables to inline
* @return The inlined AST
*/
- public static ITree<IDiceASTNode> selectiveInline(
- ITree<IDiceASTNode> ast,
- IMap<String, ITree<IDiceASTNode>> enviroment,
- IList<String> variables) {
+ public static ITree<IDiceASTNode> selectiveInline(ITree<IDiceASTNode> ast,
+ IMap<String, ITree<IDiceASTNode>> enviroment, IList<String> variables) {
// Inline the specified variables
- return selectiveInline(ast, enviroment,
- variables.toArray(new String[0]));
+ return selectiveInline(ast, enviroment, variables.toArray(new String[0]));
}
/**
* Inline the specified variables in the AST
*
* @param ast
- * The AST to inline variables into
+ * The AST to inline variables into
* @param enviroment
- * The enviroment to inline from
+ * The enviroment to inline from
* @param variables
- * The variables to inline
+ * The variables to inline
* @return The inlined AST
*/
- public static ITree<IDiceASTNode> selectiveInline(
- ITree<IDiceASTNode> ast,
- IMap<String, ITree<IDiceASTNode>> enviroment,
- String... variables) {
+ public static ITree<IDiceASTNode> selectiveInline(ITree<IDiceASTNode> ast,
+ IMap<String, ITree<IDiceASTNode>> enviroment, String... variables) {
// If we're selectively inlining, do so
if (variables != null && variables.length > 0) {
IList<String> variableNames = new FunctionalList<>(variables);
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTOptimizer.java b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTOptimizer.java
index 178b175..a93de33 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTOptimizer.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTOptimizer.java
@@ -27,7 +27,7 @@ public class DiceASTOptimizer {
* Add a pass to the list of optimization passes
*
* @param pass
- * The pass to add
+ * The pass to add
*/
public void addPass(IOptimizationPass pass) {
passes.add(pass);
@@ -37,23 +37,19 @@ public class DiceASTOptimizer {
* Optimize the passed in tree
*
* @param ast
- * The tree to optimize
+ * The tree to optimize
* @param enviroment
- * The enviroment for variable references
+ * The enviroment for variable references
* @return The optimized tree
*/
- public ITree<IDiceASTNode> optimizeTree(ITree<IDiceASTNode> ast,
- IMap<String, ITree<IDiceASTNode>> enviroment) {
- ITree<IDiceASTNode> optimizedTree = passes.reduceAux(ast,
- (currentPass, currentTree) -> {
- return currentTree.collapse(currentPass::optimizeLeaf,
- (operator) -> {
- return (nodes) -> {
- return currentPass.optimizeOperator(
- operator, nodes);
- };
- }, (tree) -> tree);
- }, (tree) -> tree);
+ public ITree<IDiceASTNode> optimizeTree(ITree<IDiceASTNode> ast, IMap<String, ITree<IDiceASTNode>> enviroment) {
+ ITree<IDiceASTNode> optimizedTree = passes.reduceAux(ast, (currentPass, currentTree) -> {
+ return currentTree.collapse(currentPass::optimizeLeaf, (operator) -> {
+ return (nodes) -> {
+ return currentPass.optimizeOperator(operator, nodes);
+ };
+ }, (tree) -> tree);
+ }, (tree) -> tree);
return optimizedTree;
}
}
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTParser.java b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTParser.java
index d536cc3..87f3640 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTParser.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTParser.java
@@ -29,135 +29,124 @@ import bjc.utils.parserutils.TreeConstructor;
*
*/
public class DiceASTParser {
- private static IDiceASTNode convertLeafNode(String leafNode) {
- DiceLiteralType literalType = ILiteralDiceNode
- .getLiteralType(leafNode);
-
- if (literalType != null) {
- switch (literalType) {
- case DICE:
- return new DiceLiteralNode(
- IDiceExpression.toExpression(leafNode));
- case INTEGER:
- return new IntegerLiteralNode(
- Integer.parseInt(leafNode));
- default:
- throw new InputMismatchException(
- "Cannot convert string '" + leafNode
- + "' into a literal.");
- }
- }
-
- if (leafNode.matches("[+-]?\\d*\\.\\d+")) {
- throw new InputMismatchException(
- "Floating point literals are not supported");
- }
-
- return new VariableDiceNode(leafNode);
+ private static IDiceASTNode convertLeafNode(String leafNode) {
+ DiceLiteralType literalType = ILiteralDiceNode.getLiteralType(leafNode);
+
+ if (literalType != null) {
+ switch (literalType) {
+ case DICE:
+ return new DiceLiteralNode(IDiceExpression.toExpression(leafNode));
+ case INTEGER:
+ return new IntegerLiteralNode(Integer.parseInt(leafNode));
+ default:
+ throw new InputMismatchException(
+ "Cannot convert string '" + leafNode + "' into a literal.");
+ }
}
- private static IDiceASTNode convertOperatorNode(String operatorNode) {
- try {
- return OperatorDiceNode.fromString(operatorNode);
- } catch (IllegalArgumentException iaex) {
- InputMismatchException imex = new InputMismatchException(
- "Attempted to parse invalid operator " + operatorNode);
+ if (leafNode.matches("[+-]?\\d*\\.\\d+")) {
+ throw new InputMismatchException("Floating point literals are not supported");
+ }
+
+ return new VariableDiceNode(leafNode);
+ }
- imex.initCause(iaex);
+ private static IDiceASTNode convertOperatorNode(String operatorNode) {
+ try {
+ return OperatorDiceNode.fromString(operatorNode);
+ } catch (IllegalArgumentException iaex) {
+ InputMismatchException imex = new InputMismatchException(
+ "Attempted to parse invalid operator " + operatorNode);
- throw imex;
- }
+ imex.initCause(iaex);
+
+ throw imex;
+ }
+ }
+
+ /**
+ * Create an AST from a list of tokens
+ *
+ * @param tokens
+ * The list of tokens to convert
+ * @return An AST built from the tokens
+ */
+ public static ITree<IDiceASTNode> createFromString(IList<String> tokens) {
+ // Mark arrays as special operators
+ Predicate<String> specialPicker = (operator) -> {
+ if (StringUtils.containsOnly(operator, "\\[") || StringUtils.containsOnly(operator, "\\]")) {
+ return true;
+ }
+
+ return false;
+ };
+
+ // Here is the map for holding special operators
+ IMap<String, Function<Deque<ITree<String>>, ITree<String>>> operators = new FunctionalMap<>();
+
+ // Handle open [
+ operators.put("[", (queuedTrees) -> {
+ // Just put in a [
+ Tree<String> openArray = new Tree<>("[");
+
+ return openArray;
+ });
+
+ operators.put("]", (queuedTrees) -> {
+ // Parse closing an array
+ return parseCloseArray(queuedTrees);
+ });
+
+ ITree<String> rawTokens = TreeConstructor.constructTree(tokens, (token) -> {
+ return isOperatorNode(token);
+ }, specialPicker, operators::get);
+
+ ITree<IDiceASTNode> tokenizedTree = rawTokens.rebuildTree(DiceASTParser::convertLeafNode,
+ DiceASTParser::convertOperatorNode);
+
+ return tokenizedTree;
+ }
+
+ private static boolean isOperatorNode(String token) {
+ if (StringUtils.containsOnly(token, "\\[")) {
+ return true;
+ } else if (StringUtils.containsOnly(token, "\\]")) {
+ return true;
}
- /**
- * Create an AST from a list of tokens
- *
- * @param tokens
- * The list of tokens to convert
- * @return An AST built from the tokens
- */
- public static ITree<IDiceASTNode> createFromString(
- IList<String> tokens) {
- // Mark arrays as special operators
- Predicate<String> specialPicker = (operator) -> {
- if (StringUtils.containsOnly(operator, "\\[") ||
- StringUtils.containsOnly(operator, "\\]")) {
- return true;
- }
-
- return false;
- };
-
- // Here is the map for holding special operators
- IMap<String, Function<Deque<ITree<String>>, ITree<String>>> operators = new FunctionalMap<>();
-
- // Handle open [
- operators.put("[", (queuedTrees) -> {
- // Just put in a [
- Tree<String> openArray = new Tree<>("[");
-
- return openArray;
- });
-
- operators.put("]", (queuedTrees) -> {
- // Parse closing an array
- return parseCloseArray(queuedTrees);
- });
-
- ITree<String> rawTokens = TreeConstructor.constructTree(tokens,
- (token) -> {
- return isOperatorNode(token);
- }, specialPicker, operators::get);
-
- ITree<IDiceASTNode> tokenizedTree = rawTokens.rebuildTree(
- DiceASTParser::convertLeafNode,
- DiceASTParser::convertOperatorNode);
-
- return tokenizedTree;
- }
-
- private static boolean isOperatorNode(String token) {
- if (StringUtils.containsOnly(token, "\\[")) {
- return true;
- } else if (StringUtils.containsOnly(token, "\\]")) {
- return true;
- }
-
- if (token.equals("[]")) {
- // This is a synthetic operator, constructed by [ and ]
- return true;
- }
-
- try {
- OperatorDiceNode.fromString(token);
- return true;
- } catch (IllegalArgumentException iaex) {
- // We don't care about details
- return false;
- }
+ if (token.equals("[]")) {
+ // This is a synthetic operator, constructed by [ and ]
+ return true;
}
- private static ITree<String> parseCloseArray(
- Deque<ITree<String>> queuedTrees) {
- IList<ITree<String>> children = new FunctionalList<>();
+ try {
+ OperatorDiceNode.fromString(token);
+ return true;
+ } catch (IllegalArgumentException iaex) {
+ // We don't care about details
+ return false;
+ }
+ }
+
+ private static ITree<String> parseCloseArray(Deque<ITree<String>> queuedTrees) {
+ IList<ITree<String>> children = new FunctionalList<>();
- while (shouldContinuePopping(queuedTrees)) {
- children.add(queuedTrees.pop());
- }
+ while (shouldContinuePopping(queuedTrees)) {
+ children.add(queuedTrees.pop());
+ }
- queuedTrees.pop();
+ queuedTrees.pop();
- children.reverse();
+ children.reverse();
- ITree<String> arrayTree = new Tree<>("[]", children);
+ ITree<String> arrayTree = new Tree<>("[]", children);
- return arrayTree;
- }
+ return arrayTree;
+ }
- private static boolean shouldContinuePopping(
- Deque<ITree<String>> queuedTrees) {
- String peekToken = queuedTrees.peek().getHead();
+ private static boolean shouldContinuePopping(Deque<ITree<String>> queuedTrees) {
+ String peekToken = queuedTrees.peek().getHead();
- return !peekToken.equals("[");
- }
+ return !peekToken.equals("[");
+ }
}
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceChecker.java b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceChecker.java
index 5492cf7..5be2090 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceChecker.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceChecker.java
@@ -13,25 +13,24 @@ import bjc.utils.data.IHolder;
* @author ben
*
*/
-public final class DiceASTReferenceChecker
- implements Consumer<IDiceASTNode> {
+public final class DiceASTReferenceChecker implements Consumer<IDiceASTNode> {
/**
* This is true if the specified node references the set variable
*/
- private IHolder<Boolean> referencesVariable;
+ private IHolder<Boolean> referencesVariable;
- private String varName;
+ private String varName;
/**
* Create a new reference checker
*
* @param referencesVar
- * The holder of whether the variable is referenced or not
+ * The holder of whether the variable is referenced or
+ * not
* @param varName
- * The variable to check for references in
+ * The variable to check for references in
*/
- public DiceASTReferenceChecker(IHolder<Boolean> referencesVar,
- String varName) {
+ public DiceASTReferenceChecker(IHolder<Boolean> referencesVar, String varName) {
this.referencesVariable = referencesVar;
this.varName = varName;
}
@@ -45,7 +44,7 @@ public final class DiceASTReferenceChecker
* Check if a given AST node directly references the specified variable
*
* @param astNode
- * The node to check
+ * The node to check
* @return Whether or not the node directly the variable
*/
private boolean isDirectReference(IDiceASTNode astNode) {
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceSanitizer.java b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceSanitizer.java
index 79c0ce7..5bb07fd 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceSanitizer.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceSanitizer.java
@@ -12,9 +12,8 @@ import bjc.utils.data.TopDownTransformResult;
import bjc.utils.data.Tree;
/**
- * Sanitize the references in an AST so that a variable that refers to
- * itself in its definition has the occurance of it replaced with its
- * previous definition
+ * Sanitize the references in an AST so that a variable that refers to itself in
+ * its definition has the occurance of it replaced with its previous definition
*
* @author ben
*
@@ -23,8 +22,7 @@ public class DiceASTReferenceSanitizer {
private static ITree<IDiceASTNode> doSanitize(ITree<IDiceASTNode> ast,
IMap<String, ITree<IDiceASTNode>> enviroment) {
if (ast.getChildrenCount() != 2) {
- throw new UnsupportedOperationException(
- "Assignment must have two arguments.");
+ throw new UnsupportedOperationException("Assignment must have two arguments.");
}
ITree<IDiceASTNode> nameTree = ast.getChild(0);
@@ -36,8 +34,7 @@ public class DiceASTReferenceSanitizer {
nameTree.doForChildren((child) -> {
if (allSimpleVariables.getValue()) {
- boolean isSimple = DiceASTUtils
- .containsSimpleVariable(child);
+ boolean isSimple = DiceASTUtils.containsSimpleVariable(child);
allSimpleVariables.replace(isSimple);
}
@@ -50,8 +47,7 @@ public class DiceASTReferenceSanitizer {
}
if (valueTree.getHead() == OperatorDiceNode.ARRAY) {
- if (nameTree.getChildrenCount() != valueTree
- .getChildrenCount()) {
+ if (nameTree.getChildrenCount() != valueTree.getChildrenCount()) {
throw new UnsupportedOperationException(
"Array assignment between arrays must be"
+ " between two arrays of equal length");
@@ -67,24 +63,21 @@ public class DiceASTReferenceSanitizer {
if (valueTree.getHead() == OperatorDiceNode.ARRAY) {
IHolder<Integer> childCounter = new Identity<>(0);
- ITree<IDiceASTNode> returnTree = new Tree<>(
- OperatorDiceNode.ARRAY);
+ ITree<IDiceASTNode> returnTree = new Tree<>(OperatorDiceNode.ARRAY);
nameTree.doForChildren((child) -> {
String variableName = child.transformHead((node) -> {
return ((VariableDiceNode) node).getVariable();
});
- ITree<IDiceASTNode> currentValue = valueTree
- .getChild(childCounter.getValue());
+ ITree<IDiceASTNode> currentValue = valueTree.getChild(childCounter.getValue());
- ITree<IDiceASTNode> sanitizedSubtree = doSingleSanitize(
- ast, enviroment, child, currentValue,
- variableName);
+ ITree<IDiceASTNode> sanitizedSubtree = doSingleSanitize(ast, enviroment, child,
+ currentValue, variableName);
if (sanitizedSubtree == null) {
- ITree<IDiceASTNode> oldTree = new Tree<>(
- ast.getHead(), child, currentValue);
+ ITree<IDiceASTNode> oldTree = new Tree<>(ast.getHead(), child,
+ currentValue);
returnTree.addChild(oldTree);
} else {
@@ -97,18 +90,16 @@ public class DiceASTReferenceSanitizer {
return returnTree;
}
- ITree<IDiceASTNode> returnTree = new Tree<>(
- OperatorDiceNode.ARRAY);
+ ITree<IDiceASTNode> returnTree = new Tree<>(OperatorDiceNode.ARRAY);
nameTree.doForChildren((child) -> {
- String variableName = child.transformHead(
- (node) -> ((VariableDiceNode) node).getVariable());
+ String variableName = child
+ .transformHead((node) -> ((VariableDiceNode) node).getVariable());
- ITree<IDiceASTNode> sanitizedChild = doSingleSanitize(ast,
- enviroment, child, valueTree, variableName);
+ ITree<IDiceASTNode> sanitizedChild = doSingleSanitize(ast, enviroment, child, valueTree,
+ variableName);
if (sanitizedChild == null) {
- ITree<IDiceASTNode> oldTree = new Tree<>(ast.getHead(),
- child, valueTree);
+ ITree<IDiceASTNode> oldTree = new Tree<>(ast.getHead(), child, valueTree);
returnTree.addChild(oldTree);
} else {
@@ -119,11 +110,10 @@ public class DiceASTReferenceSanitizer {
return returnTree;
}
- String variableName = nameTree.transformHead(
- (node) -> ((VariableDiceNode) node).getVariable());
+ String variableName = nameTree.transformHead((node) -> ((VariableDiceNode) node).getVariable());
- ITree<IDiceASTNode> sanitizedTree = doSingleSanitize(ast,
- enviroment, nameTree, valueTree, variableName);
+ ITree<IDiceASTNode> sanitizedTree = doSingleSanitize(ast, enviroment, nameTree, valueTree,
+ variableName);
if (sanitizedTree == null) {
return ast;
@@ -132,22 +122,19 @@ public class DiceASTReferenceSanitizer {
return sanitizedTree;
}
- private static ITree<IDiceASTNode> doSingleSanitize(
- ITree<IDiceASTNode> ast,
- IMap<String, ITree<IDiceASTNode>> enviroment,
- ITree<IDiceASTNode> nameTree, ITree<IDiceASTNode> valueTree,
- String variableName) {
+ private static ITree<IDiceASTNode> doSingleSanitize(ITree<IDiceASTNode> ast,
+ IMap<String, ITree<IDiceASTNode>> enviroment, ITree<IDiceASTNode> nameTree,
+ ITree<IDiceASTNode> valueTree, String variableName) {
if (enviroment.containsKey(variableName)) {
// @ is a meta-variable standing for the left side of an
// assignment
- ITree<IDiceASTNode> oldVal = enviroment.put("@",
- enviroment.get(variableName));
+ ITree<IDiceASTNode> oldVal = enviroment.put("@", enviroment.get(variableName));
- // We should always inline out references to last, because it
+ // We should always inline out references to last,
+ // because it
// will always change
- ITree<IDiceASTNode> inlinedValue = DiceASTInliner
- .selectiveInline(valueTree, enviroment, variableName,
- "last", "@");
+ ITree<IDiceASTNode> inlinedValue = DiceASTInliner.selectiveInline(valueTree, enviroment,
+ variableName, "last", "@");
if (oldVal != null) {
enviroment.put("@", oldVal);
@@ -170,32 +157,30 @@ public class DiceASTReferenceSanitizer {
*/
public static ITree<IDiceASTNode> sanitize(ITree<IDiceASTNode> ast,
IMap<String, ITree<IDiceASTNode>> enviroment) {
- return ast.topDownTransform(
- DiceASTReferenceSanitizer::shouldSanitize, (subTree) -> {
- return doSanitize(subTree, enviroment);
- });
+ return ast.topDownTransform(DiceASTReferenceSanitizer::shouldSanitize, (subTree) -> {
+ return doSanitize(subTree, enviroment);
+ });
}
- private static TopDownTransformResult shouldSanitize(
- IDiceASTNode node) {
+ private static TopDownTransformResult shouldSanitize(IDiceASTNode node) {
if (!node.isOperator()) {
return TopDownTransformResult.SKIP;
}
switch (((OperatorDiceNode) node)) {
- case ASSIGN:
- return TopDownTransformResult.TRANSFORM;
- case ARRAY:
- case LET:
- return TopDownTransformResult.PASSTHROUGH;
- case ADD:
- case COMPOUND:
- case DIVIDE:
- case GROUP:
- case MULTIPLY:
- case SUBTRACT:
- default:
- return TopDownTransformResult.SKIP;
+ case ASSIGN:
+ return TopDownTransformResult.TRANSFORM;
+ case ARRAY:
+ case LET:
+ return TopDownTransformResult.PASSTHROUGH;
+ case ADD:
+ case COMPOUND:
+ case DIVIDE:
+ case GROUP:
+ case MULTIPLY:
+ case SUBTRACT:
+ default:
+ return TopDownTransformResult.SKIP;
}
}
}
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTUtils.java b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTUtils.java
index 92658b6..4d710fe 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTUtils.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTUtils.java
@@ -20,12 +20,11 @@ public class DiceASTUtils {
* Check if a dice AST contains a simple variable reference
*
* @param nameTree
- * The tree to check for a reference in
+ * The tree to check for a reference in
* @return Whether or not a dice AST contains a simple variable
* reference
*/
- public static boolean containsSimpleVariable(
- ITree<IDiceASTNode> nameTree) {
+ public static boolean containsSimpleVariable(ITree<IDiceASTNode> nameTree) {
return nameTree.transformHead((nameNode) -> {
if (nameNode.getType() != DiceASTType.VARIABLE) {
return false;
@@ -39,28 +38,26 @@ public class DiceASTUtils {
* Convert an literal AST node to a dice expression, if possible.
*
* @param tree
- * The node to convert in tree form
+ * The node to convert in tree form
* @return The tree as a dice expression
*
* @throws ClassCastException
- * if the head of the tree is not a literal (implements
- * {@link ILiteralDiceNode})
+ * if the head of the tree is not a literal (implements
+ * {@link ILiteralDiceNode})
* @throws UnsupportedOperationException
- * if the head of the tree is not optimizable
+ * if the head of the tree is not optimizable
*/
- public static IDiceExpression literalToExpression(
- ITree<IDiceASTNode> tree) {
+ public static IDiceExpression literalToExpression(ITree<IDiceASTNode> tree) {
ILiteralDiceNode literalNode = (ILiteralDiceNode) tree.getHead();
switch (literalNode.getLiteralType()) {
- case DICE:
- return ((DiceLiteralNode) literalNode).getValue();
- case INTEGER:
- return new ScalarDie(
- ((IntegerLiteralNode) literalNode).getValue());
- default:
- throw new UnsupportedOperationException(
- "This type of literal isn't convertable to an expression");
+ case DICE:
+ return ((DiceLiteralNode) literalNode).getValue();
+ case INTEGER:
+ return new ScalarDie(((IntegerLiteralNode) literalNode).getValue());
+ default:
+ throw new UnsupportedOperationException(
+ "This type of literal isn't convertable to an expression");
}
}
@@ -68,14 +65,14 @@ public class DiceASTUtils {
* Convert an literal AST node to an integer, if possible.
*
* @param tree
- * The literal node to convert, as a tree
+ * The literal node to convert, as a tree
* @return The node as an integer
*
* @throws ClassCastException
- * if the head of the tree is not a literal (implements
- * {@link ILiteralDiceNode})
+ * if the head of the tree is not a literal (implements
+ * {@link ILiteralDiceNode})
* @throws UnsupportedOperationException
- * if the head of the tree is not optimizable
+ * if the head of the tree is not optimizable
*/
public static int literalToInteger(ITree<IDiceASTNode> tree) {
return tree.transformHead((node) -> {
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/DummyResult.java b/dice-lang/src/bjc/dicelang/v1/ast/DummyResult.java
index 1dd7057..6858022 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/DummyResult.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/DummyResult.java
@@ -16,7 +16,7 @@ public class DummyResult implements IResult {
* Create a new dummy result with a reason
*
* @param data
- * The reason why the result is a dummy
+ * The reason why the result is a dummy
*/
public DummyResult(String data) {
dummyData = data;
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/IOperatorCollapser.java b/dice-lang/src/bjc/dicelang/v1/ast/IOperatorCollapser.java
index df5f95b..bd120a8 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/IOperatorCollapser.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/IOperatorCollapser.java
@@ -13,7 +13,7 @@ import bjc.utils.funcdata.IList;
* @author ben
*
*/
-public interface IOperatorCollapser extends
- Function<IList<IPair<IResult, ITree<IDiceASTNode>>>, IPair<IResult, ITree<IDiceASTNode>>> {
+public interface IOperatorCollapser
+ extends Function<IList<IPair<IResult, ITree<IDiceASTNode>>>, IPair<IResult, ITree<IDiceASTNode>>> {
// Just an alias
}
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/IntegerResult.java b/dice-lang/src/bjc/dicelang/v1/ast/IntegerResult.java
index 2934dfa..b365282 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/IntegerResult.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/IntegerResult.java
@@ -13,7 +13,7 @@ public class IntegerResult implements IResult {
* Create a new integer valued result
*
* @param val
- * The value of the result
+ * The value of the result
*/
public IntegerResult(int val) {
value = val;
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceLiteralNode.java b/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceLiteralNode.java
index 4e9b560..bb979d1 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceLiteralNode.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceLiteralNode.java
@@ -15,7 +15,7 @@ public class DiceLiteralNode implements ILiteralDiceNode {
* Create a new literal from an expression
*
* @param exp
- * The expression to attempt to create a literal from
+ * The expression to attempt to create a literal from
*/
public DiceLiteralNode(IDiceExpression exp) {
expression = exp;
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceOperatorType.java b/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceOperatorType.java
index 35e5680..a5a79a6 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceOperatorType.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceOperatorType.java
@@ -3,8 +3,8 @@ package bjc.dicelang.v1.ast.nodes;
/**
* Represents the different type of operators.
*
- * Mostly, what distinguishes groups is that all the operators in a group
- * have similiar precedence, and operate on similiar things
+ * Mostly, what distinguishes groups is that all the operators in a group have
+ * similiar precedence, and operate on similiar things
*
* @author ben
*
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/nodes/ILiteralDiceNode.java b/dice-lang/src/bjc/dicelang/v1/ast/nodes/ILiteralDiceNode.java
index 11d0d90..ece528b 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/nodes/ILiteralDiceNode.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/nodes/ILiteralDiceNode.java
@@ -11,17 +11,15 @@ public interface ILiteralDiceNode extends IDiceASTNode {
* Check if a token represents a literal, and if so, what type
*
* @param tok
- * The token to check
- * @return The type the literal would be if it is one, or null
- * otherwise
+ * The token to check
+ * @return The type the literal would be if it is one, or null otherwise
*/
static DiceLiteralType getLiteralType(String tok) {
String diceGroup = "\\d*d\\d+\\";
String diceGroupOrNumber = "[(?:" + diceGroup + ")(?:\\d+)]";
- if (tok.matches("\\A" + diceGroupOrNumber + "?" + "c"
- + diceGroupOrNumber + "\\Z")) {
+ if (tok.matches("\\A" + diceGroupOrNumber + "?" + "c" + diceGroupOrNumber + "\\Z")) {
return DiceLiteralType.DICE;
}
@@ -34,7 +32,8 @@ public interface ILiteralDiceNode extends IDiceASTNode {
return DiceLiteralType.INTEGER;
} catch (NumberFormatException nfex) {
// We don't care about details
- // This probably shouldn't return null, but I believe it does so
+ // This probably shouldn't return null, but I believe it
+ // does so
// because where its called checks that. @FIXME
return null;
}
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/nodes/IntegerLiteralNode.java b/dice-lang/src/bjc/dicelang/v1/ast/nodes/IntegerLiteralNode.java
index 4a756d6..1c8aa56 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/nodes/IntegerLiteralNode.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/nodes/IntegerLiteralNode.java
@@ -13,7 +13,7 @@ public class IntegerLiteralNode implements ILiteralDiceNode {
* Create a new integer literal from the given number
*
* @param val
- * The value this node represents
+ * The value this node represents
*/
public IntegerLiteralNode(int val) {
value = val;
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/nodes/OperatorDiceNode.java b/dice-lang/src/bjc/dicelang/v1/ast/nodes/OperatorDiceNode.java
index b52ba49..0af9d81 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/nodes/OperatorDiceNode.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/nodes/OperatorDiceNode.java
@@ -52,34 +52,33 @@ public enum OperatorDiceNode implements IDiceASTNode {
* Create a operator node from a string
*
* @param s
- * The string to convert to a node
+ * The string to convert to a node
* @return The operator corresponding to the node
*/
public static OperatorDiceNode fromString(String s) {
switch (s) {
- case ":=":
- return ASSIGN;
- case "+":
- return ADD;
- case "-":
- return SUBTRACT;
- case "*":
- return MULTIPLY;
- case "/":
- return DIVIDE;
- case "d":
- case "group":
- return GROUP;
- case "c":
- case "compound":
- return COMPOUND;
- case "=>":
- return LET;
- case "[]":
- return ARRAY;
- default:
- throw new IllegalArgumentException(
- s + " is not a valid operator node");
+ case ":=":
+ return ASSIGN;
+ case "+":
+ return ADD;
+ case "-":
+ return SUBTRACT;
+ case "*":
+ return MULTIPLY;
+ case "/":
+ return DIVIDE;
+ case "d":
+ case "group":
+ return GROUP;
+ case "c":
+ case "compound":
+ return COMPOUND;
+ case "=>":
+ return LET;
+ case "[]":
+ return ARRAY;
+ default:
+ throw new IllegalArgumentException(s + " is not a valid operator node");
}
}
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/nodes/VariableDiceNode.java b/dice-lang/src/bjc/dicelang/v1/ast/nodes/VariableDiceNode.java
index d32b6b0..22ddf17 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/nodes/VariableDiceNode.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/nodes/VariableDiceNode.java
@@ -16,7 +16,7 @@ public class VariableDiceNode implements IDiceASTNode {
* Create a new node representing the specified variable
*
* @param varName
- * The name of the variable being referenced
+ * The name of the variable being referenced
*/
public VariableDiceNode(String varName) {
this.variableName = varName;
@@ -74,8 +74,7 @@ public class VariableDiceNode implements IDiceASTNode {
public int hashCode() {
final int prime = 31;
int result = 1;
- result = prime * result
- + ((variableName == null) ? 0 : variableName.hashCode());
+ result = prime * result + ((variableName == null) ? 0 : variableName.hashCode());
return result;
}
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/optimization/ArithmeticCollapser.java b/dice-lang/src/bjc/dicelang/v1/ast/optimization/ArithmeticCollapser.java
index 0d10a07..9fb0a5e 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/optimization/ArithmeticCollapser.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/optimization/ArithmeticCollapser.java
@@ -13,17 +13,15 @@ import bjc.utils.funcdata.IList;
import bjc.utils.data.Tree;
class ArithmeticCollapser {
- private BinaryOperator<Integer> reducer;
- private OperatorDiceNode type;
+ private BinaryOperator<Integer> reducer;
+ private OperatorDiceNode type;
- public ArithmeticCollapser(BinaryOperator<Integer> reducr,
- OperatorDiceNode typ) {
+ public ArithmeticCollapser(BinaryOperator<Integer> reducr, OperatorDiceNode typ) {
reducer = reducr;
this.type = typ;
}
- public ITree<IDiceASTNode> collapse(
- IList<ITree<IDiceASTNode>> children) {
+ public ITree<IDiceASTNode> collapse(IList<ITree<IDiceASTNode>> children) {
boolean allConstant = children.allMatch((subtree) -> {
return subtree.transformHead((node) -> {
if (node.getType() == DiceASTType.LITERAL) {
@@ -40,10 +38,8 @@ class ArithmeticCollapser {
int initState = DiceASTUtils.literalToInteger(children.first());
- return children.tail().reduceAux(initState,
- (currentNode, state) -> {
- return reducer.apply(state,
- DiceASTUtils.literalToInteger(currentNode));
- }, (state) -> new Tree<>(new IntegerLiteralNode(state)));
+ return children.tail().reduceAux(initState, (currentNode, state) -> {
+ return reducer.apply(state, DiceASTUtils.literalToInteger(currentNode));
+ }, (state) -> new Tree<>(new IntegerLiteralNode(state)));
}
}
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/optimization/ConstantCollapser.java b/dice-lang/src/bjc/dicelang/v1/ast/optimization/ConstantCollapser.java
index 89f17db..a0daf31 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/optimization/ConstantCollapser.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/optimization/ConstantCollapser.java
@@ -16,21 +16,20 @@ import bjc.utils.data.Tree;
*
*/
public class ConstantCollapser implements IOptimizationPass {
- private static final ArithmeticCollapser additionCollapser = new ArithmeticCollapser(
+ private static final ArithmeticCollapser additionCollapser = new ArithmeticCollapser(
(left, right) -> left + right, OperatorDiceNode.ADD);
- private static final ArithmeticCollapser divideCollapser = new ArithmeticCollapser(
+ private static final ArithmeticCollapser divideCollapser = new ArithmeticCollapser(
(left, right) -> left / right, OperatorDiceNode.DIVIDE);
- private static final ArithmeticCollapser multiplyCollapser = new ArithmeticCollapser(
+ private static final ArithmeticCollapser multiplyCollapser = new ArithmeticCollapser(
(left, right) -> left * right, OperatorDiceNode.MULTIPLY);
- private static final ArithmeticCollapser subtractCollapser = new ArithmeticCollapser(
+ private static final ArithmeticCollapser subtractCollapser = new ArithmeticCollapser(
(left, right) -> left - right, OperatorDiceNode.SUBTRACT);
- private static final ArithmeticCollapser compoundCollapser = new ArithmeticCollapser(
- (left, right) -> Integer.parseInt(
- Integer.toString(left) + Integer.toString(left)),
+ private static final ArithmeticCollapser compoundCollapser = new ArithmeticCollapser(
+ (left, right) -> Integer.parseInt(Integer.toString(left) + Integer.toString(left)),
OperatorDiceNode.COMPOUND);
@Override
@@ -40,51 +39,46 @@ public class ConstantCollapser implements IOptimizationPass {
}
@Override
- public ITree<IDiceASTNode> optimizeOperator(IDiceASTNode operator,
- IList<ITree<IDiceASTNode>> children) {
+ public ITree<IDiceASTNode> optimizeOperator(IDiceASTNode operator, IList<ITree<IDiceASTNode>> children) {
if (!operator.isOperator()) {
return new Tree<>(operator, children);
}
switch ((OperatorDiceNode) operator) {
- case ADD:
- return additionCollapser.collapse(children);
- case DIVIDE:
- return divideCollapser.collapse(children);
- case MULTIPLY:
- return multiplyCollapser.collapse(children);
- case SUBTRACT:
- return subtractCollapser.collapse(children);
- case COMPOUND:
- return compoundCollapser.collapse(children);
- case GROUP:
- if (children.getSize() != 2) {
- return new Tree<>(operator, children);
- }
+ case ADD:
+ return additionCollapser.collapse(children);
+ case DIVIDE:
+ return divideCollapser.collapse(children);
+ case MULTIPLY:
+ return multiplyCollapser.collapse(children);
+ case SUBTRACT:
+ return subtractCollapser.collapse(children);
+ case COMPOUND:
+ return compoundCollapser.collapse(children);
+ case GROUP:
+ if (children.getSize() != 2) {
+ return new Tree<>(operator, children);
+ }
- ComplexDice dice = new ComplexDice(
- DiceASTUtils.literalToExpression(
- children.getByIndex(0)),
- DiceASTUtils.literalToExpression(
- children.getByIndex(1)));
+ ComplexDice dice = new ComplexDice(DiceASTUtils.literalToExpression(children.getByIndex(0)),
+ DiceASTUtils.literalToExpression(children.getByIndex(1)));
- if (dice.canOptimize()) {
- return new Tree<>(
- new IntegerLiteralNode(dice.optimize()));
- }
+ if (dice.canOptimize()) {
+ return new Tree<>(new IntegerLiteralNode(dice.optimize()));
+ }
+ return new Tree<>(operator, children);
+ case ARRAY:
+ if (children.getSize() != 1) {
return new Tree<>(operator, children);
- case ARRAY:
- if (children.getSize() != 1) {
- return new Tree<>(operator, children);
- }
+ }
- return children.first();
- case ASSIGN:
- case LET:
- default:
- // We don't optimize these operators
- return new Tree<>(operator, children);
+ return children.first();
+ case ASSIGN:
+ case LET:
+ default:
+ // We don't optimize these operators
+ return new Tree<>(operator, children);
}
}
}
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/optimization/IOptimizationPass.java b/dice-lang/src/bjc/dicelang/v1/ast/optimization/IOptimizationPass.java
index 58943fa..b09d95d 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/optimization/IOptimizationPass.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/optimization/IOptimizationPass.java
@@ -15,7 +15,7 @@ public interface IOptimizationPass {
* Optimize a leaf in the tree
*
* @param leafNode
- * The node to optimize
+ * The node to optimize
* @return The optimized node
*/
public ITree<IDiceASTNode> optimizeLeaf(IDiceASTNode leafNode);
@@ -24,11 +24,10 @@ public interface IOptimizationPass {
* Optimize an operator in an AST node
*
* @param operator
- * The operator being optimized
+ * The operator being optimized
* @param children
- * The children of the operator being optimized
+ * The children of the operator being optimized
* @return The optimized node
*/
- public ITree<IDiceASTNode> optimizeOperator(IDiceASTNode operator,
- IList<ITree<IDiceASTNode>> children);
+ public ITree<IDiceASTNode> optimizeOperator(IDiceASTNode operator, IList<ITree<IDiceASTNode>> children);
}
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/optimization/OperationCondenser.java b/dice-lang/src/bjc/dicelang/v1/ast/optimization/OperationCondenser.java
index 4a34167..f00d390 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/optimization/OperationCondenser.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/optimization/OperationCondenser.java
@@ -20,18 +20,15 @@ public class OperationCondenser {
* Condense chained similiar operations into a single level
*
* @param ast
- * The AST to condense
+ * The AST to condense
* @return The condensed AST
*/
public static ITree<IDiceASTNode> condense(ITree<IDiceASTNode> ast) {
- return ast.topDownTransform(OperationCondenser::pickNode,
- OperationCondenser::doCondense);
+ return ast.topDownTransform(OperationCondenser::pickNode, OperationCondenser::doCondense);
}
- private static ITree<IDiceASTNode> doCondense(
- ITree<IDiceASTNode> ast) {
- OperatorDiceNode operation = ast
- .transformHead((node) -> (OperatorDiceNode) node);
+ private static ITree<IDiceASTNode> doCondense(ITree<IDiceASTNode> ast) {
+ OperatorDiceNode operation = ast.transformHead((node) -> (OperatorDiceNode) node);
IHolder<Boolean> canCondense = new Identity<>(true);
@@ -72,35 +69,32 @@ public class OperationCondenser {
private static TopDownTransformResult pickNode(IDiceASTNode node) {
switch (node.getType()) {
- case LITERAL:
- return TopDownTransformResult.SKIP;
- case OPERATOR:
- return pickOperator((OperatorDiceNode) node);
- case VARIABLE:
- return TopDownTransformResult.SKIP;
- default:
- throw new UnsupportedOperationException(
- "Attempted to traverse unknown node type " + node);
+ case LITERAL:
+ return TopDownTransformResult.SKIP;
+ case OPERATOR:
+ return pickOperator((OperatorDiceNode) node);
+ case VARIABLE:
+ return TopDownTransformResult.SKIP;
+ default:
+ throw new UnsupportedOperationException("Attempted to traverse unknown node type " + node);
}
}
- private static TopDownTransformResult pickOperator(
- OperatorDiceNode node) {
+ private static TopDownTransformResult pickOperator(OperatorDiceNode node) {
switch (node) {
- case ADD:
- case MULTIPLY:
- case SUBTRACT:
- case DIVIDE:
- case COMPOUND:
- return TopDownTransformResult.PUSHDOWN;
- case ARRAY:
- case ASSIGN:
- case GROUP:
- case LET:
- return TopDownTransformResult.PASSTHROUGH;
- default:
- throw new UnsupportedOperationException(
- "Attempted to traverse unknown operator " + node);
+ case ADD:
+ case MULTIPLY:
+ case SUBTRACT:
+ case DIVIDE:
+ case COMPOUND:
+ return TopDownTransformResult.PUSHDOWN;
+ case ARRAY:
+ case ASSIGN:
+ case GROUP:
+ case LET:
+ return TopDownTransformResult.PASSTHROUGH;
+ default:
+ throw new UnsupportedOperationException("Attempted to traverse unknown operator " + node);
}
}
}