From 90d1cc6c9f47f1b6f74fb57e07865795a46c23b8 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Fri, 8 Apr 2016 13:29:48 -0400 Subject: Change to data interfaces, as well as prepare to rewrite parser --- .../ast/optimization/DiceASTOptimizer.java | 59 ++++++++++------------ 1 file changed, 27 insertions(+), 32 deletions(-) (limited to 'dice-lang/src/main/java/bjc/dicelang/ast/optimization/DiceASTOptimizer.java') diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/optimization/DiceASTOptimizer.java b/dice-lang/src/main/java/bjc/dicelang/ast/optimization/DiceASTOptimizer.java index e6c62ee..f6be7ab 100644 --- a/dice-lang/src/main/java/bjc/dicelang/ast/optimization/DiceASTOptimizer.java +++ b/dice-lang/src/main/java/bjc/dicelang/ast/optimization/DiceASTOptimizer.java @@ -36,26 +36,26 @@ public class DiceASTOptimizer { @Override public AST apply(AST leftAST, AST rightAST) { - AST rightBranchOfLeftAST = - leftAST.applyToRight((rightSideAST) -> rightSideAST); - AST leftBranchOfLeftAST = - leftAST.applyToRight((rightSideAST) -> rightSideAST); + AST rightBranchOfLeftAST = leftAST + .applyToRight((rightSideAST) -> rightSideAST); + AST leftBranchOfLeftAST = leftAST + .applyToRight((rightSideAST) -> rightSideAST); boolean leftContainsNestedConstant = DiceASTOptimizer .checkNodeType(rightBranchOfLeftAST, LITERAL) && DiceASTOptimizer.isNodeConstant(leftAST); - boolean isRightConstant = - DiceASTOptimizer.checkNodeType(rightAST, LITERAL) - && DiceASTOptimizer.isNodeConstant(leftAST); + boolean isRightConstant = DiceASTOptimizer + .checkNodeType(rightAST, LITERAL) + && DiceASTOptimizer.isNodeConstant(leftAST); if (leftContainsNestedConstant && isRightConstant) { int combinedValue = valueCollapser.apply( getNodeValue(rightBranchOfLeftAST), getNodeValue(rightAST)); - AST newRightBranch = - new AST<>(new LiteralDiceNode(combinedValue)); + AST newRightBranch = new AST<>( + new LiteralDiceNode(combinedValue)); return new AST<>(type, leftBranchOfLeftAST, newRightBranch); @@ -82,13 +82,13 @@ public class DiceASTOptimizer { @Override public AST apply(AST leftAST, AST rightAST) { - boolean isLeftConstant = - DiceASTOptimizer.checkNodeType(leftAST, LITERAL) - && DiceASTOptimizer.isNodeConstant(leftAST); + boolean isLeftConstant = DiceASTOptimizer + .checkNodeType(leftAST, LITERAL) + && DiceASTOptimizer.isNodeConstant(leftAST); - boolean isRightConstant = - DiceASTOptimizer.checkNodeType(rightAST, LITERAL) - && DiceASTOptimizer.isNodeConstant(leftAST); + boolean isRightConstant = DiceASTOptimizer + .checkNodeType(rightAST, LITERAL) + && DiceASTOptimizer.isNodeConstant(leftAST); if (isLeftConstant) { if (isRightConstant) { @@ -107,10 +107,8 @@ public class DiceASTOptimizer { } } - private static Map>> - buildConstantCollapsers() { - Map>> operatorCollapsers = - new HashMap<>(); + private static Map>> buildConstantCollapsers() { + Map>> operatorCollapsers = new HashMap<>(); operatorCollapsers.put(OperatorDiceNode.ADD, new ArithmeticOperationCollapser(OperatorDiceNode.ADD, @@ -131,10 +129,8 @@ public class DiceASTOptimizer { return operatorCollapsers; } - private static Map>> - buildNestedConstantCollapsers() { - Map>> operatorCollapsers = - new HashMap<>(); + private static Map>> buildNestedConstantCollapsers() { + Map>> operatorCollapsers = new HashMap<>(); operatorCollapsers.put(OperatorDiceNode.ADD, new NestedArithmeticOperationCollapser( @@ -167,9 +163,9 @@ public class DiceASTOptimizer { IDiceExpression leaf) { if (leaf.canOptimize()) { return new LiteralDiceNode(Integer.toString(leaf.optimize())); - } else { - return node; } + + return node; } private static AST finishTree(AST tree) { @@ -184,14 +180,13 @@ public class DiceASTOptimizer { * @return The optimized tree */ public static AST optimizeTree(AST tree) { - AST astWithConstantsFolded = - tree.collapse(DiceASTOptimizer::collapseLeaf, - buildConstantCollapsers()::get, - DiceASTOptimizer::finishTree); + AST astWithConstantsFolded = tree.collapse( + DiceASTOptimizer::collapseLeaf, + buildConstantCollapsers()::get, + DiceASTOptimizer::finishTree); - AST astWithNestedConstantsFolded = - astWithConstantsFolded.collapse( - DiceASTOptimizer::collapseLeaf, + AST astWithNestedConstantsFolded = astWithConstantsFolded + .collapse(DiceASTOptimizer::collapseLeaf, buildNestedConstantCollapsers()::get, DiceASTOptimizer::finishTree); -- cgit v1.2.3