From 75fe5c351630ce26a9bd4db631809bb8d5b26357 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 10 Apr 2016 22:05:24 -0400 Subject: Restored basic functionality with new system --- .../bjc/dicelang/examples/DiceASTLanguageTest.java | 90 ++++++++++++++++++++++ .../dicelang/examples/DiceExpressionPreparer.java | 61 +++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTLanguageTest.java create mode 100644 dice-lang/src/examples/java/bjc/dicelang/examples/DiceExpressionPreparer.java (limited to 'dice-lang/src/examples/java/bjc') 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> enviroment = + new FunctionalMap<>(); + + while (!currentLine.equalsIgnoreCase("quit")) { + // Build an AST from the string expression + AST builtAST; + + IFunctionalList 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 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 prepareCommand(String currentLine) { + IFunctionalList tokens = + FunctionalStringTokenizer.fromString(currentLine).toList(); + + Deque> ops = new LinkedList<>(); + + ops.add(new Pair<>("+", "\\+")); + ops.add(new Pair<>("-", "-")); + ops.add(new Pair<>("*", "\\*")); + ops.add(new Pair<>("/", "/")); + ops.add(new Pair<>(":=", ":=")); + + IFunctionalList semiExpandedTokens = + ListUtils.splitTokens(tokens, ops); + + ops = new LinkedList<>(); + + ops.add(new Pair<>("(", "\\(")); + ops.add(new Pair<>(")", "\\)")); + + IFunctionalList fullyExpandedTokens = + ListUtils.deAffixTokens(semiExpandedTokens, ops); + + return yard.postfix(fullyExpandedTokens, (token) -> token); + } +} -- cgit v1.2.3