diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-07-27 17:42:35 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-07-27 17:42:35 -0400 |
| commit | 67fee39e6dd22fce8dfaa800f0a5ddbe0ede0be3 (patch) | |
| tree | 76db8ccbfb360ce1e4abe8b293fd86a5a95ce923 /dice-lang/src/main/java/bjc/dicelang/ast | |
| parent | bfa7c67f491d8f693308e6299266f527b49be600 (diff) | |
General cleanup
Diffstat (limited to 'dice-lang/src/main/java/bjc/dicelang/ast')
4 files changed, 23 insertions, 19 deletions
diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/ArithmeticCollapser.java b/dice-lang/src/main/java/bjc/dicelang/ast/ArithmeticCollapser.java index 3820d68..e481e5e 100644 --- a/dice-lang/src/main/java/bjc/dicelang/ast/ArithmeticCollapser.java +++ b/dice-lang/src/main/java/bjc/dicelang/ast/ArithmeticCollapser.java @@ -18,8 +18,10 @@ import bjc.dicelang.ast.nodes.OperatorDiceNode; * */ final class ArithmeticCollapser implements IOperatorCollapser { + // The type of operator we're collapsing private OperatorDiceNode type; + // The operator to use to collapse operators private BinaryOperator<Integer> valueOp; public ArithmeticCollapser(OperatorDiceNode type, @@ -31,7 +33,7 @@ final class ArithmeticCollapser implements IOperatorCollapser { @Override public IPair<IResult, ITree<IDiceASTNode>> apply( IList<IPair<IResult, ITree<IDiceASTNode>>> nodes) { - IPair<IResult, ITree<IDiceASTNode>> initState = new Pair<>( + IPair<IResult, ITree<IDiceASTNode>> initialState = new Pair<>( new IntegerResult(0), new Tree<>(type)); BinaryOperator<IPair<IResult, ITree<IDiceASTNode>>> reducer = ( @@ -44,7 +46,7 @@ final class ArithmeticCollapser implements IOperatorCollapser { }; IPair<IResult, ITree<IDiceASTNode>> reducedState = nodes - .reduceAux(initState, reducer, (state) -> state); + .reduceAux(initialState, reducer, (state) -> state); return reducedState; } diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTUtils.java b/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTUtils.java index 8d70af8..d98c8fe 100644 --- a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTUtils.java +++ b/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTUtils.java @@ -21,13 +21,14 @@ 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 * reference */ public static boolean containsSimpleVariable( ITree<IDiceASTNode> nameTree) { - return nameTree.transformHead((nameNod) -> { - if (nameNod.getType() != DiceASTType.VARIABLE) { + return nameTree.transformHead((nameNode) -> { + if (nameNode.getType() != DiceASTType.VARIABLE) { return false; } @@ -36,10 +37,10 @@ public class DiceASTUtils { } /** - * Convert an AST tree to a dice expression, if possible. + * Convert an literal AST node to a dice expression, if possible. * * @param tree - * The tree to convert + * The node to convert in tree form * @return The tree as a dice expression * * @throws ClassCastException @@ -48,15 +49,16 @@ public class DiceASTUtils { * @throws UnsupportedOperationException * if the head of the tree is not optimizable */ - public static IDiceExpression toExpression(ITree<IDiceASTNode> tree) { - ILiteralDiceNode litNode = (ILiteralDiceNode) tree.getHead(); + public static IDiceExpression literalToExpression( + ITree<IDiceASTNode> tree) { + ILiteralDiceNode literalNode = (ILiteralDiceNode) tree.getHead(); - switch (litNode.getLiteralType()) { + switch (literalNode.getLiteralType()) { case DICE: - return ((DiceLiteralNode) litNode).getValue(); + return ((DiceLiteralNode) literalNode).getValue(); case INTEGER: return new ScalarDie( - ((IntegerLiteralNode) litNode).getValue()); + ((IntegerLiteralNode) literalNode).getValue()); default: throw new UnsupportedOperationException( "This type of literal isn't convertable to an expression"); @@ -64,11 +66,11 @@ public class DiceASTUtils { } /** - * Convert an AST tree to an integer, if possible. + * Convert an literal AST node to an integer, if possible. * * @param tree - * The tree to convert - * @return The tree as an integer + * 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 @@ -76,7 +78,7 @@ public class DiceASTUtils { * @throws UnsupportedOperationException * if the head of the tree is not optimizable */ - public static int toInt(ITree<IDiceASTNode> tree) { + public static int literalToInteger(ITree<IDiceASTNode> tree) { return tree.transformHead((node) -> { return ((ILiteralDiceNode) node).optimize(); }); diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/optimization/ArithmeticCollapser.java b/dice-lang/src/main/java/bjc/dicelang/ast/optimization/ArithmeticCollapser.java index 70e518e..960fbf7 100644 --- a/dice-lang/src/main/java/bjc/dicelang/ast/optimization/ArithmeticCollapser.java +++ b/dice-lang/src/main/java/bjc/dicelang/ast/optimization/ArithmeticCollapser.java @@ -39,12 +39,12 @@ class ArithmeticCollapser { return new Tree<>(type, children); } - int initState = DiceASTUtils.toInt(children.first()); + int initState = DiceASTUtils.literalToInteger(children.first()); return children.tail().reduceAux(initState, (currentNode, state) -> { return reducer.apply(state, - DiceASTUtils.toInt(currentNode)); + DiceASTUtils.literalToInteger(currentNode)); }, (state) -> new Tree<>(new IntegerLiteralNode(state))); } } diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/optimization/ConstantCollapser.java b/dice-lang/src/main/java/bjc/dicelang/ast/optimization/ConstantCollapser.java index 3b43ff6..6749cab 100644 --- a/dice-lang/src/main/java/bjc/dicelang/ast/optimization/ConstantCollapser.java +++ b/dice-lang/src/main/java/bjc/dicelang/ast/optimization/ConstantCollapser.java @@ -64,8 +64,8 @@ public class ConstantCollapser implements IOptimizationPass { } ComplexDice dice = new ComplexDice( - DiceASTUtils.toExpression(children.getByIndex(0)), - DiceASTUtils.toExpression(children.getByIndex(1))); + DiceASTUtils.literalToExpression(children.getByIndex(0)), + DiceASTUtils.literalToExpression(children.getByIndex(1))); if (dice.canOptimize()) { return new Tree<>( |
