summaryrefslogtreecommitdiff
path: root/dice-lang/src/examples/java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-04-18 08:34:32 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-04-18 08:34:32 -0400
commit9ce39956fa1702f157c347dc4b8807d9b5dd2185 (patch)
treed981c0010a92660a1f0501431c4a3bc02d94e56d /dice-lang/src/examples/java
parent7c222f25d4b2d9f3b149d880f0e1acf8d673e4f5 (diff)
Reimplemented basic optimization.
Diffstat (limited to 'dice-lang/src/examples/java')
-rw-r--r--dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTLanguageTest.java34
-rw-r--r--dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTReferenceSanitizer.java28
2 files changed, 31 insertions, 31 deletions
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<String, DiceASTPragma> actions;
+ private static IFunctionalMap<String, DiceASTPragma> 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<IDiceASTNode> 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<IDiceASTNode> transformAST(
+ ITree<IDiceASTNode> builtAST,
+ IFunctionalMap<String, ITree<IDiceASTNode>> enviroment) {
+ ITree<IDiceASTNode> optimizedTree =
+ optimizer.optimizeTree(builtAST, enviroment);
+
+ ITree<IDiceASTNode> 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<IDiceASTNode> sanitize(ITree<IDiceASTNode> ast,
- IFunctionalMap<String, ITree<IDiceASTNode>> enviroment) {
- // TODO implement me
- return null;
- }
-}