From 9ce39956fa1702f157c347dc4b8807d9b5dd2185 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 18 Apr 2016 08:34:32 -0400 Subject: Reimplemented basic optimization. --- .../java/bjc/dicelang/ast/DiceASTOptimizer.java | 57 ++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 dice-lang/src/main/java/bjc/dicelang/ast/DiceASTOptimizer.java (limited to 'dice-lang/src/main/java/bjc/dicelang/ast/DiceASTOptimizer.java') diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTOptimizer.java b/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTOptimizer.java new file mode 100644 index 0000000..00ac871 --- /dev/null +++ b/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTOptimizer.java @@ -0,0 +1,57 @@ +package bjc.dicelang.ast; + +import bjc.dicelang.ast.nodes.IDiceASTNode; +import bjc.dicelang.ast.optimization.IOptimizationPass; +import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.IFunctionalList; +import bjc.utils.funcdata.IFunctionalMap; +import bjc.utils.funcdata.ITree; + +/** + * Contains optimizations appliable to a dice AST + * + * @author ben + * + */ +public class DiceASTOptimizer { + private IFunctionalList 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, + IFunctionalMap> enviroment) { + return passes.reduceAux(ast, (currentPass, currentTree) -> { + return currentTree.collapse(currentPass::optimizeLeaf, + (operator) -> { + return (nodes) -> { + return currentPass.optimizeOperator(operator, + nodes); + }; + }, (tree) -> tree); + }, (tree) -> tree); + } +} -- cgit v1.2.3