From e7413128ff4e376997de6e94e4bea5eca14811ef Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Thu, 27 Oct 2016 21:56:18 -0400 Subject: Moved examples --- .../src/bjc/dicelang/ast/DiceASTOptimizer.java | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 dice-lang/src/bjc/dicelang/ast/DiceASTOptimizer.java (limited to 'dice-lang/src/bjc/dicelang/ast/DiceASTOptimizer.java') diff --git a/dice-lang/src/bjc/dicelang/ast/DiceASTOptimizer.java b/dice-lang/src/bjc/dicelang/ast/DiceASTOptimizer.java new file mode 100644 index 0000000..d7fc23c --- /dev/null +++ b/dice-lang/src/bjc/dicelang/ast/DiceASTOptimizer.java @@ -0,0 +1,60 @@ +package bjc.dicelang.ast; + +import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.IList; +import bjc.utils.funcdata.IMap; +import bjc.utils.funcdata.ITree; + +import bjc.dicelang.ast.nodes.IDiceASTNode; +import bjc.dicelang.ast.optimization.IOptimizationPass; + +/** + * Contains optimizations appliable to a dice AST + * + * @author ben + * + */ +public class DiceASTOptimizer { + private IList passes; + + /** + * Create a new optimizer + */ + public DiceASTOptimizer() { + passes = new FunctionalList<>(); + } + + /** + * Add a pass to the list of optimization passes + * + * @param pass + * The pass to add + */ + public void addPass(IOptimizationPass pass) { + passes.add(pass); + } + + /** + * Optimize the passed in tree + * + * @param ast + * The tree to optimize + * @param enviroment + * The enviroment for variable references + * @return The optimized tree + */ + public ITree optimizeTree(ITree ast, + IMap> enviroment) { + ITree optimizedTree = passes.reduceAux(ast, + (currentPass, currentTree) -> { + return currentTree.collapse(currentPass::optimizeLeaf, + (operator) -> { + return (nodes) -> { + return currentPass.optimizeOperator( + operator, nodes); + }; + }, (tree) -> tree); + }, (tree) -> tree); + return optimizedTree; + } +} -- cgit v1.2.3