From 67fee39e6dd22fce8dfaa800f0a5ddbe0ede0be3 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Wed, 27 Jul 2016 17:42:35 -0400 Subject: General cleanup --- .../java/bjc/dicelang/ast/ArithmeticCollapser.java | 6 +++-- .../main/java/bjc/dicelang/ast/DiceASTUtils.java | 28 ++++++++++++---------- .../ast/optimization/ArithmeticCollapser.java | 4 ++-- .../ast/optimization/ConstantCollapser.java | 4 ++-- 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 valueOp; public ArithmeticCollapser(OperatorDiceNode type, @@ -31,7 +33,7 @@ final class ArithmeticCollapser implements IOperatorCollapser { @Override public IPair> apply( IList>> nodes) { - IPair> initState = new Pair<>( + IPair> initialState = new Pair<>( new IntegerResult(0), new Tree<>(type)); BinaryOperator>> reducer = ( @@ -44,7 +46,7 @@ final class ArithmeticCollapser implements IOperatorCollapser { }; IPair> 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 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 tree) { - ILiteralDiceNode litNode = (ILiteralDiceNode) tree.getHead(); + public static IDiceExpression literalToExpression( + ITree 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 tree) { + public static int literalToInteger(ITree 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<>( -- cgit v1.2.3