diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-10 22:05:24 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-10 22:05:24 -0400 |
| commit | 75fe5c351630ce26a9bd4db631809bb8d5b26357 (patch) | |
| tree | 90137a8470012b6e6a58118663c904c429311699 /dice-lang/src/examples/java/bjc | |
| parent | 6f42b318ee33152fb9f92e576706e7a368829d2e (diff) | |
Restored basic functionality with new system
Diffstat (limited to 'dice-lang/src/examples/java/bjc')
| -rw-r--r-- | dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTLanguageTest.java | 90 | ||||
| -rw-r--r-- | dice-lang/src/examples/java/bjc/dicelang/examples/DiceExpressionPreparer.java | 61 |
2 files changed, 151 insertions, 0 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 new file mode 100644 index 0000000..94186dd --- /dev/null +++ b/dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTLanguageTest.java @@ -0,0 +1,90 @@ +package bjc.dicelang.examples; + +import java.util.Scanner; + +import bjc.dicelang.ast.DiceASTEvaluator; +import bjc.dicelang.ast.DiceASTParser; +import bjc.dicelang.ast.nodes.IDiceASTNode; + +import bjc.utils.funcdata.FunctionalMap; +import bjc.utils.funcdata.IFunctionalList; +import bjc.utils.funcdata.IFunctionalMap; +import bjc.utils.parserutils.AST; + +/** + * Test interface for AST-based dice language + * + * @author ben + * + */ +public class DiceASTLanguageTest { + /** + * Main method of class + * + * @param args + * Unused CLI args + */ + public static void main(String[] args) { + Scanner inputSource = new Scanner(System.in); + int commandNumber = 0; + + System.out.print("dice-lang-" + commandNumber + "> "); + String currentLine = inputSource.nextLine(); + + // The enviroment for variables + IFunctionalMap<String, AST<IDiceASTNode>> enviroment = + new FunctionalMap<>(); + + while (!currentLine.equalsIgnoreCase("quit")) { + // Build an AST from the string expression + AST<IDiceASTNode> builtAST; + + IFunctionalList<String> preparedTokens = + DiceExpressionPreparer.prepareCommand(currentLine); + + try { + builtAST = DiceASTParser.createFromString(preparedTokens); + } catch (IllegalStateException isex) { + System.out.println("ERROR: " + isex.getLocalizedMessage()); + + currentLine = getNextCommand(inputSource, commandNumber); + + continue; + } + + int sampleRoll; + + try { + sampleRoll = + DiceASTEvaluator.evaluateAST(builtAST, enviroment); + } catch (UnsupportedOperationException usex) { + System.out.println("ERROR: " + usex.getLocalizedMessage()); + + currentLine = getNextCommand(inputSource, commandNumber); + + continue; + } + + // Print out results + System.out.println("\tParsed: " + builtAST.toString()); + System.out.println("\t\tSample Roll: " + sampleRoll); + + // Increase the number of commands + commandNumber++; + + currentLine = getNextCommand(inputSource, commandNumber); + } + + System.out.println("Bye."); + + // Cleanup after ourselves + inputSource.close(); + } + + private static String getNextCommand(Scanner inputSource, + int commandNumber) { + System.out.print("dice-lang-" + commandNumber + "> "); + + return inputSource.nextLine(); + } +} diff --git a/dice-lang/src/examples/java/bjc/dicelang/examples/DiceExpressionPreparer.java b/dice-lang/src/examples/java/bjc/dicelang/examples/DiceExpressionPreparer.java new file mode 100644 index 0000000..1d46cc9 --- /dev/null +++ b/dice-lang/src/examples/java/bjc/dicelang/examples/DiceExpressionPreparer.java @@ -0,0 +1,61 @@ +package bjc.dicelang.examples; + +import java.util.Deque; +import java.util.LinkedList; + +import bjc.utils.data.IPair; +import bjc.utils.data.Pair; +import bjc.utils.funcdata.FunctionalStringTokenizer; +import bjc.utils.funcdata.IFunctionalList; +import bjc.utils.funcutils.ListUtils; +import bjc.utils.parserutils.ShuntingYard; + +/** + * Prepare a dice expression to be parsed + * + * @author ben + * + */ +public class DiceExpressionPreparer { + /** + * The yard to use for shunting expressions + */ + private static ShuntingYard<String> yard; + + static { + yard = new ShuntingYard<>(); + + yard.addOp("d", 5); // dice operator: use for creating variable + // size dice groups + yard.addOp("c", 6); // compound operator: use for creating compound + // dice from expressions + yard.addOp(":=", 0); // binding operator: Bind a name to a variable + // expression + } + + static IFunctionalList<String> prepareCommand(String currentLine) { + IFunctionalList<String> tokens = + FunctionalStringTokenizer.fromString(currentLine).toList(); + + Deque<IPair<String, String>> ops = new LinkedList<>(); + + ops.add(new Pair<>("+", "\\+")); + ops.add(new Pair<>("-", "-")); + ops.add(new Pair<>("*", "\\*")); + ops.add(new Pair<>("/", "/")); + ops.add(new Pair<>(":=", ":=")); + + IFunctionalList<String> semiExpandedTokens = + ListUtils.splitTokens(tokens, ops); + + ops = new LinkedList<>(); + + ops.add(new Pair<>("(", "\\(")); + ops.add(new Pair<>(")", "\\)")); + + IFunctionalList<String> fullyExpandedTokens = + ListUtils.deAffixTokens(semiExpandedTokens, ops); + + return yard.postfix(fullyExpandedTokens, (token) -> token); + } +} |
