diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-10-27 21:56:18 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-10-27 22:12:47 -0400 |
| commit | e7413128ff4e376997de6e94e4bea5eca14811ef (patch) | |
| tree | 0749e270fdb754d04dc223abd95d47436508047f /dice-lang/src/main/java/bjc/dicelang/ast/optimization/OperationCondenser.java | |
| parent | e13a6981bd278c2cfc3b5ecb2517367b117f7a52 (diff) | |
Moved examples
Diffstat (limited to 'dice-lang/src/main/java/bjc/dicelang/ast/optimization/OperationCondenser.java')
| -rw-r--r-- | dice-lang/src/main/java/bjc/dicelang/ast/optimization/OperationCondenser.java | 107 |
1 files changed, 0 insertions, 107 deletions
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<IDiceASTNode> condense(ITree<IDiceASTNode> ast) { - return ast.topDownTransform(OperationCondenser::pickNode, - OperationCondenser::doCondense); - } - - private static ITree<IDiceASTNode> doCondense( - ITree<IDiceASTNode> ast) { - OperatorDiceNode operation = ast - .transformHead((node) -> (OperatorDiceNode) node); - - IHolder<Boolean> 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<IDiceASTNode> 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); - } - } -} |
