summaryrefslogtreecommitdiff
path: root/dice-lang/src/bjc/dicelang/v1/ast
diff options
context:
space:
mode:
authorEVE <EVE@EVE-PC>2017-03-14 12:08:11 -0400
committerEVE <EVE@EVE-PC>2017-03-14 12:08:11 -0400
commit635d3150e3e85c01b777ff165e21fa8965d58440 (patch)
tree3389128f83a5a79f8d0eec0a0e19f54b9d117b66 /dice-lang/src/bjc/dicelang/v1/ast
parente59e2a97773f93bdd25bd4680809c10699f0feb3 (diff)
Cleanup
Diffstat (limited to 'dice-lang/src/bjc/dicelang/v1/ast')
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/ArithmeticCollapser.java30
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/ArrayResult.java6
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/DiceASTEvaluator.java48
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/DiceASTInliner.java32
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/DiceASTOptimizer.java6
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/DiceASTParser.java43
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceChecker.java12
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceSanitizer.java54
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/DiceASTUtils.java18
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/DummyResult.java6
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/IOperatorCollapser.java6
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/IResult.java4
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/IntegerResult.java6
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/ResultType.java2
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceASTType.java2
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceLiteralNode.java6
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceLiteralType.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/IDiceASTNode.java6
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/nodes/ILiteralDiceNode.java19
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/nodes/IntegerLiteralNode.java6
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/nodes/OperatorDiceNode.java8
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/nodes/VariableDiceNode.java34
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/nodes/package-info.java2
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/optimization/ArithmeticCollapser.java17
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/optimization/ConstantCollapser.java22
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/optimization/IOptimizationPass.java6
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/optimization/OperationCondenser.java22
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/optimization/package-info.java2
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/package-info.java2
30 files changed, 188 insertions, 245 deletions
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/ArithmeticCollapser.java b/dice-lang/src/bjc/dicelang/v1/ast/ArithmeticCollapser.java
index 1a41ce6..28f29f0 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/ArithmeticCollapser.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/ArithmeticCollapser.java
@@ -1,18 +1,18 @@
package bjc.dicelang.v1.ast;
-import java.util.function.BinaryOperator;
-
import bjc.dicelang.v1.ast.nodes.IDiceASTNode;
import bjc.dicelang.v1.ast.nodes.OperatorDiceNode;
import bjc.utils.data.IPair;
import bjc.utils.data.ITree;
import bjc.utils.data.Pair;
-import bjc.utils.funcdata.IList;
import bjc.utils.data.Tree;
+import bjc.utils.funcdata.IList;
+
+import java.util.function.BinaryOperator;
/**
* Responsible for collapsing arithmetic operators
- *
+ *
* @author ben
*
*/
@@ -54,17 +54,15 @@ final class ArithmeticCollapser implements IOperatorCollapser {
IList<IResult> currentList = ((ArrayResult) currentValue).getValue();
IList<IResult> accumulatedList = ((ArrayResult) accumulatedValue).getValue();
- if (currentList.getSize() != accumulatedList.getSize()) {
+ if(currentList.getSize() != accumulatedList.getSize())
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) {
+ if(currentNotInt || accumulatedNotInt)
throw new UnsupportedOperationException("Nesting of array operations isn't allowed");
- }
int accumulatedInt = ((IntegerResult) accumulatedNode).getValue();
int currentInt = ((IntegerResult) currentNode).getValue();
@@ -77,7 +75,7 @@ final class ArithmeticCollapser implements IOperatorCollapser {
private IPair<IResult, ITree<IDiceASTNode>> doArithmeticCollapse(IResult accumulatedValue,
ITree<IDiceASTNode> accumulatedTree, IResult currentValue) {
- if (accumulatedValue.getType() == ResultType.DUMMY || currentValue.getType() == ResultType.DUMMY) {
+ 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() + ").");
@@ -88,8 +86,8 @@ final class ArithmeticCollapser implements IOperatorCollapser {
boolean currentIsInt = currentValue.getType() == ResultType.INTEGER;
boolean accumulatedIsInt = accumulatedValue.getType() == ResultType.INTEGER;
- if (!currentIsInt) {
- if (!accumulatedIsInt) {
+ if(!currentIsInt) {
+ if(!accumulatedIsInt) {
IList<IResult> resultList = combineArrayResults(accumulatedValue, currentValue);
return new Pair<>(new ArrayResult(resultList), accumulatedTree);
@@ -99,7 +97,7 @@ final class ArithmeticCollapser implements IOperatorCollapser {
accumulatedValue, true);
return new Pair<>(new ArrayResult(resultList), accumulatedTree);
- } else if (!accumulatedIsInt) {
+ } else if(!accumulatedIsInt) {
IList<IResult> resultList = halfCombineLists(((ArrayResult) accumulatedValue).getValue(),
currentValue, false);
@@ -115,22 +113,20 @@ final class ArithmeticCollapser implements IOperatorCollapser {
}
private IList<IResult> halfCombineLists(IList<IResult> list, IResult scalar, boolean scalarLeft) {
- if (scalar.getType() != ResultType.INTEGER) {
+ if(scalar.getType() != ResultType.INTEGER)
throw new UnsupportedOperationException("Nested array operations not supported");
- }
int scalarInt = ((IntegerResult) scalar).getValue();
return list.map((element) -> {
- if (element.getType() != ResultType.INTEGER) {
+ if(element.getType() != ResultType.INTEGER)
throw new UnsupportedOperationException("Nested array operations not supported");
- }
int elementInt = ((IntegerResult) element).getValue();
IResult combinedValue;
- if (scalarLeft) {
+ if(scalarLeft) {
combinedValue = new IntegerResult(valueOp.apply(scalarInt, elementInt));
} else {
combinedValue = new IntegerResult(valueOp.apply(elementInt, scalarInt));
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/ArrayResult.java b/dice-lang/src/bjc/dicelang/v1/ast/ArrayResult.java
index c8a35a6..1053360 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/ArrayResult.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/ArrayResult.java
@@ -4,7 +4,7 @@ import bjc.utils.funcdata.IList;
/**
* Represents a result that is an array of other results
- *
+ *
* @author ben
*
* TODO finish implementing me
@@ -14,7 +14,7 @@ public class ArrayResult implements IResult {
/**
* Create a new array-valued result
- *
+ *
* @param results
* The results in the array
*/
@@ -29,7 +29,7 @@ public class ArrayResult implements IResult {
/**
* Get the value of this result
- *
+ *
* @return The value of this result
*/
public IList<IResult> getValue() {
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTEvaluator.java b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTEvaluator.java
index af31ad7..8273525 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTEvaluator.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTEvaluator.java
@@ -1,7 +1,5 @@
package bjc.dicelang.v1.ast;
-import java.util.function.Supplier;
-
import bjc.dicelang.v1.ComplexDice;
import bjc.dicelang.v1.ast.nodes.DiceASTType;
import bjc.dicelang.v1.ast.nodes.DiceLiteralNode;
@@ -17,15 +15,17 @@ import bjc.utils.data.ITree;
import bjc.utils.data.Identity;
import bjc.utils.data.LazyPair;
import bjc.utils.data.Pair;
+import bjc.utils.data.Tree;
import bjc.utils.funcdata.FunctionalList;
import bjc.utils.funcdata.FunctionalMap;
import bjc.utils.funcdata.IList;
import bjc.utils.funcdata.IMap;
-import bjc.utils.data.Tree;
+
+import java.util.function.Supplier;
/**
* Evaluate a dice AST to an integer value
- *
+ *
* @author ben
*
*/
@@ -33,7 +33,7 @@ public class DiceASTEvaluator {
private static IResult bindLiteralValue(IDiceASTNode leafNode, IMap<String, ITree<IDiceASTNode>> enviroment) {
String variableName = ((VariableDiceNode) leafNode).getVariable();
- if (enviroment.containsKey(variableName)) {
+ if(enviroment.containsKey(variableName)) {
IResult result = evaluateAST(enviroment.get(variableName), enviroment);
return result;
@@ -45,7 +45,7 @@ public class DiceASTEvaluator {
/**
* Build the map of operations to use when collapsing the AST
- *
+ *
* @param enviroment
* The enviroment to evaluate bindings and such against
* @return The operations to use when collapsing the AST
@@ -114,11 +114,9 @@ public class DiceASTEvaluator {
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 "
- + nameNode.getRight());
- }
+ if(nameTree.getHead().getType() != DiceASTType.VARIABLE) throw new UnsupportedOperationException(
+ "Assigning to complex variables isn't supported. Problem node is "
+ + nameNode.getRight());
String varName = child.transformHead((nameNod) -> {
return ((VariableDiceNode) nameNod).getVariable();
@@ -131,7 +129,7 @@ public class DiceASTEvaluator {
/**
* Evaluate the provided AST to a numeric value
- *
+ *
* @param expression
* The expression to evaluate
* @param enviroment
@@ -150,7 +148,7 @@ public class DiceASTEvaluator {
IMap<String, ITree<IDiceASTNode>> enviroment) {
ITree<IDiceASTNode> returnedAST = new Tree<>(leafNode);
- switch (leafNode.getType()) {
+ switch(leafNode.getType()) {
case LITERAL:
return new Pair<>(evaluateLiteral(leafNode), returnedAST);
@@ -168,7 +166,7 @@ public class DiceASTEvaluator {
private static IResult evaluateLiteral(IDiceASTNode leafNode) {
DiceLiteralType literalType = ((ILiteralDiceNode) leafNode).getLiteralType();
- switch (literalType) {
+ switch(literalType) {
case DICE:
int diceRoll = ((DiceLiteralNode) leafNode).getValue().roll();
@@ -185,17 +183,15 @@ public class DiceASTEvaluator {
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);
- }
+ if(nodes.getSize() != 2) throw new UnsupportedOperationException(
+ "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);
return nameNode.bindRight((nameTree) -> {
return valueNode.bind((valueValue, valueTree) -> {
- if (DiceASTUtils.containsSimpleVariable(nameTree)) {
+ if(DiceASTUtils.containsSimpleVariable(nameTree)) {
String varName = nameTree.transformHead((nameNod) -> {
return ((VariableDiceNode) nameNod).getVariable();
});
@@ -203,12 +199,11 @@ public class DiceASTEvaluator {
enviroment.put(varName, valueTree);
return new Pair<>(valueValue, nameTree);
- } else if (nameTree.getHead() == OperatorDiceNode.ARRAY) {
- if (valueTree.getHead() == OperatorDiceNode.ARRAY) {
- if (nameTree.getChildrenCount() != valueTree.getChildrenCount()) {
+ } else if(nameTree.getHead() == OperatorDiceNode.ARRAY) {
+ if(valueTree.getHead() == OperatorDiceNode.ARRAY) {
+ if(nameTree.getChildrenCount() != valueTree.getChildrenCount())
throw new UnsupportedOperationException(
"Array assignment must be between two equal length arrays");
- }
IHolder<Integer> childCount = new Identity<>(0);
@@ -242,9 +237,7 @@ 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");
- }
+ if(nodes.getSize() != 2) 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);
@@ -262,9 +255,8 @@ public class DiceASTEvaluator {
private static IPair<IResult, ITree<IDiceASTNode>> parseLet(IMap<String, ITree<IDiceASTNode>> enviroment,
IList<IPair<IResult, ITree<IDiceASTNode>>> nodes) {
- if (nodes.getSize() != 2) {
+ if(nodes.getSize() != 2)
throw new UnsupportedOperationException("Can only use let with two expressions.");
- }
ITree<IDiceASTNode> bindTree = nodes.getByIndex(0).getRight();
ITree<IDiceASTNode> expressionTree = nodes.getByIndex(1).getRight();
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTInliner.java b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTInliner.java
index 38e1361..74e59c6 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTInliner.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTInliner.java
@@ -4,21 +4,21 @@ import bjc.dicelang.v1.ast.nodes.DiceASTType;
import bjc.dicelang.v1.ast.nodes.IDiceASTNode;
import bjc.dicelang.v1.ast.nodes.VariableDiceNode;
import bjc.utils.data.ITree;
+import bjc.utils.data.Tree;
import bjc.utils.funcdata.FunctionalList;
import bjc.utils.funcdata.IList;
import bjc.utils.funcdata.IMap;
-import bjc.utils.data.Tree;
/**
* Inline variables in a dice AST
- *
+ *
* @author ben
*
*/
public class DiceASTInliner {
/**
* Inline all the variables in the AST
- *
+ *
* @param ast
* The AST to inline variables into
* @param enviroment
@@ -35,22 +35,18 @@ public class DiceASTInliner {
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) {
- return new Tree<>(node);
- }
+ if(node.getType() != DiceASTType.VARIABLE) return new Tree<>(node);
// Get the name of what we're inlining
String variableName = ((VariableDiceNode) node).getVariable();
// If we're inlining only certain variables, do so
- if (specificInline) {
+ if(specificInline) {
// Only inline the variable if we're supposed to
- if (variableNames.contains(variableName)) {
+ if(variableNames.contains(variableName)) {
// You can't inline non-existent variables
- if (!enviroment.containsKey(variableName)) {
- throw new UnsupportedOperationException(
- "Attempted to inline non-existant variable " + variableName);
- }
+ if(!enviroment.containsKey(variableName)) throw new UnsupportedOperationException(
+ "Attempted to inline non-existant variable " + variableName);
// Return the tree for the variable
return enviroment.get(variableName);
@@ -61,10 +57,8 @@ public class DiceASTInliner {
}
// You can't inline non-existent variables
- if (!enviroment.containsKey(variableName)) {
- throw new UnsupportedOperationException(
- "Attempted to inline non-existant variable " + variableName);
- }
+ if(!enviroment.containsKey(variableName)) throw new UnsupportedOperationException(
+ "Attempted to inline non-existant variable " + variableName);
// Return the tree for the variable
return enviroment.get(variableName);
@@ -72,7 +66,7 @@ public class DiceASTInliner {
/**
* Inline the specified variables in the AST
- *
+ *
* @param ast
* The AST to inline variables into
* @param enviroment
@@ -89,7 +83,7 @@ public class DiceASTInliner {
/**
* Inline the specified variables in the AST
- *
+ *
* @param ast
* The AST to inline variables into
* @param enviroment
@@ -101,7 +95,7 @@ public class DiceASTInliner {
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) {
+ if(variables != null && variables.length > 0) {
IList<String> variableNames = new FunctionalList<>(variables);
// Selectively inline each tree node
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTOptimizer.java b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTOptimizer.java
index a93de33..fa4f0ca 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTOptimizer.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTOptimizer.java
@@ -9,7 +9,7 @@ import bjc.utils.funcdata.IMap;
/**
* Contains optimizations appliable to a dice AST
- *
+ *
* @author ben
*
*/
@@ -25,7 +25,7 @@ public class DiceASTOptimizer {
/**
* Add a pass to the list of optimization passes
- *
+ *
* @param pass
* The pass to add
*/
@@ -35,7 +35,7 @@ public class DiceASTOptimizer {
/**
* Optimize the passed in tree
- *
+ *
* @param ast
* The tree to optimize
* @param enviroment
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTParser.java b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTParser.java
index 87f3640..4a2822c 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTParser.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTParser.java
@@ -1,10 +1,5 @@
package bjc.dicelang.v1.ast;
-import java.util.Deque;
-import java.util.InputMismatchException;
-import java.util.function.Function;
-import java.util.function.Predicate;
-
import bjc.dicelang.v1.IDiceExpression;
import bjc.dicelang.v1.ast.nodes.DiceLiteralNode;
import bjc.dicelang.v1.ast.nodes.DiceLiteralType;
@@ -14,17 +9,22 @@ import bjc.dicelang.v1.ast.nodes.IntegerLiteralNode;
import bjc.dicelang.v1.ast.nodes.OperatorDiceNode;
import bjc.dicelang.v1.ast.nodes.VariableDiceNode;
import bjc.utils.data.ITree;
+import bjc.utils.data.Tree;
import bjc.utils.funcdata.FunctionalList;
import bjc.utils.funcdata.FunctionalMap;
import bjc.utils.funcdata.IList;
import bjc.utils.funcdata.IMap;
-import bjc.utils.data.Tree;
import bjc.utils.funcutils.StringUtils;
import bjc.utils.parserutils.TreeConstructor;
+import java.util.Deque;
+import java.util.InputMismatchException;
+import java.util.function.Function;
+import java.util.function.Predicate;
+
/**
* Parse a string expression into AST form. Doesn't do anything else
- *
+ *
* @author ben
*
*/
@@ -32,8 +32,8 @@ public class DiceASTParser {
private static IDiceASTNode convertLeafNode(String leafNode) {
DiceLiteralType literalType = ILiteralDiceNode.getLiteralType(leafNode);
- if (literalType != null) {
- switch (literalType) {
+ if(literalType != null) {
+ switch(literalType) {
case DICE:
return new DiceLiteralNode(IDiceExpression.toExpression(leafNode));
case INTEGER:
@@ -44,9 +44,8 @@ public class DiceASTParser {
}
}
- if (leafNode.matches("[+-]?\\d*\\.\\d+")) {
+ if(leafNode.matches("[+-]?\\d*\\.\\d+"))
throw new InputMismatchException("Floating point literals are not supported");
- }
return new VariableDiceNode(leafNode);
}
@@ -54,7 +53,7 @@ public class DiceASTParser {
private static IDiceASTNode convertOperatorNode(String operatorNode) {
try {
return OperatorDiceNode.fromString(operatorNode);
- } catch (IllegalArgumentException iaex) {
+ } catch(IllegalArgumentException iaex) {
InputMismatchException imex = new InputMismatchException(
"Attempted to parse invalid operator " + operatorNode);
@@ -66,7 +65,7 @@ public class DiceASTParser {
/**
* Create an AST from a list of tokens
- *
+ *
* @param tokens
* The list of tokens to convert
* @return An AST built from the tokens
@@ -74,9 +73,8 @@ public class DiceASTParser {
public static ITree<IDiceASTNode> createFromString(IList<String> tokens) {
// Mark arrays as special operators
Predicate<String> specialPicker = (operator) -> {
- if (StringUtils.containsOnly(operator, "\\[") || StringUtils.containsOnly(operator, "\\]")) {
+ if(StringUtils.containsOnly(operator, "\\[") || StringUtils.containsOnly(operator, "\\]"))
return true;
- }
return false;
};
@@ -108,21 +106,18 @@ public class DiceASTParser {
}
private static boolean isOperatorNode(String token) {
- if (StringUtils.containsOnly(token, "\\[")) {
- return true;
- } else if (StringUtils.containsOnly(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 ]
+ if(token.equals("[]")) // This is a synthetic operator,
+ // constructed by [ and ]
return true;
- }
try {
OperatorDiceNode.fromString(token);
return true;
- } catch (IllegalArgumentException iaex) {
+ } catch(IllegalArgumentException iaex) {
// We don't care about details
return false;
}
@@ -131,7 +126,7 @@ public class DiceASTParser {
private static ITree<String> parseCloseArray(Deque<ITree<String>> queuedTrees) {
IList<ITree<String>> children = new FunctionalList<>();
- while (shouldContinuePopping(queuedTrees)) {
+ while(shouldContinuePopping(queuedTrees)) {
children.add(queuedTrees.pop());
}
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceChecker.java b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceChecker.java
index 5be2090..f668c72 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceChecker.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceChecker.java
@@ -1,15 +1,15 @@
package bjc.dicelang.v1.ast;
-import java.util.function.Consumer;
-
import bjc.dicelang.v1.ast.nodes.DiceASTType;
import bjc.dicelang.v1.ast.nodes.IDiceASTNode;
import bjc.dicelang.v1.ast.nodes.VariableDiceNode;
import bjc.utils.data.IHolder;
+import java.util.function.Consumer;
+
/**
* Check if the specified node references a particular variable
- *
+ *
* @author ben
*
*/
@@ -23,7 +23,7 @@ public final class DiceASTReferenceChecker implements Consumer<IDiceASTNode> {
/**
* Create a new reference checker
- *
+ *
* @param referencesVar
* The holder of whether the variable is referenced or
* not
@@ -42,13 +42,13 @@ public final class DiceASTReferenceChecker implements Consumer<IDiceASTNode> {
/**
* Check if a given AST node directly references the specified variable
- *
+ *
* @param astNode
* The node to check
* @return Whether or not the node directly the variable
*/
private boolean isDirectReference(IDiceASTNode astNode) {
- if (astNode.getType() == DiceASTType.VARIABLE) {
+ if(astNode.getType() == DiceASTType.VARIABLE) {
VariableDiceNode node = (VariableDiceNode) astNode;
return node.getVariable().equals(varName);
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceSanitizer.java b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceSanitizer.java
index 5bb07fd..ec595ea 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceSanitizer.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceSanitizer.java
@@ -6,61 +6,55 @@ import bjc.dicelang.v1.ast.nodes.VariableDiceNode;
import bjc.utils.data.IHolder;
import bjc.utils.data.ITree;
import bjc.utils.data.Identity;
-
-import bjc.utils.funcdata.IMap;
import bjc.utils.data.TopDownTransformResult;
import bjc.utils.data.Tree;
+import bjc.utils.funcdata.IMap;
/**
* 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
*
*/
public class DiceASTReferenceSanitizer {
private static ITree<IDiceASTNode> doSanitize(ITree<IDiceASTNode> ast,
IMap<String, ITree<IDiceASTNode>> enviroment) {
- if (ast.getChildrenCount() != 2) {
+ if(ast.getChildrenCount() != 2)
throw new UnsupportedOperationException("Assignment must have two arguments.");
- }
ITree<IDiceASTNode> nameTree = ast.getChild(0);
ITree<IDiceASTNode> valueTree = ast.getChild(1);
- if (!DiceASTUtils.containsSimpleVariable(nameTree)) {
- if (nameTree.getHead() == OperatorDiceNode.ARRAY) {
+ if(!DiceASTUtils.containsSimpleVariable(nameTree)) {
+ if(nameTree.getHead() == OperatorDiceNode.ARRAY) {
IHolder<Boolean> allSimpleVariables = new Identity<>(true);
nameTree.doForChildren((child) -> {
- if (allSimpleVariables.getValue()) {
+ if(allSimpleVariables.getValue()) {
boolean isSimple = DiceASTUtils.containsSimpleVariable(child);
allSimpleVariables.replace(isSimple);
}
});
- if (!allSimpleVariables.getValue()) {
- throw new UnsupportedOperationException(
- "Array assignment must be between variables and"
- + " a expression/array of expressions");
- }
+ if(!allSimpleVariables.getValue()) throw new UnsupportedOperationException(
+ "Array assignment must be between variables and"
+ + " a expression/array of expressions");
- if (valueTree.getHead() == OperatorDiceNode.ARRAY) {
- if (nameTree.getChildrenCount() != valueTree.getChildrenCount()) {
+ if(valueTree.getHead() == OperatorDiceNode.ARRAY) {
+ if(nameTree.getChildrenCount() != valueTree.getChildrenCount())
throw new UnsupportedOperationException(
"Array assignment between arrays must be"
+ " between two arrays of equal length");
- }
}
- } else {
+ } else
throw new UnsupportedOperationException(
"Assignment must be between a variable and a expression");
- }
}
- if (nameTree.getHead() == OperatorDiceNode.ARRAY) {
- if (valueTree.getHead() == OperatorDiceNode.ARRAY) {
+ if(nameTree.getHead() == OperatorDiceNode.ARRAY) {
+ if(valueTree.getHead() == OperatorDiceNode.ARRAY) {
IHolder<Integer> childCounter = new Identity<>(0);
ITree<IDiceASTNode> returnTree = new Tree<>(OperatorDiceNode.ARRAY);
@@ -75,7 +69,7 @@ public class DiceASTReferenceSanitizer {
ITree<IDiceASTNode> sanitizedSubtree = doSingleSanitize(ast, enviroment, child,
currentValue, variableName);
- if (sanitizedSubtree == null) {
+ if(sanitizedSubtree == null) {
ITree<IDiceASTNode> oldTree = new Tree<>(ast.getHead(), child,
currentValue);
@@ -98,7 +92,7 @@ public class DiceASTReferenceSanitizer {
ITree<IDiceASTNode> sanitizedChild = doSingleSanitize(ast, enviroment, child, valueTree,
variableName);
- if (sanitizedChild == null) {
+ if(sanitizedChild == null) {
ITree<IDiceASTNode> oldTree = new Tree<>(ast.getHead(), child, valueTree);
returnTree.addChild(oldTree);
@@ -115,9 +109,7 @@ public class DiceASTReferenceSanitizer {
ITree<IDiceASTNode> sanitizedTree = doSingleSanitize(ast, enviroment, nameTree, valueTree,
variableName);
- if (sanitizedTree == null) {
- return ast;
- }
+ if(sanitizedTree == null) return ast;
return sanitizedTree;
}
@@ -125,7 +117,7 @@ public class DiceASTReferenceSanitizer {
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)) {
+ if(enviroment.containsKey(variableName)) {
// @ is a meta-variable standing for the left side of an
// assignment
ITree<IDiceASTNode> oldVal = enviroment.put("@", enviroment.get(variableName));
@@ -136,7 +128,7 @@ public class DiceASTReferenceSanitizer {
ITree<IDiceASTNode> inlinedValue = DiceASTInliner.selectiveInline(valueTree, enviroment,
variableName, "last", "@");
- if (oldVal != null) {
+ if(oldVal != null) {
enviroment.put("@", oldVal);
} else {
enviroment.remove("@");
@@ -150,7 +142,7 @@ public class DiceASTReferenceSanitizer {
/**
* Sanitize the references in an AST
- *
+ *
* @param ast
* @param enviroment
* @return The sanitized AST
@@ -163,11 +155,9 @@ public class DiceASTReferenceSanitizer {
}
private static TopDownTransformResult shouldSanitize(IDiceASTNode node) {
- if (!node.isOperator()) {
- return TopDownTransformResult.SKIP;
- }
+ if(!node.isOperator()) return TopDownTransformResult.SKIP;
- switch (((OperatorDiceNode) node)) {
+ switch((OperatorDiceNode) node) {
case ASSIGN:
return TopDownTransformResult.TRANSFORM;
case ARRAY:
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTUtils.java b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTUtils.java
index 4d710fe..e37d3c3 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTUtils.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTUtils.java
@@ -11,14 +11,14 @@ import bjc.utils.data.ITree;
/**
* Functions that are useful when dealing with dice ASTs
- *
+ *
* @author ben
*
*/
public class DiceASTUtils {
/**
* Check if a dice AST contains a simple variable reference
- *
+ *
* @param nameTree
* The tree to check for a reference in
* @return Whether or not a dice AST contains a simple variable
@@ -26,9 +26,7 @@ public class DiceASTUtils {
*/
public static boolean containsSimpleVariable(ITree<IDiceASTNode> nameTree) {
return nameTree.transformHead((nameNode) -> {
- if (nameNode.getType() != DiceASTType.VARIABLE) {
- return false;
- }
+ if(nameNode.getType() != DiceASTType.VARIABLE) return false;
return true;
});
@@ -36,11 +34,11 @@ public class DiceASTUtils {
/**
* Convert an literal AST node to a dice expression, if possible.
- *
+ *
* @param tree
* 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})
@@ -50,7 +48,7 @@ public class DiceASTUtils {
public static IDiceExpression literalToExpression(ITree<IDiceASTNode> tree) {
ILiteralDiceNode literalNode = (ILiteralDiceNode) tree.getHead();
- switch (literalNode.getLiteralType()) {
+ switch(literalNode.getLiteralType()) {
case DICE:
return ((DiceLiteralNode) literalNode).getValue();
case INTEGER:
@@ -63,11 +61,11 @@ public class DiceASTUtils {
/**
* Convert an literal AST node to an integer, if possible.
- *
+ *
* @param 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})
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/DummyResult.java b/dice-lang/src/bjc/dicelang/v1/ast/DummyResult.java
index 6858022..2328d3c 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/DummyResult.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/DummyResult.java
@@ -2,7 +2,7 @@ package bjc.dicelang.v1.ast;
/**
* A dummy result
- *
+ *
* @author ben
*
*/
@@ -14,7 +14,7 @@ public class DummyResult implements IResult {
/**
* Create a new dummy result with a reason
- *
+ *
* @param data
* The reason why the result is a dummy
*/
@@ -24,7 +24,7 @@ public class DummyResult implements IResult {
/**
* Get the data in this dummy
- *
+ *
* @return The reason why this result is a dummy
*/
public String getData() {
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/IOperatorCollapser.java b/dice-lang/src/bjc/dicelang/v1/ast/IOperatorCollapser.java
index bd120a8..ab097db 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/IOperatorCollapser.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/IOperatorCollapser.java
@@ -1,15 +1,15 @@
package bjc.dicelang.v1.ast;
-import java.util.function.Function;
-
import bjc.dicelang.v1.ast.nodes.IDiceASTNode;
import bjc.utils.data.IPair;
import bjc.utils.data.ITree;
import bjc.utils.funcdata.IList;
+import java.util.function.Function;
+
/**
* Alias for operator collapsers. Because 68-char types are too long
- *
+ *
* @author ben
*
*/
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/IResult.java b/dice-lang/src/bjc/dicelang/v1/ast/IResult.java
index 093cfd0..df67061 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/IResult.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/IResult.java
@@ -2,14 +2,14 @@ package bjc.dicelang.v1.ast;
/**
* Represents a result from an expression evaluation
- *
+ *
* @author ben
*
*/
public interface IResult {
/**
* Get the type of this result
- *
+ *
* @return The type of this result
*/
public ResultType getType();
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/IntegerResult.java b/dice-lang/src/bjc/dicelang/v1/ast/IntegerResult.java
index b365282..61d57b7 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/IntegerResult.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/IntegerResult.java
@@ -2,7 +2,7 @@ package bjc.dicelang.v1.ast;
/**
* Represents a integer-valued result
- *
+ *
* @author ben
*
*/
@@ -11,7 +11,7 @@ public class IntegerResult implements IResult {
/**
* Create a new integer valued result
- *
+ *
* @param val
* The value of the result
*/
@@ -26,7 +26,7 @@ public class IntegerResult implements IResult {
/**
* Get the value of this result
- *
+ *
* @return The value of this result
*/
public int getValue() {
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/ResultType.java b/dice-lang/src/bjc/dicelang/v1/ast/ResultType.java
index 73616ee..c5afee8 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/ResultType.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/ResultType.java
@@ -2,7 +2,7 @@ package bjc.dicelang.v1.ast;
/**
* Represents the result of a computation
- *
+ *
* @author ben
*
*/
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceASTType.java b/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceASTType.java
index ea85b2a..47e8b39 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceASTType.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceASTType.java
@@ -2,7 +2,7 @@ package bjc.dicelang.v1.ast.nodes;
/**
* An enum to represent the type of node an AST node is
- *
+ *
* @author ben
*
*/
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 bb979d1..4241463 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceLiteralNode.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceLiteralNode.java
@@ -4,7 +4,7 @@ import bjc.dicelang.v1.IDiceExpression;
/**
* Represents a literal backed by a dice expression
- *
+ *
* @author ben
*
*/
@@ -13,7 +13,7 @@ public class DiceLiteralNode implements ILiteralDiceNode {
/**
* Create a new literal from an expression
- *
+ *
* @param exp
* The expression to attempt to create a literal from
*/
@@ -33,7 +33,7 @@ public class DiceLiteralNode implements ILiteralDiceNode {
/**
* Return the expression being represented
- *
+ *
* @return The expression being represented
*/
public IDiceExpression getValue() {
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceLiteralType.java b/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceLiteralType.java
index 9440f85..d48104c 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceLiteralType.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceLiteralType.java
@@ -2,7 +2,7 @@ package bjc.dicelang.v1.ast.nodes;
/**
* Represents the type of literals that can be in an AST
- *
+ *
* @author ben
*
*/
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 a5a79a6..b842604 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceOperatorType.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceOperatorType.java
@@ -2,10 +2,10 @@ 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
- *
+ *
* @author ben
*
*/
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/nodes/IDiceASTNode.java b/dice-lang/src/bjc/dicelang/v1/ast/nodes/IDiceASTNode.java
index 219cf4a..7e8bb81 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/nodes/IDiceASTNode.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/nodes/IDiceASTNode.java
@@ -2,21 +2,21 @@ package bjc.dicelang.v1.ast.nodes;
/**
* The interface for a node in a dice AST
- *
+ *
* @author ben
*
*/
public interface IDiceASTNode {
/**
* Get the type of AST node this node is
- *
+ *
* @return The type of AST node this AST node is
*/
public DiceASTType getType();
/**
* Check if this node represents an operator or not
- *
+ *
* @return Whether or not this node represents an operator
*/
public boolean isOperator();
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 ece528b..5a3f5b3 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/nodes/ILiteralDiceNode.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/nodes/ILiteralDiceNode.java
@@ -2,14 +2,14 @@ package bjc.dicelang.v1.ast.nodes;
/**
* Represents a literal of some type in the AST
- *
+ *
* @author ben
*
*/
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
@@ -19,18 +19,15 @@ public interface ILiteralDiceNode extends IDiceASTNode {
String diceGroupOrNumber = "[(?:" + diceGroup + ")(?:\\d+)]";
- if (tok.matches("\\A" + diceGroupOrNumber + "?" + "c" + diceGroupOrNumber + "\\Z")) {
+ if(tok.matches("\\A" + diceGroupOrNumber + "?" + "c" + diceGroupOrNumber + "\\Z"))
return DiceLiteralType.DICE;
- }
- if (tok.matches("\\A" + diceGroup + "Z")) {
- return DiceLiteralType.DICE;
- }
+ if(tok.matches("\\A" + diceGroup + "Z")) return DiceLiteralType.DICE;
try {
Integer.parseInt(tok);
return DiceLiteralType.INTEGER;
- } catch (NumberFormatException nfex) {
+ } catch(NumberFormatException nfex) {
// We don't care about details
// This probably shouldn't return null, but I believe it
// does so
@@ -41,7 +38,7 @@ public interface ILiteralDiceNode extends IDiceASTNode {
/**
* Check if this node can be optimized to a constant
- *
+ *
* @return Whether or not this node can be optimized to a constant
* @see bjc.dicelang.v1.IDiceExpression#canOptimize()
*/
@@ -49,7 +46,7 @@ public interface ILiteralDiceNode extends IDiceASTNode {
/**
* Get the type of literal this node represents
- *
+ *
* @return The type of literal this node represents
*/
DiceLiteralType getLiteralType();
@@ -66,7 +63,7 @@ public interface ILiteralDiceNode extends IDiceASTNode {
/**
* Optimize this node to a constant if possible
- *
+ *
* @return This node in constant form if possible
* @see bjc.dicelang.v1.IDiceExpression#optimize()
*/
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 1c8aa56..fd1a1e6 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/nodes/IntegerLiteralNode.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/nodes/IntegerLiteralNode.java
@@ -2,7 +2,7 @@ package bjc.dicelang.v1.ast.nodes;
/**
* Represents an integer literal of some kind
- *
+ *
* @author ben
*
*/
@@ -11,7 +11,7 @@ public class IntegerLiteralNode implements ILiteralDiceNode {
/**
* Create a new integer literal from the given number
- *
+ *
* @param val
* The value this node represents
*/
@@ -31,7 +31,7 @@ public class IntegerLiteralNode implements ILiteralDiceNode {
/**
* Get the value this node represents
- *
+ *
* @return The integer value of this node
*/
public int getValue() {
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 0af9d81..0181314 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/nodes/OperatorDiceNode.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/nodes/OperatorDiceNode.java
@@ -6,7 +6,7 @@ import static bjc.dicelang.v1.ast.nodes.DiceOperatorType.MATH;
/**
* A node that represents an operator
- *
+ *
* @author ben
*
*/
@@ -50,13 +50,13 @@ public enum OperatorDiceNode implements IDiceASTNode {
/**
* Create a operator node from a string
- *
+ *
* @param s
* The string to convert to a node
* @return The operator corresponding to the node
*/
public static OperatorDiceNode fromString(String s) {
- switch (s) {
+ switch(s) {
case ":=":
return ASSIGN;
case "+":
@@ -84,7 +84,7 @@ public enum OperatorDiceNode implements IDiceASTNode {
/**
* Represents the group of operator this operator is sorted into.
- *
+ *
*/
public final DiceOperatorType type;
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 22ddf17..e44f2ab 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/nodes/VariableDiceNode.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/nodes/VariableDiceNode.java
@@ -2,7 +2,7 @@ package bjc.dicelang.v1.ast.nodes;
/**
* A node that represents a reference to a variable
- *
+ *
* @author ben
*
*/
@@ -14,7 +14,7 @@ public class VariableDiceNode implements IDiceASTNode {
/**
* Create a new node representing the specified variable
- *
+ *
* @param varName
* The name of the variable being referenced
*/
@@ -24,28 +24,24 @@ public class VariableDiceNode implements IDiceASTNode {
/*
* (non-Javadoc)
- *
+ *
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
// Handle special cases
- if (this == obj) {
+ if(this == obj)
return true;
- } else if (obj == null) {
+ else if(obj == null)
return false;
- } else if (getClass() != obj.getClass()) {
+ else if(getClass() != obj.getClass())
return false;
- } else {
+ else {
VariableDiceNode other = (VariableDiceNode) obj;
- if (variableName == null) {
- if (other.variableName != null) {
- return false;
- }
- } else if (!variableName.equals(other.variableName)) {
- return false;
- }
+ if(variableName == null) {
+ if(other.variableName != null) return false;
+ } else if(!variableName.equals(other.variableName)) return false;
return true;
}
@@ -58,7 +54,7 @@ public class VariableDiceNode implements IDiceASTNode {
/**
* Get the variable referenced by this AST node
- *
+ *
* @return the variable referenced by this AST node
*/
public String getVariable() {
@@ -67,20 +63,20 @@ public class VariableDiceNode implements IDiceASTNode {
/*
* (non-Javadoc)
- *
+ *
* @see java.lang.Object#hashCode()
*/
@Override
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;
}
/*
* (non-Javadoc)
- *
+ *
* @see bjc.utils.dice.ast.IDiceASTNode#isOperator()
*/
@Override
@@ -90,7 +86,7 @@ public class VariableDiceNode implements IDiceASTNode {
/*
* (non-Javadoc)
- *
+ *
* @see java.lang.Object#toString()
*/
@Override
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/nodes/package-info.java b/dice-lang/src/bjc/dicelang/v1/ast/nodes/package-info.java
index 098157d..cdd63e9 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/nodes/package-info.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/nodes/package-info.java
@@ -1,6 +1,6 @@
/**
* Classes for nodes in the dice-lang AST
- *
+ *
* @author ben
*
*/
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 9fb0a5e..acc1afa 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/optimization/ArithmeticCollapser.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/optimization/ArithmeticCollapser.java
@@ -1,7 +1,5 @@
package bjc.dicelang.v1.ast.optimization;
-import java.util.function.BinaryOperator;
-
import bjc.dicelang.v1.ast.DiceASTUtils;
import bjc.dicelang.v1.ast.nodes.DiceASTType;
import bjc.dicelang.v1.ast.nodes.IDiceASTNode;
@@ -9,12 +7,14 @@ import bjc.dicelang.v1.ast.nodes.ILiteralDiceNode;
import bjc.dicelang.v1.ast.nodes.IntegerLiteralNode;
import bjc.dicelang.v1.ast.nodes.OperatorDiceNode;
import bjc.utils.data.ITree;
-import bjc.utils.funcdata.IList;
import bjc.utils.data.Tree;
+import bjc.utils.funcdata.IList;
+
+import java.util.function.BinaryOperator;
class ArithmeticCollapser {
- private BinaryOperator<Integer> reducer;
- private OperatorDiceNode type;
+ private BinaryOperator<Integer> reducer;
+ private OperatorDiceNode type;
public ArithmeticCollapser(BinaryOperator<Integer> reducr, OperatorDiceNode typ) {
reducer = reducr;
@@ -24,17 +24,14 @@ class ArithmeticCollapser {
public ITree<IDiceASTNode> collapse(IList<ITree<IDiceASTNode>> children) {
boolean allConstant = children.allMatch((subtree) -> {
return subtree.transformHead((node) -> {
- if (node.getType() == DiceASTType.LITERAL) {
+ if(node.getType() == DiceASTType.LITERAL)
return ((ILiteralDiceNode) node).canOptimize();
- }
return false;
});
});
- if (!allConstant) {
- return new Tree<>(type, children);
- }
+ if(!allConstant) return new Tree<>(type, children);
int initState = DiceASTUtils.literalToInteger(children.first());
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 a0daf31..bf44953 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/optimization/ConstantCollapser.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/optimization/ConstantCollapser.java
@@ -6,12 +6,12 @@ import bjc.dicelang.v1.ast.nodes.IDiceASTNode;
import bjc.dicelang.v1.ast.nodes.IntegerLiteralNode;
import bjc.dicelang.v1.ast.nodes.OperatorDiceNode;
import bjc.utils.data.ITree;
-import bjc.utils.funcdata.IList;
import bjc.utils.data.Tree;
+import bjc.utils.funcdata.IList;
/**
* Collapses operations with constants to constants
- *
+ *
* @author ben
*
*/
@@ -40,11 +40,9 @@ public class ConstantCollapser implements IOptimizationPass {
@Override
public ITree<IDiceASTNode> optimizeOperator(IDiceASTNode operator, IList<ITree<IDiceASTNode>> children) {
- if (!operator.isOperator()) {
- return new Tree<>(operator, children);
- }
+ if(!operator.isOperator()) return new Tree<>(operator, children);
- switch ((OperatorDiceNode) operator) {
+ switch((OperatorDiceNode) operator) {
case ADD:
return additionCollapser.collapse(children);
case DIVIDE:
@@ -56,22 +54,16 @@ public class ConstantCollapser implements IOptimizationPass {
case COMPOUND:
return compoundCollapser.collapse(children);
case GROUP:
- if (children.getSize() != 2) {
- return new Tree<>(operator, children);
- }
+ if(children.getSize() != 2) return new Tree<>(operator, children);
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);
- }
+ if(children.getSize() != 1) return new Tree<>(operator, children);
return children.first();
case ASSIGN:
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 b09d95d..082f042 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/optimization/IOptimizationPass.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/optimization/IOptimizationPass.java
@@ -6,14 +6,14 @@ import bjc.utils.funcdata.IList;
/**
* Represents a pass of optimizations over a dice AST
- *
+ *
* @author ben
*
*/
public interface IOptimizationPass {
/**
* Optimize a leaf in the tree
- *
+ *
* @param leafNode
* The node to optimize
* @return The optimized node
@@ -22,7 +22,7 @@ public interface IOptimizationPass {
/**
* Optimize an operator in an AST node
- *
+ *
* @param operator
* The operator being optimized
* @param 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 f00d390..7ce6f5d 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/optimization/OperationCondenser.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/optimization/OperationCondenser.java
@@ -11,14 +11,14 @@ import bjc.utils.data.Tree;
/**
* Condenses chained operations into a single level
- *
+ *
* @author ben
*
*/
public class OperationCondenser {
/**
* Condense chained similiar operations into a single level
- *
+ *
* @param ast
* The AST to condense
* @return The condensed AST
@@ -33,12 +33,10 @@ public class OperationCondenser {
IHolder<Boolean> canCondense = new Identity<>(true);
ast.doForChildren((child) -> {
- if (canCondense.getValue()) {
+ if(canCondense.getValue()) {
canCondense.replace(child.transformHead((node) -> {
- if (node.getType() == DiceASTType.OPERATOR) {
- if (operation.equals(node)) {
- return true;
- }
+ if(node.getType() == DiceASTType.OPERATOR) {
+ if(operation.equals(node)) return true;
return false;
}
@@ -48,14 +46,12 @@ public class OperationCondenser {
}
});
- if (!canCondense.getValue()) {
- return ast;
- }
+ if(!canCondense.getValue()) return ast;
ITree<IDiceASTNode> condensedAST = new Tree<>(operation);
ast.doForChildren((child) -> {
- if (child.getHead().getType() == DiceASTType.OPERATOR) {
+ if(child.getHead().getType() == DiceASTType.OPERATOR) {
child.doForChildren((subChild) -> {
condensedAST.addChild(subChild);
});
@@ -68,7 +64,7 @@ public class OperationCondenser {
}
private static TopDownTransformResult pickNode(IDiceASTNode node) {
- switch (node.getType()) {
+ switch(node.getType()) {
case LITERAL:
return TopDownTransformResult.SKIP;
case OPERATOR:
@@ -81,7 +77,7 @@ public class OperationCondenser {
}
private static TopDownTransformResult pickOperator(OperatorDiceNode node) {
- switch (node) {
+ switch(node) {
case ADD:
case MULTIPLY:
case SUBTRACT:
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/optimization/package-info.java b/dice-lang/src/bjc/dicelang/v1/ast/optimization/package-info.java
index 04727d7..3c74a21 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/optimization/package-info.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/optimization/package-info.java
@@ -1,6 +1,6 @@
/**
* Contains optimizations on dice ASTs
- *
+ *
* @author ben
*
*/
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/package-info.java b/dice-lang/src/bjc/dicelang/v1/ast/package-info.java
index 4c79a45..f1de4f7 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/package-info.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/package-info.java
@@ -1,6 +1,6 @@
/**
* New implementation of AST for dice-lang
- *
+ *
* @author ben
*
*/