diff options
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/dice')
20 files changed, 574 insertions, 183 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/BindingDiceExpression.java b/BJC-Utils2/src/main/java/bjc/utils/dice/BindingDiceExpression.java index a715074..9ecce97 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/BindingDiceExpression.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/BindingDiceExpression.java @@ -10,10 +10,34 @@ import java.util.Map; * */ public class BindingDiceExpression implements IDiceExpression { - private String name; + /** + * The expression being bound to a name + */ private IDiceExpression exp; /** + * The name to bind the expression to + */ + private String name; + + /** + * 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 + * @param right + * The right side to bind to the name + * @param env + * The enviroment to bind into + */ + public BindingDiceExpression(IDiceExpression left, + IDiceExpression right, Map<String, IDiceExpression> env) { + this(((ReferenceDiceExpression) left).getName(), right, env); + } + + /** * Create a new dice expression binder * * @param name @@ -31,16 +55,21 @@ public class BindingDiceExpression implements IDiceExpression { env.put(name, exp); } - public BindingDiceExpression(IDiceExpression left, - IDiceExpression right, Map<String, IDiceExpression> env) { - this(((ReferenceDiceExpression) left).getName(), right, env); - } - + /* + * (non-Javadoc) + * + * @see bjc.utils.dice.IDiceExpression#roll() + */ @Override public int roll() { return exp.roll(); } + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ @Override public String toString() { return "assign[n=" + name + ", exp=" + exp.toString() + "]"; diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/ComplexDice.java b/BJC-Utils2/src/main/java/bjc/utils/dice/ComplexDice.java index 98a510a..226f9fd 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/ComplexDice.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/ComplexDice.java @@ -9,6 +9,37 @@ package bjc.utils.dice; */ public class ComplexDice implements IDiceExpression { /** + * Create a dice from a string expression + * + * @param dice + * The string to parse the dice from + * @return A dice group parsed from the string + */ + public static ComplexDice fromString(String dice) { + /* + * Split it on the dice type marker + */ + String[] strangs = dice.split("d"); + + try { + /* + * Create the actual dice + */ + return new ComplexDice( + new ScalarDie(Integer.parseInt(strangs[0])), + new Die(Integer.parseInt(strangs[1]))); + } catch (NumberFormatException nfex) { + /* + * Tell the user the expression is invalid + */ + throw new IllegalArgumentException( + "Attempted to create a dice using something that's not" + + " an integer: " + strangs[0] + " and " + + strangs[1] + " are likely culprits."); + } + } + + /** * The die being rolled */ private IDiceExpression die; @@ -34,7 +65,7 @@ public class ComplexDice implements IDiceExpression { /** * Create a new collection of dice * - * @param nDce + * @param nSides * The number of dice in the collection * @param de * The type of dice the collection is composed of @@ -44,6 +75,11 @@ public class ComplexDice implements IDiceExpression { die = new Die(de); } + /* + * (non-Javadoc) + * + * @see bjc.utils.dice.IDiceExpression#roll() + */ @Override public int roll() { int res = 0; @@ -60,37 +96,11 @@ public class ComplexDice implements IDiceExpression { return res; } - /** - * Create a dice from a string expression + /* + * (non-Javadoc) * - * @param dice - * The string to parse the dice from - * @return A dice group parsed from the string + * @see java.lang.Object#toString() */ - public static ComplexDice fromString(String dice) { - /* - * Split it on the dice type marker - */ - String[] strangs = dice.split("d"); - - try { - /* - * Create the actual dice - */ - return new ComplexDice( - new ScalarDie(Integer.parseInt(strangs[0])), - new Die(Integer.parseInt(strangs[1]))); - } catch (NumberFormatException nfex) { - /* - * Tell the user the expression is invalid - */ - throw new IllegalArgumentException( - "Attempted to create a dice using something that's not" - + " an integer: " + strangs[0] + " and " - + strangs[1] + " are likely culprits."); - } - } - @Override public String toString() { if (nDice instanceof ScalarDie && die instanceof Die) { diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/CompoundDice.java b/BJC-Utils2/src/main/java/bjc/utils/dice/CompoundDice.java index 4e2e9f3..3393711 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/CompoundDice.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/CompoundDice.java @@ -14,6 +14,7 @@ public class CompoundDice implements IDiceExpression { * The left die of the expression */ private IDiceExpression l; + /** * The right die of the expression */ @@ -32,14 +33,33 @@ public class CompoundDice implements IDiceExpression { this.r = r; } + /** + * Create a new compound dice from two dice strings + * + * @param l + * The left side dice + * @param r + * The right side dice + */ public CompoundDice(String l, String r) { this(ComplexDice.fromString(l), ComplexDice.fromString(r)); } + /** + * Create a new compound dice from an array of dice strings + * + * @param exps + * An array of dice strings + */ public CompoundDice(String[] exps) { this(exps[0], exps[1]); } + /* + * (non-Javadoc) + * + * @see bjc.utils.dice.IDiceExpression#roll() + */ @Override public int roll() { /* @@ -48,6 +68,11 @@ public class CompoundDice implements IDiceExpression { return Integer.parseInt(l.roll() + "" + r.roll()); } + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ @Override public String toString() { return "compound[l=" + l.toString() + ", r=" + r.toString() + "]"; diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/CompoundDiceExpression.java b/BJC-Utils2/src/main/java/bjc/utils/dice/CompoundDiceExpression.java index 12238c8..41b1df2 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/CompoundDiceExpression.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/CompoundDiceExpression.java @@ -39,6 +39,11 @@ public class CompoundDiceExpression implements IDiceExpression { this.det = det; } + /* + * (non-Javadoc) + * + * @see bjc.utils.dice.IDiceExpression#roll() + */ @Override public int roll() { /* @@ -65,6 +70,11 @@ public class CompoundDiceExpression implements IDiceExpression { } } + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ @Override public String toString() { return "dice-exp[type=" + det + ", l=" + left.toString() + ", r=" diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionParser.java b/BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionParser.java index a7d1f8c..4113be4 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionParser.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionParser.java @@ -21,6 +21,8 @@ public class DiceExpressionParser { * * @param exp * The string to parse an expression from + * @param env + * The enviroment to use when parsing expressions * @return The parsed dice expression */ public IDiceExpression parse(String exp, @@ -48,8 +50,8 @@ public class DiceExpressionParser { /* * Shunt the expression to postfix form */ - FunctionalList<String> ls = yard.postfix(fst.toList(s -> s), - s -> s); + FunctionalList<String> ls = + yard.postfix(fst.toList(s -> s), s -> s); /* * Create a stack for building an expression from parts diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionType.java b/BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionType.java index 2259308..d719ae8 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionType.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionType.java @@ -1,11 +1,35 @@ package bjc.utils.dice; -/* +/** * Enumeration for basic dice expression operators */ public enum DiceExpressionType { - ADD, DIVIDE, MULTIPLY, SUBTRACT; + /** + * Add two expressions + */ + ADD, + /** + * Divide two expressions + */ + DIVIDE, + + /** + * Multiply two expressions + */ + MULTIPLY, + + /** + * Subtract two expressions + */ + SUBTRACT; + + /* + * (non-Javadoc) + * + * @see java.lang.Enum#toString() + */ + @Override public String toString() { switch (this) { case ADD: diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/Die.java b/BJC-Utils2/src/main/java/bjc/utils/dice/Die.java index 9575df5..0bd6964 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/Die.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/Die.java @@ -29,15 +29,19 @@ public class Die implements IDiceExpression { this.nSides = nSides; } - /** - * Roll this dice once - * - * @return The result of rolling the dice + /* + * (non-Javadoc) + * @see bjc.utils.dice.IDiceExpression#roll() */ + @Override public int roll() { return rng.nextInt(nSides) + 1; } + /* + * (non-Javadoc) + * @see java.lang.Object#toString() + */ @Override public String toString() { return "d" + nSides; diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/IDiceExpression.java b/BJC-Utils2/src/main/java/bjc/utils/dice/IDiceExpression.java index 4bd0973..5ead9ad 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/IDiceExpression.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/IDiceExpression.java @@ -6,9 +6,11 @@ package bjc.utils.dice; * @author ben * */ +@FunctionalInterface public interface IDiceExpression { /** * Roll the dice once + * * @return The result of rowing the dice */ public int roll(); diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/PolyhedralDice.java b/BJC-Utils2/src/main/java/bjc/utils/dice/PolyhedralDice.java index 511fd99..314d47b 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/PolyhedralDice.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/PolyhedralDice.java @@ -1,7 +1,7 @@ package bjc.utils.dice; /** - * Class that produces common polyhedral dice + * Utility class that produces common polyhedral dice * * @author ben * @@ -52,33 +52,33 @@ public class PolyhedralDice { } /** - * Produce the specified number of 10-sided dice + * Produce the specified number of 4-sided dice * * @param nDice - * The number of ten-sided dice to produce - * @return A group of ten-sided dice of the specified size + * The number of four-sided dice to produce + * @return A group of four-sided dice of the specified size */ public static IDiceExpression d4(int nDice) { return new ComplexDice(nDice, 4); } /** - * Produce the specified number of 10-sided dice + * Produce the specified number of 6-sided dice * * @param nDice - * The number of ten-sided dice to produce - * @return A group of ten-sided dice of the specified size + * The number of six-sided dice to produce + * @return A group of six-sided dice of the specified size */ public static IDiceExpression d6(int nDice) { return new ComplexDice(nDice, 6); } /** - * Produce the specified number of 10-sided dice + * Produce the specified number of 8-sided dice * * @param nDice - * The number of ten-sided dice to produce - * @return A group of ten-sided dice of the specified size + * The number of eight-sided dice to produce + * @return A group of eight-sided dice of the specified size */ public static IDiceExpression d8(int nDice) { return new ComplexDice(nDice, 8); diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/ReferenceDiceExpression.java b/BJC-Utils2/src/main/java/bjc/utils/dice/ReferenceDiceExpression.java index d8062da..d38e0f9 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/ReferenceDiceExpression.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/ReferenceDiceExpression.java @@ -11,14 +11,14 @@ import java.util.Map; */ public class ReferenceDiceExpression implements IDiceExpression { /** - * The name of the bound variable + * The enviroment to do variable dereferencing against */ - private String name; + private Map<String, IDiceExpression> env; /** - * The enviroment to do variable dereferencing against + * The name of the bound variable */ - private Map<String, IDiceExpression> env; + private String name; /** * Create a new reference dice expression referring to the given name @@ -35,11 +35,30 @@ public class ReferenceDiceExpression implements IDiceExpression { this.env = env; } + /** + * Get the name of the referenced variable + * + * @return the name of the referenced variable + */ + public String getName() { + return name; + } + + /* + * (non-Javadoc) + * + * @see bjc.utils.dice.IDiceExpression#roll() + */ @Override public int roll() { return env.get(name).roll(); } + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ @Override public String toString() { if (env.containsKey(name)) { @@ -48,13 +67,4 @@ public class ReferenceDiceExpression implements IDiceExpression { return name; } } - - /** - * Get the name of the referenced variable - * - * @return the name of the referenced variable - */ - public String getName() { - return name; - } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/ScalarDiceExpression.java b/BJC-Utils2/src/main/java/bjc/utils/dice/ScalarDiceExpression.java index 2b174c8..267e6ef 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/ScalarDiceExpression.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/ScalarDiceExpression.java @@ -11,6 +11,7 @@ public class ScalarDiceExpression implements IDiceExpression { * The operation to combine with */ private DiceExpressionType det; + /** * The expression to be combined */ @@ -38,6 +39,10 @@ public class ScalarDiceExpression implements IDiceExpression { det = dt; } + /* + * (non-Javadoc) + * @see bjc.utils.dice.IDiceExpression#roll() + */ @Override public int roll() { switch (det) { @@ -56,6 +61,10 @@ public class ScalarDiceExpression implements IDiceExpression { } } + /* + * (non-Javadoc) + * @see java.lang.Object#toString() + */ @Override public String toString() { return "scalar-exp[type=" + det + ", l=" + scalar + ", r=" diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/ScalarDie.java b/BJC-Utils2/src/main/java/bjc/utils/dice/ScalarDie.java index bef68e1..4ed99b9 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/ScalarDie.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/ScalarDie.java @@ -12,11 +12,6 @@ public class ScalarDie implements IDiceExpression { */ private int num; - @Override - public int roll() { - return num; - } - /** * Create a dice with the specified number * @@ -27,6 +22,21 @@ public class ScalarDie implements IDiceExpression { this.num = num; } + /* + * (non-Javadoc) + * + * @see bjc.utils.dice.IDiceExpression#roll() + */ + @Override + public int roll() { + return num; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ @Override public String toString() { return Integer.toString(num); diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTExpression.java b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTExpression.java index 3b81888..aceeed0 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTExpression.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTExpression.java @@ -12,48 +12,22 @@ import bjc.utils.dice.CompoundDice; import bjc.utils.dice.IDiceExpression; import bjc.utils.parserutils.AST; +/** + * An implementation of {@link IDiceExpression} backed by an AST of + * {@link IDiceASTNode}s + * + * @author ben + * + */ public class DiceASTExpression implements IDiceExpression { - private AST<IDiceASTNode> ast; - private Map<String, DiceASTExpression> env; - - public DiceASTExpression(AST<IDiceASTNode> ast, - Map<String, DiceASTExpression> env) { - this.ast = ast; - this.env = env; - } - - private Pair<Integer, AST<IDiceASTNode>> evalLeaf(IDiceASTNode tokn) { - if (tokn instanceof VariableDiceNode) { - String varName = ((VariableDiceNode) tokn).getVariable(); - - if (env.containsKey(varName)) { - return new Pair<>(env.get(varName).roll(), new AST<>(tokn)); - } else { - // Handle special case for defining variables - return new Pair<>(0, new AST<>(tokn)); - } - } else { - LiteralDiceNode lnod = (LiteralDiceNode) tokn; - String dat = lnod.getData(); - - if (StringUtils.countMatches(dat, 'c') == 1 - && !dat.equalsIgnoreCase("c")) { - String[] strangs = dat.split("c"); - return new Pair<>(new CompoundDice(strangs).roll(), - new AST<>(tokn)); - } else if (StringUtils.countMatches(dat, 'd') == 1 - && !dat.equalsIgnoreCase("d")) { - /* - * Handle dice groups - */ - return new Pair<>(ComplexDice.fromString(dat).roll(), - new AST<>(tokn)); - } else { - return new Pair<>(Integer.parseInt(dat), new AST<>(tokn)); - } - } - } + /** + * Build the map of operations to use when collapsing the AST + * + * @param env + * The enviroment to evaluate bindings and such against + * @return The operations to use when collapsing the AST + */ private static Map<IDiceASTNode, BinaryOperator<Pair<Integer, AST<IDiceASTNode>>>> buildOperations(Map<String, DiceASTExpression> env) { @@ -121,6 +95,83 @@ public class DiceASTExpression implements IDiceExpression { return opCollapsers; } + /** + * The AST this expression will evaluate + */ + private AST<IDiceASTNode> ast; + + /** + * The enviroment to evaluate bindings and such against + */ + private Map<String, DiceASTExpression> env; + + /** + * Create a new dice expression backed by an AST + * + * @param ast + * The AST backing this expression + * @param env + * The enviroment to evaluate bindings against + */ + public DiceASTExpression(AST<IDiceASTNode> ast, + Map<String, DiceASTExpression> env) { + this.ast = ast; + this.env = env; + } + + /** + * Expand a leaf AST token into a pair for evaluation + * + * @param tokn + * The token to evaluate + * @return A pair consisting of the token's value and the AST it + * represents + */ + private Pair<Integer, AST<IDiceASTNode>> evalLeaf(IDiceASTNode tokn) { + if (tokn instanceof VariableDiceNode) { + String varName = ((VariableDiceNode) tokn).getVariable(); + + if (env.containsKey(varName)) { + return new Pair<>(env.get(varName).roll(), + new AST<>(tokn)); + } else { + // Handle special case for defining variables + return new Pair<>(0, new AST<>(tokn)); + } + } else { + LiteralDiceNode lnod = (LiteralDiceNode) tokn; + String dat = lnod.getData(); + + if (StringUtils.countMatches(dat, 'c') == 1 + && !dat.equalsIgnoreCase("c")) { + String[] strangs = dat.split("c"); + return new Pair<>(new CompoundDice(strangs).roll(), + new AST<>(tokn)); + } else if (StringUtils.countMatches(dat, 'd') == 1 + && !dat.equalsIgnoreCase("d")) { + /* + * Handle dice groups + */ + return new Pair<>(ComplexDice.fromString(dat).roll(), + new AST<>(tokn)); + } else { + return new Pair<>(Integer.parseInt(dat), new AST<>(tokn)); + } + } + } + + /** + * Get the AST bound to this expression + * @return the ast + */ + public AST<IDiceASTNode> getAst() { + return ast; + } + + /* + * (non-Javadoc) + * @see bjc.utils.dice.IDiceExpression#roll() + */ @Override public int roll() { Map<IDiceASTNode, BinaryOperator<Pair<Integer, AST<IDiceASTNode>>>> operations = @@ -130,15 +181,12 @@ public class DiceASTExpression implements IDiceExpression { (r) -> r.merge((left, right) -> left)); } + /* + * (non-Javadoc) + * @see java.lang.Object#toString() + */ @Override public String toString() { return ast.toString(); } - - /** - * @return the ast - */ - public AST<IDiceASTNode> getAst() { - return ast; - } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTFlattener.java b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTFlattener.java index da402c3..70465a5 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTFlattener.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTFlattener.java @@ -16,25 +16,25 @@ import bjc.utils.dice.ReferenceDiceExpression; import bjc.utils.dice.ScalarDie; import bjc.utils.parserutils.AST; +/** + * Flatten an {@link AST} of {@link IDiceASTNode} into a + * {@link IDiceExpression} + * + * @author ben + * + */ public class DiceASTFlattener { - public static IDiceExpression flatten(AST<IDiceASTNode> ast, - Map<String, IDiceExpression> env) { - Map<IDiceASTNode, BinaryOperator<IDiceExpression>> opCollapsers = buildOperations( - env); - - return ast.collapse((nod) -> { - if (nod instanceof LiteralDiceNode) { - return expFromLiteral((LiteralDiceNode) nod); - } else { - return new ReferenceDiceExpression( - ((VariableDiceNode) nod).getVariable(), env); - } - } , opCollapsers::get, (r) -> r); - } - - private static Map<IDiceASTNode, BinaryOperator<IDiceExpression>> buildOperations( - Map<String, IDiceExpression> env) { - Map<IDiceASTNode, BinaryOperator<IDiceExpression>> opCollapsers = new HashMap<>(); + /** + * Build the operations to use for tree flattening + * + * @param env + * The enviroment the tree will be flattened against + * @return The operations needed for tree flattening + */ + private static Map<IDiceASTNode, BinaryOperator<IDiceExpression>> + buildOperations(Map<String, IDiceExpression> env) { + Map<IDiceASTNode, BinaryOperator<IDiceExpression>> opCollapsers = + new HashMap<>(); opCollapsers.put(OperatorDiceNode.ADD, (left, right) -> { return new CompoundDiceExpression(right, left, DiceExpressionType.ADD); @@ -64,6 +64,13 @@ public class DiceASTFlattener { return opCollapsers; } + /** + * Create a dice expression from a literal token + * + * @param tok + * The token to convert to an expression + * @return The dice expression represented by the token + */ private static IDiceExpression expFromLiteral(LiteralDiceNode tok) { String data = tok.getData(); @@ -80,4 +87,28 @@ public class DiceASTFlattener { return new ScalarDie(Integer.parseInt(data)); } } + + /** + * Flatten a AST into a dice expression + * + * @param ast + * The AST to flatten + * @param env + * The enviroment to flatten against + * @return The AST, flattened into a dice expression + */ + public static IDiceExpression flatten(AST<IDiceASTNode> ast, + Map<String, IDiceExpression> env) { + Map<IDiceASTNode, BinaryOperator<IDiceExpression>> opCollapsers = + buildOperations(env); + + return ast.collapse((nod) -> { + if (nod instanceof LiteralDiceNode) { + return expFromLiteral((LiteralDiceNode) nod); + } else { + return new ReferenceDiceExpression( + ((VariableDiceNode) nod).getVariable(), env); + } + } , opCollapsers::get, (r) -> r); + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTFreezer.java b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTFreezer.java index 04cc99b..efe37c0 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTFreezer.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTFreezer.java @@ -4,7 +4,52 @@ import java.util.Map; import bjc.utils.parserutils.AST; +/** + * Freeze references in a dice AST, replacing variable references with what + * the variables refer to + * + * @author ben + * + */ public class DiceASTFreezer { + /** + * Expand a reference + * + * @param vnode + * The node containing the reference to expand + * @param env + * The enviroment to expand against + * @return The expanded reference + */ + private static AST<IDiceASTNode> expandNode(VariableDiceNode vnode, + Map<String, AST<IDiceASTNode>> env) { + return env.get(vnode.getVariable()); + } + + /** + * Expand a reference + * + * @param vnode + * The node containing the reference to expand + * @param env + * The enviroment to expand against + * @return The expanded reference + */ + private static AST<IDiceASTNode> expandNode2(VariableDiceNode vnode, + Map<String, DiceASTExpression> env) { + return env.get(vnode.getVariable()).getAst(); + } + + /** + * Freeze the references in an AST + * + * @param tree + * The tree to freeze references in + * @param env + * The enviroment to get reference values from + * @return The tree with references frozen + */ + @SuppressWarnings("unused") public static AST<IDiceASTNode> freezeAST(AST<IDiceASTNode> tree, Map<String, AST<IDiceASTNode>> env) { return tree.collapse((nod) -> { @@ -20,6 +65,16 @@ public class DiceASTFreezer { } , (r) -> r); } + /** + * Freeze the references in an expression backed by an AST + * + * @param tree + * The tree-backed expression to freeze references in + * @param env + * The enviroment to get reference values from + * @return The tree with references frozen + */ + @SuppressWarnings("unused") public static AST<IDiceASTNode> freezeAST(DiceASTExpression tree, Map<String, DiceASTExpression> env) { return tree.getAst().collapse((nod) -> { @@ -34,14 +89,4 @@ public class DiceASTFreezer { return new AST<IDiceASTNode>(op, left, right); } , (r) -> r); } - - private static AST<IDiceASTNode> expandNode(VariableDiceNode vnode, - Map<String, AST<IDiceASTNode>> env) { - return env.get(vnode.getVariable()); - } - - private static AST<IDiceASTNode> expandNode2(VariableDiceNode vnode, - Map<String, DiceASTExpression> env) { - return env.get(vnode.getVariable()).getAst(); - } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTParser.java b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTParser.java index d56ad0e..b25f5b4 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTParser.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTParser.java @@ -8,7 +8,16 @@ import bjc.utils.parserutils.AST; import bjc.utils.parserutils.ShuntingYard; import bjc.utils.parserutils.TreeConstructor; +/** + * Create an AST from a string expression + * + * @author ben + * + */ public class DiceASTParser { + /** + * The yard to use for shunting expressions + */ private static ShuntingYard<String> yard; static { @@ -22,14 +31,21 @@ public class DiceASTParser { // expression } + /** + * Build an AST from a string expression + * + * @param exp + * The string to build from + * @return An AST built from the passed in string + */ public AST<IDiceASTNode> buildAST(String exp) { - FunctionalList<String> tokens = FunctionalStringTokenizer - .fromString(exp).toList((s) -> s); + FunctionalList<String> tokens = + FunctionalStringTokenizer.fromString(exp).toList((s) -> s); FunctionalList<String> shunted = yard.postfix(tokens, (s) -> s); - AST<String> rawAST = TreeConstructor.constructTree(shunted, - this::isOperator); + AST<String> rawAST = + TreeConstructor.constructTree(shunted, this::isOperator); AST<IDiceASTNode> bakedAST = rawAST.transmuteAST((tok) -> { if (isOperator(tok)) { @@ -44,22 +60,14 @@ public class DiceASTParser { return bakedAST; } - private boolean isOperator(String tok) { - switch (tok) { - case ":=": - case "+": - case "-": - case "*": - case "/": - case "c": - case "d": - return true; - default: - return false; - } - } - - private boolean isLiteral(String tok) { + /** + * Check if a token represents a literal + * + * @param tok + * The token to check + * @return Whether or not the token represents a literal + */ + private static boolean isLiteral(String tok) { if (StringUtils.countMatches(tok, 'c') == 1 && !tok.equalsIgnoreCase("c")) { return true; @@ -75,4 +83,26 @@ public class DiceASTParser { } } } + + /** + * Check if a token represents an operator + * + * @param tok + * The token to check if it represents an operator + * @return Whether or not the token represents an operator + */ + private boolean isOperator(String tok) { + switch (tok) { + case ":=": + case "+": + case "-": + case "*": + case "/": + case "c": + case "d": + return true; + default: + return false; + } + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/IDiceASTNode.java b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/IDiceASTNode.java index 3fb14fe..073da89 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/IDiceASTNode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/IDiceASTNode.java @@ -1,5 +1,16 @@ package bjc.utils.dice.ast; +/** + * The interface for a node in a dice AST + * + * @author ben + * + */ public interface IDiceASTNode { + /** + * Check if this node represents an operator or not + * + * @return Whether or not this node represents an operator + */ public boolean isOperator(); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/LiteralDiceNode.java b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/LiteralDiceNode.java index 20358fb..b0c1400 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/LiteralDiceNode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/LiteralDiceNode.java @@ -1,8 +1,23 @@ package bjc.utils.dice.ast; +/** + * A AST node that represents a literal value + * + * @author ben + * + */ public class LiteralDiceNode implements IDiceASTNode { + /** + * The value contained by this node + */ private String data; + /** + * Create a new node with the given value + * + * @param data + * The value to be in this node + */ public LiteralDiceNode(String data) { this.data = data; } @@ -20,7 +35,12 @@ public class LiteralDiceNode implements IDiceASTNode { public String getData() { return data; } - + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ @Override public String toString() { return data; diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/OperatorDiceNode.java b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/OperatorDiceNode.java index 92b49b7..c4f7763 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/OperatorDiceNode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/OperatorDiceNode.java @@ -4,16 +4,51 @@ package bjc.utils.dice.ast; // 1. DiceASTExpression // 2. DiceASTFlattener // 3. DiceASTParser +/** + * A node that represents an operator + * + * @author ben + * + */ public enum OperatorDiceNode implements IDiceASTNode { - ASSIGN, ADD, SUBTRACT, MULTIPLY, DIVIDE, GROUP, COMPOUND; - - @Override - public boolean isOperator() { - return true; - } - + /** + * Represents adding two nodes + */ + ADD, + /** + * Represents assigning one node to another + */ + ASSIGN, + /** + * Representings combining two node values together + */ + COMPOUND, + /** + * Represents dividing two nodes + */ + DIVIDE, + /** + * Represents using one node a variable number of times + */ + GROUP, + /** + * Represents multiplying two nodes + */ + MULTIPLY, + /** + * Represents subtracting two nodes + */ + SUBTRACT; + + /** + * 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 "+": @@ -29,7 +64,18 @@ public enum OperatorDiceNode implements IDiceASTNode { case "c": return COMPOUND; default: - throw new IllegalArgumentException(s + " is not a valid operator node"); + throw new IllegalArgumentException( + s + " is not a valid operator node"); } } + + /* + * (non-Javadoc) + * + * @see bjc.utils.dice.ast.IDiceASTNode#isOperator() + */ + @Override + public boolean isOperator() { + return true; + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/VariableDiceNode.java b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/VariableDiceNode.java index 6ae3189..43a09b2 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/VariableDiceNode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/VariableDiceNode.java @@ -1,17 +1,27 @@ package bjc.utils.dice.ast; +/** + * A node that represents a variable reference + * + * @author ben + * + */ public class VariableDiceNode implements IDiceASTNode { + /** + * The variable referenced by this node + */ private String var; + /** + * Create a new node representing the specified variable + * + * @param data + * The name of the variable being referenced + */ public VariableDiceNode(String data) { this.var = data; } - @Override - public boolean isOperator() { - return false; - } - /** * Get the variable referenced by this AST node * @@ -20,7 +30,22 @@ public class VariableDiceNode implements IDiceASTNode { public String getVariable() { return var; } - + + /* + * (non-Javadoc) + * + * @see bjc.utils.dice.ast.IDiceASTNode#isOperator() + */ + @Override + public boolean isOperator() { + return false; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ @Override public String toString() { return var; |
