From 9ce39956fa1702f157c347dc4b8807d9b5dd2185 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 18 Apr 2016 08:34:32 -0400 Subject: Reimplemented basic optimization. --- .../bjc/dicelang/examples/DiceASTLanguageTest.java | 34 ++++++++++++++++++++-- .../examples/DiceASTReferenceSanitizer.java | 28 ------------------ 2 files changed, 31 insertions(+), 31 deletions(-) delete mode 100644 dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTReferenceSanitizer.java (limited to 'dice-lang/src/examples/java') diff --git a/dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTLanguageTest.java b/dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTLanguageTest.java index 5dbaff3..8d87aee 100644 --- a/dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTLanguageTest.java +++ b/dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTLanguageTest.java @@ -4,8 +4,11 @@ import java.util.Scanner; import bjc.dicelang.ast.DiceASTEvaluator; import bjc.dicelang.ast.DiceASTInliner; +import bjc.dicelang.ast.DiceASTOptimizer; import bjc.dicelang.ast.DiceASTParser; +import bjc.dicelang.ast.DiceASTReferenceSanitizer; import bjc.dicelang.ast.nodes.IDiceASTNode; +import bjc.dicelang.ast.optimization.ConstantCollapser; import bjc.utils.funcdata.FunctionalMap; import bjc.utils.funcdata.FunctionalStringTokenizer; import bjc.utils.funcdata.IFunctionalList; @@ -19,7 +22,9 @@ import bjc.utils.funcdata.ITree; * */ public class DiceASTLanguageTest { - private static IFunctionalMap actions; + private static IFunctionalMap actions; + + private static DiceASTOptimizer optimizer; static { actions = new FunctionalMap<>(); @@ -31,6 +36,10 @@ public class DiceASTLanguageTest { System.out.println(varName + " is bound to " + varValue); }); }); + + optimizer = new DiceASTOptimizer(); + + optimizer.addPass(new ConstantCollapser()); } private static void handleInlineAction( @@ -117,9 +126,14 @@ public class DiceASTLanguageTest { int sampleRoll; + ITree transformedAST = + transformAST(builtAST, enviroment); + try { - sampleRoll = - DiceASTEvaluator.evaluateAST(builtAST, enviroment); + sampleRoll = DiceASTEvaluator.evaluateAST(transformedAST, + enviroment); + + enviroment.put("last", transformedAST); } catch (UnsupportedOperationException usex) { System.out.println("ERROR: " + usex.getLocalizedMessage()); @@ -130,6 +144,8 @@ public class DiceASTLanguageTest { // Print out results System.out.println("\tParsed: " + builtAST.toString()); + System.out + .println("\tEvaluated: " + transformedAST.toString()); System.out.println("\t\tSample Roll: " + sampleRoll); // Increase the number of commands @@ -144,6 +160,18 @@ public class DiceASTLanguageTest { inputSource.close(); } + private static ITree transformAST( + ITree builtAST, + IFunctionalMap> enviroment) { + ITree optimizedTree = + optimizer.optimizeTree(builtAST, enviroment); + + ITree sanitizedTree = DiceASTReferenceSanitizer + .sanitize(optimizedTree, enviroment); + + return sanitizedTree; + } + private static String getNextCommand(Scanner inputSource, int commandNumber) { System.out.print("\ndice-lang-" + commandNumber + "> "); diff --git a/dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTReferenceSanitizer.java b/dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTReferenceSanitizer.java deleted file mode 100644 index b2e441d..0000000 --- a/dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTReferenceSanitizer.java +++ /dev/null @@ -1,28 +0,0 @@ -package bjc.dicelang.examples; - -import bjc.dicelang.ast.nodes.IDiceASTNode; -import bjc.utils.funcdata.IFunctionalMap; -import bjc.utils.funcdata.ITree; - -/** - * Sanitize the references in an AST so that a variable that refers to - * itself in its definition has the occurance of it replaced with its - * previous definition - * - * @author ben - * - */ -public class DiceASTReferenceSanitizer { - /** - * Sanitize the references in an AST - * - * @param ast - * @param enviroment - * @return The sanitized AST - */ - public static ITree sanitize(ITree ast, - IFunctionalMap> enviroment) { - // TODO implement me - return null; - } -} -- cgit v1.2.3