summaryrefslogtreecommitdiff
path: root/dice-lang/src/examples/java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-04-17 20:41:47 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-04-17 20:41:47 -0400
commit7c222f25d4b2d9f3b149d880f0e1acf8d673e4f5 (patch)
treea07cd6b07522d9ffe24a82c5a1a487d307875561 /dice-lang/src/examples/java
parentd9437c1d328ccc2b26bd0aae19c2aff7140e466b (diff)
Fixed a bug with arithmetic operators
Diffstat (limited to 'dice-lang/src/examples/java')
-rw-r--r--dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTLanguageTest.java67
-rw-r--r--dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTPragma.java19
-rw-r--r--dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTReferenceSanitizer.java28
-rw-r--r--dice-lang/src/examples/java/bjc/dicelang/examples/DiceExpressionPreparer.java7
4 files changed, 119 insertions, 2 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 18f0562..5dbaff3 100644
--- a/dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTLanguageTest.java
+++ b/dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTLanguageTest.java
@@ -3,10 +3,11 @@ package bjc.dicelang.examples;
import java.util.Scanner;
import bjc.dicelang.ast.DiceASTEvaluator;
+import bjc.dicelang.ast.DiceASTInliner;
import bjc.dicelang.ast.DiceASTParser;
import bjc.dicelang.ast.nodes.IDiceASTNode;
-
import bjc.utils.funcdata.FunctionalMap;
+import bjc.utils.funcdata.FunctionalStringTokenizer;
import bjc.utils.funcdata.IFunctionalList;
import bjc.utils.funcdata.IFunctionalMap;
import bjc.utils.funcdata.ITree;
@@ -18,6 +19,49 @@ import bjc.utils.funcdata.ITree;
*
*/
public class DiceASTLanguageTest {
+ private static IFunctionalMap<String, DiceASTPragma> actions;
+
+ static {
+ actions = new FunctionalMap<>();
+
+ actions.put("inline", DiceASTLanguageTest::handleInlineAction);
+
+ actions.put("env", (tokenizer, enviroment) -> {
+ enviroment.forEach((varName, varValue) -> {
+ System.out.println(varName + " is bound to " + varValue);
+ });
+ });
+ }
+
+ private static void handleInlineAction(
+ FunctionalStringTokenizer tokenizer,
+ IFunctionalMap<String, ITree<IDiceASTNode>> enviroment) {
+ // Skip the pragma name
+ tokenizer.nextToken();
+
+ IFunctionalList<String> pragmaArgs = tokenizer.toList();
+
+ if (pragmaArgs.getSize() < 3) {
+ System.err.println(
+ "ERROR: Inline requires at least 3 parameters. They are:"
+ + "\n\t1. The name of the expression to inline."
+ + "\n\t2. The name of the variable to bind the result to."
+ + "\n\t3 and onwards. Names of variables to inline in the expression.");
+ } else {
+ String inlineExpression = pragmaArgs.getByIndex(0);
+ String variableName = pragmaArgs.getByIndex(1);
+
+ IFunctionalList<String> inlinedVariables =
+ pragmaArgs.tail().tail();
+
+ ITree<IDiceASTNode> inlinedExpression = DiceASTInliner
+ .selectiveInline(enviroment.get(inlineExpression),
+ enviroment, inlinedVariables);
+
+ enviroment.put(variableName, inlinedExpression);
+ }
+ }
+
/**
* Main method of class
*
@@ -36,6 +80,25 @@ public class DiceASTLanguageTest {
new FunctionalMap<>();
while (!currentLine.equalsIgnoreCase("quit")) {
+ String possibleActionName = currentLine.split(" ")[0];
+
+ if (actions.containsKey(possibleActionName)) {
+ System.err.println(
+ "\nTRACE: Executing action " + possibleActionName
+ + " with line " + currentLine + "\n");
+
+ // Execute action
+ FunctionalStringTokenizer tokenizer =
+ new FunctionalStringTokenizer(currentLine);
+
+ actions.get(possibleActionName).accept(tokenizer,
+ enviroment);
+
+ currentLine = getNextCommand(inputSource, commandNumber);
+
+ continue;
+ }
+
// Build an AST from the string expression
ITree<IDiceASTNode> builtAST;
@@ -83,7 +146,7 @@ public class DiceASTLanguageTest {
private static String getNextCommand(Scanner inputSource,
int commandNumber) {
- System.out.print("dice-lang-" + commandNumber + "> ");
+ System.out.print("\ndice-lang-" + commandNumber + "> ");
return inputSource.nextLine();
}
diff --git a/dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTPragma.java b/dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTPragma.java
new file mode 100644
index 0000000..67b0adc
--- /dev/null
+++ b/dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTPragma.java
@@ -0,0 +1,19 @@
+package bjc.dicelang.examples;
+
+import java.util.function.BiConsumer;
+
+import bjc.dicelang.ast.nodes.IDiceASTNode;
+import bjc.utils.funcdata.FunctionalStringTokenizer;
+import bjc.utils.funcdata.IFunctionalMap;
+import bjc.utils.funcdata.ITree;
+
+/**
+ * Alias for the type of a 'pragma' or special language command
+ *
+ * @author ben
+ *
+ */
+public interface DiceASTPragma extends
+ BiConsumer<FunctionalStringTokenizer, IFunctionalMap<String, ITree<IDiceASTNode>>> {
+ // Just an alias
+}
diff --git a/dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTReferenceSanitizer.java b/dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTReferenceSanitizer.java
new file mode 100644
index 0000000..b2e441d
--- /dev/null
+++ b/dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTReferenceSanitizer.java
@@ -0,0 +1,28 @@
+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;
+ }
+}
diff --git a/dice-lang/src/examples/java/bjc/dicelang/examples/DiceExpressionPreparer.java b/dice-lang/src/examples/java/bjc/dicelang/examples/DiceExpressionPreparer.java
index 1d46cc9..91abf7d 100644
--- a/dice-lang/src/examples/java/bjc/dicelang/examples/DiceExpressionPreparer.java
+++ b/dice-lang/src/examples/java/bjc/dicelang/examples/DiceExpressionPreparer.java
@@ -52,10 +52,17 @@ public class DiceExpressionPreparer {
ops.add(new Pair<>("(", "\\("));
ops.add(new Pair<>(")", "\\)"));
+ ops.add(new Pair<>("+", "\\+"));
+ ops.add(new Pair<>("-", "-"));
+ ops.add(new Pair<>("*", "\\*"));
+ ops.add(new Pair<>("/", "/"));
+ ops.add(new Pair<>(":=", ":="));
IFunctionalList<String> fullyExpandedTokens =
ListUtils.deAffixTokens(semiExpandedTokens, ops);
+ fullyExpandedTokens.removeIf((strang) -> strang.equals(""));
+
return yard.postfix(fullyExpandedTokens, (token) -> token);
}
}