summaryrefslogtreecommitdiff
path: root/dice-lang/src/bjc/dicelang/v1
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
parente59e2a97773f93bdd25bd4680809c10699f0feb3 (diff)
Cleanup
Diffstat (limited to 'dice-lang/src/bjc/dicelang/v1')
-rw-r--r--dice-lang/src/bjc/dicelang/v1/BindingDiceExpression.java20
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ComplexDice.java37
-rw-r--r--dice-lang/src/bjc/dicelang/v1/CompoundDice.java16
-rw-r--r--dice-lang/src/bjc/dicelang/v1/DiceExpressionBuilder.java24
-rw-r--r--dice-lang/src/bjc/dicelang/v1/DiceExpressionParser.java24
-rw-r--r--dice-lang/src/bjc/dicelang/v1/DiceExpressionType.java2
-rw-r--r--dice-lang/src/bjc/dicelang/v1/Die.java12
-rw-r--r--dice-lang/src/bjc/dicelang/v1/IDiceExpression.java28
-rw-r--r--dice-lang/src/bjc/dicelang/v1/OperatorDiceExpression.java8
-rw-r--r--dice-lang/src/bjc/dicelang/v1/PolyhedralDice.java30
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ReferenceDiceExpression.java13
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ScalarDie.java4
-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
-rw-r--r--dice-lang/src/bjc/dicelang/v1/examples/DiceASTLanguageTest.java20
-rw-r--r--dice-lang/src/bjc/dicelang/v1/examples/DiceASTPragma.java8
-rw-r--r--dice-lang/src/bjc/dicelang/v1/examples/DiceExpressionParserTest.java12
-rw-r--r--dice-lang/src/bjc/dicelang/v1/examples/DiceExpressionPreparer.java16
-rw-r--r--dice-lang/src/bjc/dicelang/v1/examples/DiceLanguageState.java8
-rw-r--r--dice-lang/src/bjc/dicelang/v1/examples/DiceLanguageTest.java16
48 files changed, 326 insertions, 405 deletions
diff --git a/dice-lang/src/bjc/dicelang/v1/BindingDiceExpression.java b/dice-lang/src/bjc/dicelang/v1/BindingDiceExpression.java
index cce31a2..14dbc42 100644
--- a/dice-lang/src/bjc/dicelang/v1/BindingDiceExpression.java
+++ b/dice-lang/src/bjc/dicelang/v1/BindingDiceExpression.java
@@ -5,7 +5,7 @@ import java.util.Map;
/**
* A variable expression that represents binding a variable to a name in an
* enviroment
- *
+ *
* @author ben
*
*/
@@ -23,7 +23,7 @@ public class BindingDiceExpression implements IDiceExpression {
/**
* Create a new dice expression binder from two expressions and an
* enviroment
- *
+ *
* @param left
* The left side expression to get a name from. Must be a
* ReferenceDiceExpression
@@ -34,12 +34,10 @@ public class BindingDiceExpression implements IDiceExpression {
*/
public BindingDiceExpression(IDiceExpression left, IDiceExpression right,
Map<String, IDiceExpression> enviroment) {
- if (!(left instanceof ReferenceDiceExpression)) {
- throw new UnsupportedOperationException(
- "Error: Binding an expression to something that is not a variable reference,"
- + " or array thereof. is unsupported."
- + " Problematic expression is " + left);
- }
+ if(!(left instanceof ReferenceDiceExpression)) throw new UnsupportedOperationException(
+ "Error: Binding an expression to something that is not a variable reference,"
+ + " or array thereof. is unsupported." + " Problematic expression is "
+ + left);
String varName = ((ReferenceDiceExpression) left).getName();
@@ -48,7 +46,7 @@ public class BindingDiceExpression implements IDiceExpression {
/**
* Create a new dice expression binder
- *
+ *
* @param name
* The name of the variable to bind
* @param expression
@@ -69,7 +67,7 @@ public class BindingDiceExpression implements IDiceExpression {
/*
* (non-Javadoc)
- *
+ *
* @see bjc.utils.dice.IDiceExpression#roll()
*/
@Override
@@ -79,7 +77,7 @@ public class BindingDiceExpression implements IDiceExpression {
/*
* (non-Javadoc)
- *
+ *
* @see java.lang.Object#toString()
*/
@Override
diff --git a/dice-lang/src/bjc/dicelang/v1/ComplexDice.java b/dice-lang/src/bjc/dicelang/v1/ComplexDice.java
index b9796f9..76a34ba 100644
--- a/dice-lang/src/bjc/dicelang/v1/ComplexDice.java
+++ b/dice-lang/src/bjc/dicelang/v1/ComplexDice.java
@@ -3,14 +3,14 @@ package bjc.dicelang.v1;
/**
* Implements a collection of one or more of a particular die, where the number
* of dice in the group is variable.
- *
+ *
* @author ben
*
*/
public class ComplexDice implements IDiceExpression {
/**
* Create a dice from a string expression
- *
+ *
* @param expression
* The string to parse the dice from
* @return A dice group parsed from the string
@@ -18,9 +18,7 @@ public class ComplexDice implements IDiceExpression {
public static IDiceExpression fromString(String expression) {
// Handle the case where someone passes us a simple expression
// containing a single die
- if (!expression.contains("d")) {
- return new Die(Integer.parseInt(expression));
- }
+ if(!expression.contains("d")) return new Die(Integer.parseInt(expression));
// Split it on the dice type marker
@@ -30,7 +28,7 @@ public class ComplexDice implements IDiceExpression {
// Create the actual group of dice
return new ComplexDice(new ScalarDie(Integer.parseInt(strangs[0])),
new Die(Integer.parseInt(strangs[1])));
- } catch (NumberFormatException nfex) {
+ } catch(NumberFormatException nfex) {
// We don't care about details
// Tell the user the expression is invalid
@@ -52,7 +50,7 @@ public class ComplexDice implements IDiceExpression {
/**
* Create a new collection of dice
- *
+ *
* @param nDce
* The number of dice in the collection
* @param de
@@ -65,7 +63,7 @@ public class ComplexDice implements IDiceExpression {
/**
* Create a new collection of dice
- *
+ *
* @param nSides
* The number of dice in the collection
* @param de
@@ -80,20 +78,15 @@ public class ComplexDice implements IDiceExpression {
public boolean canOptimize() {
// Can only optimize this dice group if both components can be
// optimized and the die itself has only one value
- if (nDice.canOptimize() && die.canOptimize()) {
- return die.optimize() == 1;
- }
+ if(nDice.canOptimize() && die.canOptimize()) return die.optimize() == 1;
return false;
}
@Override
public int optimize() {
- if (!canOptimize()) {
- throw new UnsupportedOperationException("This complex dice cannot be optimized. "
- + "Both the dice to be rolled and the number of"
- + " dice must be optimizable.");
- }
+ if(!canOptimize()) throw new UnsupportedOperationException("This complex dice cannot be optimized. "
+ + "Both the dice to be rolled and the number of" + " dice must be optimizable.");
return nDice.optimize();
}
@@ -107,13 +100,11 @@ public class ComplexDice implements IDiceExpression {
*/
int nRoll = nDice.roll();
- if (nRoll < 0) {
- throw new UnsupportedOperationException("Attempted to roll a negative number of dice. "
- + "The problematic expression is " + nDice);
- }
+ if(nRoll < 0) throw new UnsupportedOperationException("Attempted to roll a negative number of dice. "
+ + "The problematic expression is " + nDice);
// Roll all the dice and combine them
- for (int i = 0; i < nRoll; i++) {
+ for(int i = 0; i < nRoll; i++) {
res += die.roll();
}
@@ -123,9 +114,7 @@ public class ComplexDice implements IDiceExpression {
@Override
public String toString() {
// Print simple dice groups in a much clearer manner
- if (nDice instanceof ScalarDie && die instanceof Die) {
- return nDice.toString() + die.toString();
- }
+ if(nDice instanceof ScalarDie && die instanceof Die) return nDice.toString() + die.toString();
return "complex[n=" + nDice.toString() + ", d=" + die.toString() + "]";
}
diff --git a/dice-lang/src/bjc/dicelang/v1/CompoundDice.java b/dice-lang/src/bjc/dicelang/v1/CompoundDice.java
index a70ae39..43d64e8 100644
--- a/dice-lang/src/bjc/dicelang/v1/CompoundDice.java
+++ b/dice-lang/src/bjc/dicelang/v1/CompoundDice.java
@@ -2,10 +2,10 @@ package bjc.dicelang.v1;
/**
* Implements a "compound dice"
- *
+ *
* To explain, a compound dice is something like a d100 composed from two d10s
* instead of a hundred sided die.
- *
+ *
* @author ben
*
*/
@@ -22,7 +22,7 @@ public class CompoundDice implements IDiceExpression {
/**
* Create a new compound dice using the specified dice
- *
+ *
* @param lft
* The die to use on the left
* @param rght
@@ -35,7 +35,7 @@ public class CompoundDice implements IDiceExpression {
/**
* Create a new compound dice from two dice strings
- *
+ *
* @param lft
* The left side dice as a string
* @param rght
@@ -47,7 +47,7 @@ public class CompoundDice implements IDiceExpression {
/**
* Create a new compound dice from an array of dice strings
- *
+ *
* @param exps
* An array of two dice strings
*/
@@ -62,10 +62,8 @@ public class CompoundDice implements IDiceExpression {
@Override
public int optimize() {
- if (!canOptimize()) {
- throw new UnsupportedOperationException("Cannot optimize this compound dice. "
- + "Both component dice must be optimizable" + " to optimize a compound dice");
- }
+ if(!canOptimize()) throw new UnsupportedOperationException("Cannot optimize this compound dice. "
+ + "Both component dice must be optimizable" + " to optimize a compound dice");
return Integer.parseInt(left.optimize() + "" + right.optimize());
}
diff --git a/dice-lang/src/bjc/dicelang/v1/DiceExpressionBuilder.java b/dice-lang/src/bjc/dicelang/v1/DiceExpressionBuilder.java
index e0c62c1..3b20d03 100644
--- a/dice-lang/src/bjc/dicelang/v1/DiceExpressionBuilder.java
+++ b/dice-lang/src/bjc/dicelang/v1/DiceExpressionBuilder.java
@@ -7,7 +7,7 @@ import static bjc.dicelang.v1.DiceExpressionType.SUBTRACT;
/**
* Build a dice expression piece by piece
- *
+ *
* @author ben
*
*/
@@ -19,7 +19,7 @@ public class DiceExpressionBuilder {
/**
* Build a dice expression from a seed dice expression
- *
+ *
* @param seed
* The dice expression to use as a seed
*/
@@ -29,7 +29,7 @@ public class DiceExpressionBuilder {
/**
* Build a dice expression from a seed dice
- *
+ *
* @param nSides
* The number of sides in the dice
* @param nDice
@@ -41,7 +41,7 @@ public class DiceExpressionBuilder {
/**
* Add a term to this dice expression
- *
+ *
* @param exp
* The expression to use on the left
* @return A new expression adding the two dice
@@ -53,7 +53,7 @@ public class DiceExpressionBuilder {
/**
* Add a scalar to this dice
- *
+ *
* @param num
* The scalar to add to the dice
* @return A dice expression adding a scalar to this
@@ -65,7 +65,7 @@ public class DiceExpressionBuilder {
/**
* Bake the expression being built to completion
- *
+ *
* @return A usable dice expression
*/
public IDiceExpression bake() {
@@ -74,7 +74,7 @@ public class DiceExpressionBuilder {
/**
* Divide a term from dice expression
- *
+ *
* @param exp
* The expression to use on the left
* @return A new expression dividing the two dice
@@ -86,7 +86,7 @@ public class DiceExpressionBuilder {
/**
* Divide a scalar from this dice
- *
+ *
* @param num
* The scalar to add to the dice
* @return A dice expression dividing a scalar from this
@@ -98,7 +98,7 @@ public class DiceExpressionBuilder {
/**
* Multiply a term by this dice expression
- *
+ *
* @param exp
* The expression to use on the left
* @return A new expression multiplying the two dice
@@ -110,7 +110,7 @@ public class DiceExpressionBuilder {
/**
* Multiply a scalar by this dice
- *
+ *
* @param num
* The scalar to multiply to the dice
* @return A dice expression multiplying a scalar to this
@@ -122,7 +122,7 @@ public class DiceExpressionBuilder {
/**
* Add a term to this dice expression
- *
+ *
* @param exp
* The expression to use on the left
* @return A new expression adding the two dice
@@ -134,7 +134,7 @@ public class DiceExpressionBuilder {
/**
* Add a scalar to this dice
- *
+ *
* @param num
* The scalar to add to the dice
* @return A dice expression adding a scalar to this
diff --git a/dice-lang/src/bjc/dicelang/v1/DiceExpressionParser.java b/dice-lang/src/bjc/dicelang/v1/DiceExpressionParser.java
index 38a842e..ff2e800 100644
--- a/dice-lang/src/bjc/dicelang/v1/DiceExpressionParser.java
+++ b/dice-lang/src/bjc/dicelang/v1/DiceExpressionParser.java
@@ -1,24 +1,24 @@
package bjc.dicelang.v1;
-import java.util.Map;
-import java.util.Stack;
-
import bjc.utils.funcdata.FunctionalStringTokenizer;
import bjc.utils.funcdata.IList;
import bjc.utils.parserutils.ShuntingYard;
+import java.util.Map;
+import java.util.Stack;
+
import org.apache.commons.lang3.StringUtils;
/**
* Parse a dice expression from a string
- *
+ *
* @author ben
*
*/
public class DiceExpressionParser {
/**
* Parse a dice expression from a string
- *
+ *
* @param expression
* The string to parse an expression from
* @param enviroment
@@ -65,12 +65,12 @@ public class DiceExpressionParser {
/*
* Handle compound dice
*/
- if (StringUtils.countMatches(expressionPart, 'c') == 1
+ if(StringUtils.countMatches(expressionPart, 'c') == 1
&& !expressionPart.equalsIgnoreCase("c")) {
String[] strangs = expressionPart.split("c");
expressions.push(new CompoundDice(strangs));
- } else if (StringUtils.countMatches(expressionPart, 'd') == 1
+ } else if(StringUtils.countMatches(expressionPart, 'd') == 1
&& !expressionPart.equalsIgnoreCase("d")) {
/*
* Handle dice groups
@@ -82,10 +82,10 @@ public class DiceExpressionParser {
* Handle scalar numbers
*/
expressions.push(new ScalarDie(Integer.parseInt(expressionPart)));
- } catch (NumberFormatException nfex) {
+ } catch(NumberFormatException nfex) {
// We don't care about details, just
// that it failed
- if (expressions.size() >= 2) {
+ if(expressions.size() >= 2) {
/*
* Apply an operation to two
* dice
@@ -93,7 +93,7 @@ public class DiceExpressionParser {
IDiceExpression right = expressions.pop();
IDiceExpression left = expressions.pop();
- switch (expressionPart) {
+ switch(expressionPart) {
case ":=":
expressions.push(new BindingDiceExpression(left, right,
enviroment));
@@ -124,7 +124,7 @@ public class DiceExpressionParser {
/*
* Parse it as a
* variable reference
- *
+ *
* Make sure to restore
* popped variables
*/
@@ -146,7 +146,7 @@ public class DiceExpressionParser {
}
});
- if (expressions.size() != 1) {
+ if(expressions.size() != 1) {
System.err.println(
"WARNING: Leftovers found on dice expression stack. Remember, := is assignment.");
}
diff --git a/dice-lang/src/bjc/dicelang/v1/DiceExpressionType.java b/dice-lang/src/bjc/dicelang/v1/DiceExpressionType.java
index 8ffe78d..7c0510f 100644
--- a/dice-lang/src/bjc/dicelang/v1/DiceExpressionType.java
+++ b/dice-lang/src/bjc/dicelang/v1/DiceExpressionType.java
@@ -26,7 +26,7 @@ public enum DiceExpressionType {
@Override
public String toString() {
- switch (this) {
+ switch(this) {
case ADD:
return "+";
case DIVIDE:
diff --git a/dice-lang/src/bjc/dicelang/v1/Die.java b/dice-lang/src/bjc/dicelang/v1/Die.java
index ffb6093..667e18c 100644
--- a/dice-lang/src/bjc/dicelang/v1/Die.java
+++ b/dice-lang/src/bjc/dicelang/v1/Die.java
@@ -4,7 +4,7 @@ import java.util.Random;
/**
* A single polyhedral dice
- *
+ *
* @author ben
*
*/
@@ -21,14 +21,12 @@ public class Die implements IDiceExpression {
/**
* Create a die with the specified number of sides
- *
+ *
* @param nSides
* The number of sides this dice has
*/
public Die(int nSides) {
- if (nSides < 1) {
- throw new UnsupportedOperationException("Dice with less than 1 side are not supported");
- }
+ if(nSides < 1) throw new UnsupportedOperationException("Dice with less than 1 side are not supported");
this.nSides = nSides;
}
@@ -40,9 +38,7 @@ public class Die implements IDiceExpression {
@Override
public int optimize() {
- if (nSides != 1) {
- throw new UnsupportedOperationException("Can't optimize " + nSides + "-sided dice");
- }
+ if(nSides != 1) throw new UnsupportedOperationException("Can't optimize " + nSides + "-sided dice");
return 1;
}
diff --git a/dice-lang/src/bjc/dicelang/v1/IDiceExpression.java b/dice-lang/src/bjc/dicelang/v1/IDiceExpression.java
index 86c4d6a..5bf062b 100644
--- a/dice-lang/src/bjc/dicelang/v1/IDiceExpression.java
+++ b/dice-lang/src/bjc/dicelang/v1/IDiceExpression.java
@@ -4,7 +4,7 @@ import bjc.utils.funcutils.StringUtils;
/**
* An expression for something that can be rolled like a polyhedral die
- *
+ *
* @author ben
*
*/
@@ -12,7 +12,7 @@ import bjc.utils.funcutils.StringUtils;
public interface IDiceExpression {
/**
* Parse a string into an expression.
- *
+ *
* It can accept the following types of expressions
* <ul>
* <li>Simple integers - '2'</li>
@@ -21,13 +21,13 @@ public interface IDiceExpression {
* <li>Number concatenation - '2c6'</li>
* <li>Dice concatenation - '1d10c1d10</li>
* </ul>
- *
+ *
* Dice concatenation is like using 2 d10s to emulate a d100, so instead
* of adding them, it reads them side by side.
- *
+ *
* @param expression
* The string to convert to an expression
- *
+ *
* @return The string, converted into expression form
*/
static IDiceExpression toExpression(String expression) {
@@ -35,22 +35,22 @@ public interface IDiceExpression {
String diceMatcher = "\\Ad\\d+\\Z";
- if (StringUtils.containsInfixOperator(literalData, "c")) {
+ if(StringUtils.containsInfixOperator(literalData, "c")) {
// Parse a compound die
String[] strangs = literalData.split("c");
return new CompoundDice(strangs);
- } else if (StringUtils.containsInfixOperator(literalData, "d")) {
+ } else if(StringUtils.containsInfixOperator(literalData, "d"))
// Handle groups of similiar dice
return ComplexDice.fromString(literalData);
- } else if (literalData.matches(diceMatcher)) {
+ else if(literalData.matches(diceMatcher))
// Handle people who put 'd6' instead of '1d6'
return new Die(Integer.parseInt(literalData.substring(1)));
- } else {
+ else {
// Parse a scalar number
try {
return new ScalarDie(Integer.parseInt(literalData));
- } catch (NumberFormatException nfex) {
+ } catch(NumberFormatException nfex) {
UnsupportedOperationException usex = new UnsupportedOperationException(
"Found malformed leaf token " + expression + ". Floating point numbers "
+ "are not supported.");
@@ -64,7 +64,7 @@ public interface IDiceExpression {
/**
* Check if this expression can be optimized to a scalar value
- *
+ *
* @return Whether or not this expression can be optimized to a scalar
* value
*/
@@ -74,9 +74,9 @@ public interface IDiceExpression {
/**
* Optimize this expression to a scalar value
- *
+ *
* @return This expression, optimized to a scalar value
- *
+ *
* @throws UnsupportedOperationException
* if this type of expression can't be optimized
*/
@@ -86,7 +86,7 @@ public interface IDiceExpression {
/**
* Roll the dice once
- *
+ *
* @return The result of rowing the dice
*/
public int roll();
diff --git a/dice-lang/src/bjc/dicelang/v1/OperatorDiceExpression.java b/dice-lang/src/bjc/dicelang/v1/OperatorDiceExpression.java
index bf9254e..a5d37e2 100644
--- a/dice-lang/src/bjc/dicelang/v1/OperatorDiceExpression.java
+++ b/dice-lang/src/bjc/dicelang/v1/OperatorDiceExpression.java
@@ -2,7 +2,7 @@ package bjc.dicelang.v1;
/**
* Implements a class for combining two dice with an operator
- *
+ *
* @author ben
*
*/
@@ -24,7 +24,7 @@ public class OperatorDiceExpression implements IDiceExpression {
/**
* Create a new compound expression using the specified parameters
- *
+ *
* @param rght
* The die on the right side of the expression
* @param lft
@@ -43,7 +43,7 @@ public class OperatorDiceExpression implements IDiceExpression {
/*
* Handle each operator
*/
- switch (type) {
+ switch(type) {
case ADD:
return right.roll() + left.roll();
case SUBTRACT:
@@ -58,7 +58,7 @@ public class OperatorDiceExpression implements IDiceExpression {
*/
try {
return right.roll() / left.roll();
- } catch (ArithmeticException aex) {
+ } catch(ArithmeticException aex) {
UnsupportedOperationException usex = new UnsupportedOperationException(
"Attempted to divide by zero." + " Problematic expression is " + left);
diff --git a/dice-lang/src/bjc/dicelang/v1/PolyhedralDice.java b/dice-lang/src/bjc/dicelang/v1/PolyhedralDice.java
index e20a12a..402de18 100644
--- a/dice-lang/src/bjc/dicelang/v1/PolyhedralDice.java
+++ b/dice-lang/src/bjc/dicelang/v1/PolyhedralDice.java
@@ -2,14 +2,14 @@ package bjc.dicelang.v1;
/**
* Utility class that produces common polyhedral dice
- *
+ *
* @author ben
*
*/
public class PolyhedralDice {
/**
* Produce a single d10
- *
+ *
* @return A single d10
*/
public static IDiceExpression d10() {
@@ -18,7 +18,7 @@ public class PolyhedralDice {
/**
* Produce the specified number of 10-sided dice
- *
+ *
* @param nDice
* The number of ten-sided dice to produce
* @return A group of ten-sided dice of the specified size
@@ -29,7 +29,7 @@ public class PolyhedralDice {
/**
* Produce a single d100
- *
+ *
* @return A single d100
*/
public static IDiceExpression d100() {
@@ -38,7 +38,7 @@ public class PolyhedralDice {
/**
* Produce the specified number of 100-sided dice
- *
+ *
* @param nDice
* The number of hundred-sided dice to produce
* @return A group of hundred-sided dice of the specified size
@@ -49,7 +49,7 @@ public class PolyhedralDice {
/**
* Produce a single d12
- *
+ *
* @return A single d12
*/
public static IDiceExpression d12() {
@@ -58,7 +58,7 @@ public class PolyhedralDice {
/**
* Produce the specified number of 12-sided dice
- *
+ *
* @param nDice
* The number of twelve-sided dice to produce
* @return A group of twelve-sided dice of the specified size
@@ -69,7 +69,7 @@ public class PolyhedralDice {
/**
* Produce a single d20
- *
+ *
* @return A single d20
*/
public static IDiceExpression d20() {
@@ -78,7 +78,7 @@ public class PolyhedralDice {
/**
* Produce the specified number of 20-sided dice
- *
+ *
* @param nDice
* The number of twenty-sided dice to produce
* @return A group of twenty-sided dice of the specified size
@@ -89,7 +89,7 @@ public class PolyhedralDice {
/**
* Produce a single d4
- *
+ *
* @return A single d4
*/
public static IDiceExpression d4() {
@@ -98,7 +98,7 @@ public class PolyhedralDice {
/**
* Produce the specified number of 4-sided dice
- *
+ *
* @param nDice
* The number of four-sided dice to produce
* @return A group of four-sided dice of the specified size
@@ -109,7 +109,7 @@ public class PolyhedralDice {
/**
* Produce a single d6
- *
+ *
* @return A single d6
*/
public static IDiceExpression d6() {
@@ -118,7 +118,7 @@ public class PolyhedralDice {
/**
* Produce the specified number of 6-sided dice
- *
+ *
* @param nDice
* The number of six-sided dice to produce
* @return A group of six-sided dice of the specified size
@@ -129,7 +129,7 @@ public class PolyhedralDice {
/**
* Produce a single d8
- *
+ *
* @return A single d8
*/
public static IDiceExpression d8() {
@@ -138,7 +138,7 @@ public class PolyhedralDice {
/**
* Produce the specified number of 8-sided dice
- *
+ *
* @param nDice
* The number of eight-sided dice to produce
* @return A group of eight-sided dice of the specified size
diff --git a/dice-lang/src/bjc/dicelang/v1/ReferenceDiceExpression.java b/dice-lang/src/bjc/dicelang/v1/ReferenceDiceExpression.java
index b3b979a..11f466e 100644
--- a/dice-lang/src/bjc/dicelang/v1/ReferenceDiceExpression.java
+++ b/dice-lang/src/bjc/dicelang/v1/ReferenceDiceExpression.java
@@ -4,7 +4,7 @@ import java.util.Map;
/**
* A dice expression that refers to a variable bound in a mutable enviroment
- *
+ *
* @author ben
*
*/
@@ -22,7 +22,7 @@ public class ReferenceDiceExpression implements IDiceExpression {
/**
* Create a new reference dice expression referring to the given name in
* an enviroment
- *
+ *
* @param nme
* The name of the bound variable
* @param env
@@ -35,7 +35,7 @@ public class ReferenceDiceExpression implements IDiceExpression {
/**
* Get the name of the referenced variable
- *
+ *
* @return the name of the referenced variable
*/
public String getName() {
@@ -44,18 +44,15 @@ public class ReferenceDiceExpression implements IDiceExpression {
@Override
public int roll() {
- if (!enviroment.containsKey(name)) {
+ if(!enviroment.containsKey(name))
throw new UnsupportedOperationException("Attempted to reference undefined variable " + name);
- }
return enviroment.get(name).roll();
}
@Override
public String toString() {
- if (enviroment.containsKey(name)) {
- return enviroment.get(name).toString() + "(bound to " + name + ")";
- }
+ if(enviroment.containsKey(name)) return enviroment.get(name).toString() + "(bound to " + name + ")";
return name + "(unbound)";
}
diff --git a/dice-lang/src/bjc/dicelang/v1/ScalarDie.java b/dice-lang/src/bjc/dicelang/v1/ScalarDie.java
index c0bb3a9..62ca151 100644
--- a/dice-lang/src/bjc/dicelang/v1/ScalarDie.java
+++ b/dice-lang/src/bjc/dicelang/v1/ScalarDie.java
@@ -2,7 +2,7 @@ package bjc.dicelang.v1;
/**
* A die that represents a static number
- *
+ *
* @author ben
*
*/
@@ -14,7 +14,7 @@ public class ScalarDie implements IDiceExpression {
/**
* Create a dice with the specified number
- *
+ *
* @param num
* The number used for the dice
*/
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
*
*/
diff --git a/dice-lang/src/bjc/dicelang/v1/examples/DiceASTLanguageTest.java b/dice-lang/src/bjc/dicelang/v1/examples/DiceASTLanguageTest.java
index f20e276..205d986 100644
--- a/dice-lang/src/bjc/dicelang/v1/examples/DiceASTLanguageTest.java
+++ b/dice-lang/src/bjc/dicelang/v1/examples/DiceASTLanguageTest.java
@@ -1,8 +1,5 @@
package bjc.dicelang.v1.examples;
-import java.util.InputMismatchException;
-import java.util.Scanner;
-
import bjc.dicelang.v1.ast.DiceASTEvaluator;
import bjc.dicelang.v1.ast.DiceASTInliner;
import bjc.dicelang.v1.ast.DiceASTOptimizer;
@@ -18,9 +15,12 @@ import bjc.utils.funcdata.FunctionalStringTokenizer;
import bjc.utils.funcdata.IList;
import bjc.utils.funcdata.IMap;
+import java.util.InputMismatchException;
+import java.util.Scanner;
+
/**
* Test interface for AST-based dice language
- *
+ *
* @author ben
*
*/
@@ -66,7 +66,7 @@ public class DiceASTLanguageTest {
// Get the pragma arguments
IList<String> pragmaArgs = tokenizer.toList();
- if (pragmaArgs.getSize() < 3) {
+ if(pragmaArgs.getSize() < 3) {
// Complain about pragma arguments not being valid
System.err.println("ERROR: Inline requires at least 3 parameters. They are:"
+ "\n\t1. The name of the expression to inline."
@@ -91,7 +91,7 @@ public class DiceASTLanguageTest {
/**
* Main method of class
- *
+ *
* @param args
* Unused CLI args
*/
@@ -107,12 +107,12 @@ public class DiceASTLanguageTest {
IMap<String, ITree<IDiceASTNode>> enviroment = new FunctionalMap<>();
// Handle commands
- while (!currentLine.equalsIgnoreCase("quit")) {
+ while(!currentLine.equalsIgnoreCase("quit")) {
// Get the name of a possible action
String possibleActionName = currentLine.split(" ")[0];
// Check and see if we're executing an action
- if (actions.containsKey(possibleActionName)) {
+ if(actions.containsKey(possibleActionName)) {
// Execute action
FunctionalStringTokenizer tokenizer = new FunctionalStringTokenizer(currentLine);
@@ -146,7 +146,7 @@ public class DiceASTLanguageTest {
System.out.println("Command parsed in "
+ (double) (System.nanoTime() - time) / 1000000000 + " s");
- } catch (InputMismatchException | IllegalStateException | UnsupportedOperationException ex) {
+ } catch(InputMismatchException | IllegalStateException | UnsupportedOperationException ex) {
// Tell the user there was an error in parsing
System.out.println("PARSING ERROR: " + ex.getLocalizedMessage());
@@ -187,7 +187,7 @@ public class DiceASTLanguageTest {
// Update the 'last' meta-variable
enviroment.put("last", transformedAST);
- } catch (UnsupportedOperationException usex) {
+ } catch(UnsupportedOperationException usex) {
// Tell the user there was an error in
// evaluation
System.out.println("EVALUATION ERROR: " + usex.getLocalizedMessage());
diff --git a/dice-lang/src/bjc/dicelang/v1/examples/DiceASTPragma.java b/dice-lang/src/bjc/dicelang/v1/examples/DiceASTPragma.java
index a29b8b5..502dafd 100644
--- a/dice-lang/src/bjc/dicelang/v1/examples/DiceASTPragma.java
+++ b/dice-lang/src/bjc/dicelang/v1/examples/DiceASTPragma.java
@@ -1,18 +1,18 @@
package bjc.dicelang.v1.examples;
-import java.util.function.BiConsumer;
-
import bjc.dicelang.v1.ast.nodes.IDiceASTNode;
import bjc.utils.data.ITree;
import bjc.utils.funcdata.FunctionalStringTokenizer;
import bjc.utils.funcdata.IMap;
+import java.util.function.BiConsumer;
+
/**
* Alias for the type of a 'pragma' or special language command
- *
+ *
* To explain it, a pragma is a function that takes a tokenizer with the rest of
* the line, and an enviroment that contains variable bindings
- *
+ *
* @author ben
*
*/
diff --git a/dice-lang/src/bjc/dicelang/v1/examples/DiceExpressionParserTest.java b/dice-lang/src/bjc/dicelang/v1/examples/DiceExpressionParserTest.java
index adffc69..9d51c28 100644
--- a/dice-lang/src/bjc/dicelang/v1/examples/DiceExpressionParserTest.java
+++ b/dice-lang/src/bjc/dicelang/v1/examples/DiceExpressionParserTest.java
@@ -1,21 +1,21 @@
package bjc.dicelang.v1.examples;
-import java.util.HashMap;
-import java.util.Scanner;
-
import bjc.dicelang.v1.DiceExpressionParser;
import bjc.dicelang.v1.IDiceExpression;
+import java.util.HashMap;
+import java.util.Scanner;
+
/**
* Driver class for testing expression parser
- *
+ *
* @author ben
*
*/
public class DiceExpressionParserTest {
/**
* Run the parser test
- *
+ *
* @param args
* Unused CLI arguments
*/
@@ -44,7 +44,7 @@ public class DiceExpressionParserTest {
/*
* Roll the dice a specified amount of times
*/
- for (int i = 1; i <= nTimes; i++) {
+ for(int i = 1; i <= nTimes; i++) {
int roll = dexp.roll();
System.out.println("Rolled " + roll);
diff --git a/dice-lang/src/bjc/dicelang/v1/examples/DiceExpressionPreparer.java b/dice-lang/src/bjc/dicelang/v1/examples/DiceExpressionPreparer.java
index 121847c..5f38374 100644
--- a/dice-lang/src/bjc/dicelang/v1/examples/DiceExpressionPreparer.java
+++ b/dice-lang/src/bjc/dicelang/v1/examples/DiceExpressionPreparer.java
@@ -1,8 +1,5 @@
package bjc.dicelang.v1.examples;
-import java.util.Deque;
-import java.util.LinkedList;
-
import bjc.utils.data.IPair;
import bjc.utils.data.Pair;
import bjc.utils.funcdata.FunctionalStringTokenizer;
@@ -10,9 +7,12 @@ import bjc.utils.funcdata.IList;
import bjc.utils.funcutils.ListUtils;
import bjc.utils.parserutils.ShuntingYard;
+import java.util.Deque;
+import java.util.LinkedList;
+
/**
* Prepare a dice expression to be parsed
- *
+ *
* @author ben
*
*/
@@ -22,9 +22,9 @@ public class DiceExpressionPreparer {
*/
private static ShuntingYard<String> yard;
- private static final int MATH_PREC = 20;
- private static final int DICE_PREC = 10;
- private static final int EXPR_PREC = 0;
+ private static final int MATH_PREC = 20;
+ private static final int DICE_PREC = 10;
+ private static final int EXPR_PREC = 0;
// Do initialization for all parsers
static {
@@ -54,7 +54,7 @@ public class DiceExpressionPreparer {
/**
* Prepare a command, turning raw tokens into input for the tree builder
- *
+ *
* @param currentLine
* The command to prepare
* @return A stream of tokens representing the command
diff --git a/dice-lang/src/bjc/dicelang/v1/examples/DiceLanguageState.java b/dice-lang/src/bjc/dicelang/v1/examples/DiceLanguageState.java
index e2a71b4..17c147c 100644
--- a/dice-lang/src/bjc/dicelang/v1/examples/DiceLanguageState.java
+++ b/dice-lang/src/bjc/dicelang/v1/examples/DiceLanguageState.java
@@ -1,14 +1,14 @@
package bjc.dicelang.v1.examples;
-import java.util.Map;
-
import bjc.dicelang.v1.DiceExpressionParser;
import bjc.dicelang.v1.IDiceExpression;
import bjc.utils.data.Pair;
+import java.util.Map;
+
/**
* Internal state of dice language
- *
+ *
* @author ben
*
*/
@@ -22,7 +22,7 @@ public class DiceLanguageState extends Pair<DiceExpressionParser, Map<String, ID
/**
* Create a new state with the desired parameters
- *
+ *
* @param left
* The parser to use
* @param right
diff --git a/dice-lang/src/bjc/dicelang/v1/examples/DiceLanguageTest.java b/dice-lang/src/bjc/dicelang/v1/examples/DiceLanguageTest.java
index 2d90318..9a13b78 100644
--- a/dice-lang/src/bjc/dicelang/v1/examples/DiceLanguageTest.java
+++ b/dice-lang/src/bjc/dicelang/v1/examples/DiceLanguageTest.java
@@ -1,16 +1,16 @@
package bjc.dicelang.v1.examples;
+import bjc.dicelang.v1.DiceExpressionParser;
+import bjc.dicelang.v1.IDiceExpression;
+
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.function.BiConsumer;
-import bjc.dicelang.v1.DiceExpressionParser;
-import bjc.dicelang.v1.IDiceExpression;
-
/**
* Test of dice language
- *
+ *
* @author ben
*
*/
@@ -26,7 +26,7 @@ public class DiceLanguageTest {
/**
* Main method
- *
+ *
* @param args
* Unused CLI args
*/
@@ -41,10 +41,10 @@ public class DiceLanguageTest {
Map<String, IDiceExpression> env = new HashMap<>();
DiceLanguageState state = new DiceLanguageState(dep, env);
- while (!ln.equalsIgnoreCase("quit")) {
+ while(!ln.equalsIgnoreCase("quit")) {
String header = ln.split(" ")[0];
- if (acts.containsKey(header)) {
+ if(acts.containsKey(header)) {
acts.get(header).accept(ln, state);
} else {
IDiceExpression exp = DiceExpressionParser.parse(ln, env);
@@ -86,7 +86,7 @@ public class DiceLanguageTest {
IDiceExpression dexp = stat.merge((dep, env) -> env.get(strangs[1]));
- for (int i = 1; i <= nRolls; i++) {
+ for(int i = 1; i <= nRolls; i++) {
int roll = dexp.roll();
System.out.println("\tRolled " + roll);