From e7413128ff4e376997de6e94e4bea5eca14811ef Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Thu, 27 Oct 2016 21:56:18 -0400 Subject: Moved examples --- .../ast/optimization/ArithmeticCollapser.java | 50 ---------- .../ast/optimization/ConstantCollapser.java | 91 ------------------ .../ast/optimization/IOptimizationPass.java | 35 ------- .../ast/optimization/OperationCondenser.java | 107 --------------------- .../dicelang/ast/optimization/package-info.java | 7 -- 5 files changed, 290 deletions(-) delete mode 100644 dice-lang/src/main/java/bjc/dicelang/ast/optimization/ArithmeticCollapser.java delete mode 100644 dice-lang/src/main/java/bjc/dicelang/ast/optimization/ConstantCollapser.java delete mode 100644 dice-lang/src/main/java/bjc/dicelang/ast/optimization/IOptimizationPass.java delete mode 100644 dice-lang/src/main/java/bjc/dicelang/ast/optimization/OperationCondenser.java delete mode 100644 dice-lang/src/main/java/bjc/dicelang/ast/optimization/package-info.java (limited to 'dice-lang/src/main/java/bjc/dicelang/ast/optimization') 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 deleted file mode 100644 index 960fbf7..0000000 --- a/dice-lang/src/main/java/bjc/dicelang/ast/optimization/ArithmeticCollapser.java +++ /dev/null @@ -1,50 +0,0 @@ -package bjc.dicelang.ast.optimization; - -import java.util.function.BinaryOperator; - -import bjc.utils.funcdata.IList; -import bjc.utils.funcdata.ITree; -import bjc.utils.funcdata.Tree; - -import bjc.dicelang.ast.DiceASTUtils; -import bjc.dicelang.ast.nodes.DiceASTType; -import bjc.dicelang.ast.nodes.IDiceASTNode; -import bjc.dicelang.ast.nodes.ILiteralDiceNode; -import bjc.dicelang.ast.nodes.IntegerLiteralNode; -import bjc.dicelang.ast.nodes.OperatorDiceNode; - -class ArithmeticCollapser { - private BinaryOperator reducer; - private OperatorDiceNode type; - - public ArithmeticCollapser(BinaryOperator reducr, - OperatorDiceNode typ) { - reducer = reducr; - this.type = typ; - } - - public ITree collapse( - IList> children) { - boolean allConstant = children.allMatch((subtree) -> { - return subtree.transformHead((node) -> { - if (node.getType() == DiceASTType.LITERAL) { - return ((ILiteralDiceNode) node).canOptimize(); - } - - return false; - }); - }); - - if (!allConstant) { - return new Tree<>(type, children); - } - - int initState = DiceASTUtils.literalToInteger(children.first()); - - return children.tail().reduceAux(initState, - (currentNode, state) -> { - return reducer.apply(state, - 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 deleted file mode 100644 index 95badd2..0000000 --- a/dice-lang/src/main/java/bjc/dicelang/ast/optimization/ConstantCollapser.java +++ /dev/null @@ -1,91 +0,0 @@ -package bjc.dicelang.ast.optimization; - -import bjc.utils.funcdata.IList; -import bjc.utils.funcdata.ITree; -import bjc.utils.funcdata.Tree; - -import bjc.dicelang.ComplexDice; -import bjc.dicelang.ast.DiceASTUtils; -import bjc.dicelang.ast.nodes.IDiceASTNode; -import bjc.dicelang.ast.nodes.IntegerLiteralNode; -import bjc.dicelang.ast.nodes.OperatorDiceNode; - -/** - * Collapses operations with constants to constants - * - * @author ben - * - */ -public class ConstantCollapser implements IOptimizationPass { - private static final ArithmeticCollapser additionCollapser = new ArithmeticCollapser( - (left, right) -> left + right, OperatorDiceNode.ADD); - - private static final ArithmeticCollapser divideCollapser = new ArithmeticCollapser( - (left, right) -> left / right, OperatorDiceNode.DIVIDE); - - private static final ArithmeticCollapser multiplyCollapser = new ArithmeticCollapser( - (left, right) -> left * right, OperatorDiceNode.MULTIPLY); - - private static final ArithmeticCollapser subtractCollapser = new ArithmeticCollapser( - (left, right) -> left - right, OperatorDiceNode.SUBTRACT); - - private static final ArithmeticCollapser compoundCollapser = new ArithmeticCollapser( - (left, right) -> Integer.parseInt( - Integer.toString(left) + Integer.toString(left)), - OperatorDiceNode.COMPOUND); - - @Override - public ITree optimizeLeaf(IDiceASTNode leafNode) { - // We don't do anything special here - return new Tree<>(leafNode); - } - - @Override - public ITree optimizeOperator(IDiceASTNode operator, - IList> children) { - if (!operator.isOperator()) { - return new Tree<>(operator, children); - } - - switch ((OperatorDiceNode) operator) { - case ADD: - return additionCollapser.collapse(children); - case DIVIDE: - return divideCollapser.collapse(children); - case MULTIPLY: - return multiplyCollapser.collapse(children); - case SUBTRACT: - return subtractCollapser.collapse(children); - case COMPOUND: - return compoundCollapser.collapse(children); - case GROUP: - 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())); - } - - return new Tree<>(operator, children); - case ARRAY: - if (children.getSize() != 1) { - return new Tree<>(operator, children); - } - - return children.first(); - case ASSIGN: - case LET: - default: - // We don't optimize these operators - return new Tree<>(operator, children); - } - } -} diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/optimization/IOptimizationPass.java b/dice-lang/src/main/java/bjc/dicelang/ast/optimization/IOptimizationPass.java deleted file mode 100644 index 36b03f1..0000000 --- a/dice-lang/src/main/java/bjc/dicelang/ast/optimization/IOptimizationPass.java +++ /dev/null @@ -1,35 +0,0 @@ -package bjc.dicelang.ast.optimization; - -import bjc.utils.funcdata.IList; -import bjc.utils.funcdata.ITree; - -import bjc.dicelang.ast.nodes.IDiceASTNode; - -/** - * 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 - */ - public ITree optimizeLeaf(IDiceASTNode leafNode); - - /** - * Optimize an operator in an AST node - * - * @param operator - * The operator being optimized - * @param children - * The children of the operator being optimized - * @return The optimized node - */ - public ITree optimizeOperator(IDiceASTNode operator, - IList> children); -} diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/optimization/OperationCondenser.java b/dice-lang/src/main/java/bjc/dicelang/ast/optimization/OperationCondenser.java deleted file mode 100644 index f646a17..0000000 --- a/dice-lang/src/main/java/bjc/dicelang/ast/optimization/OperationCondenser.java +++ /dev/null @@ -1,107 +0,0 @@ -package bjc.dicelang.ast.optimization; - -import bjc.utils.data.IHolder; -import bjc.utils.data.Identity; -import bjc.utils.funcdata.ITree; -import bjc.utils.funcdata.TopDownTransformResult; -import bjc.utils.funcdata.Tree; - -import bjc.dicelang.ast.nodes.DiceASTType; -import bjc.dicelang.ast.nodes.IDiceASTNode; -import bjc.dicelang.ast.nodes.OperatorDiceNode; - -/** - * 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 - */ - public static ITree condense(ITree ast) { - return ast.topDownTransform(OperationCondenser::pickNode, - OperationCondenser::doCondense); - } - - private static ITree doCondense( - ITree ast) { - OperatorDiceNode operation = ast - .transformHead((node) -> (OperatorDiceNode) node); - - IHolder canCondense = new Identity<>(true); - - ast.doForChildren((child) -> { - if (canCondense.getValue()) { - canCondense.replace(child.transformHead((node) -> { - if (node.getType() == DiceASTType.OPERATOR) { - if (operation.equals(node)) { - return true; - } - - return false; - } - - return true; - })); - } - }); - - if (!canCondense.getValue()) { - return ast; - } - - ITree condensedAST = new Tree<>(operation); - - ast.doForChildren((child) -> { - if (child.getHead().getType() == DiceASTType.OPERATOR) { - child.doForChildren((subChild) -> { - condensedAST.addChild(subChild); - }); - } else { - condensedAST.addChild(child); - } - }); - - return condensedAST; - } - - private static TopDownTransformResult pickNode(IDiceASTNode node) { - switch (node.getType()) { - case LITERAL: - return TopDownTransformResult.SKIP; - case OPERATOR: - return pickOperator((OperatorDiceNode) node); - case VARIABLE: - return TopDownTransformResult.SKIP; - default: - throw new UnsupportedOperationException( - "Attempted to traverse unknown node type " + node); - } - } - - private static TopDownTransformResult pickOperator( - OperatorDiceNode node) { - switch (node) { - case ADD: - case MULTIPLY: - case SUBTRACT: - case DIVIDE: - case COMPOUND: - return TopDownTransformResult.PUSHDOWN; - case ARRAY: - case ASSIGN: - case GROUP: - case LET: - return TopDownTransformResult.PASSTHROUGH; - default: - throw new UnsupportedOperationException( - "Attempted to traverse unknown operator " + node); - } - } -} diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/optimization/package-info.java b/dice-lang/src/main/java/bjc/dicelang/ast/optimization/package-info.java deleted file mode 100644 index 6f75bf9..0000000 --- a/dice-lang/src/main/java/bjc/dicelang/ast/optimization/package-info.java +++ /dev/null @@ -1,7 +0,0 @@ -/** - * Contains optimizations on dice ASTs - * - * @author ben - * - */ -package bjc.dicelang.ast.optimization; \ No newline at end of file -- cgit v1.2.3