diff options
| author | EVE <EVE@EVE-PC> | 2017-03-13 16:41:45 -0400 |
|---|---|---|
| committer | EVE <EVE@EVE-PC> | 2017-03-13 16:41:45 -0400 |
| commit | 01136c6796e21f023713e026674576d8e623462d (patch) | |
| tree | e77886fe0e0adaf3c0430fba9ce248ef83f74fe4 /dice-lang/src/bjc/dicelang/v1/examples | |
| parent | 870d769cfc152171d27b2331a7c590d0b307ad48 (diff) | |
Formatting
Diffstat (limited to 'dice-lang/src/bjc/dicelang/v1/examples')
6 files changed, 64 insertions, 95 deletions
diff --git a/dice-lang/src/bjc/dicelang/v1/examples/DiceASTLanguageTest.java b/dice-lang/src/bjc/dicelang/v1/examples/DiceASTLanguageTest.java index 117bc98..f20e276 100644 --- a/dice-lang/src/bjc/dicelang/v1/examples/DiceASTLanguageTest.java +++ b/dice-lang/src/bjc/dicelang/v1/examples/DiceASTLanguageTest.java @@ -25,9 +25,9 @@ import bjc.utils.funcdata.IMap; * */ public class DiceASTLanguageTest { - private static IMap<String, DiceASTPragma> actions; + private static IMap<String, DiceASTPragma> actions; - private static DiceASTOptimizer optimizer; + private static DiceASTOptimizer optimizer; // Set up things that need to be configured static { @@ -42,7 +42,7 @@ public class DiceASTLanguageTest { System.out.println(varName + " is bound to " + varValue); }); }); - + // Create and configure the optimizer optimizer = new DiceASTOptimizer(); @@ -50,8 +50,7 @@ public class DiceASTLanguageTest { } // Read in a command - private static String getNextCommand(Scanner inputSource, - int commandNumber) { + private static String getNextCommand(Scanner inputSource, int commandNumber) { // Print a prompt using the current command number System.out.print("\ndice-lang-" + commandNumber + "> "); @@ -59,8 +58,7 @@ public class DiceASTLanguageTest { return inputSource.nextLine(); } - private static void handleInlineAction( - FunctionalStringTokenizer tokenizer, + private static void handleInlineAction(FunctionalStringTokenizer tokenizer, IMap<String, ITree<IDiceASTNode>> enviroment) { // Skip the pragma name tokenizer.nextToken(); @@ -70,11 +68,10 @@ public class DiceASTLanguageTest { if (pragmaArgs.getSize() < 3) { // Complain about pragma arguments not being valid - 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."); + 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 { // Get arguments String inlineExpression = pragmaArgs.getByIndex(0); @@ -84,9 +81,8 @@ public class DiceASTLanguageTest { IList<String> inlinedVariables = pragmaArgs.tail().tail(); // Actually inline the variable - ITree<IDiceASTNode> inlinedExpression = DiceASTInliner - .selectiveInline(enviroment.get(inlineExpression), - enviroment, inlinedVariables); + ITree<IDiceASTNode> inlinedExpression = DiceASTInliner.selectiveInline( + enviroment.get(inlineExpression), enviroment, inlinedVariables); // Stick the inlined variable into the enviroment enviroment.put(variableName, inlinedExpression); @@ -97,7 +93,7 @@ public class DiceASTLanguageTest { * Main method of class * * @param args - * Unused CLI args + * Unused CLI args */ public static void main(String[] args) { // Prepare the things we need for input @@ -118,12 +114,10 @@ public class DiceASTLanguageTest { // Check and see if we're executing an action if (actions.containsKey(possibleActionName)) { // Execute action - FunctionalStringTokenizer tokenizer = new FunctionalStringTokenizer( - currentLine); + FunctionalStringTokenizer tokenizer = new FunctionalStringTokenizer(currentLine); // Execute the action - actions.get(possibleActionName).accept(tokenizer, - enviroment); + actions.get(possibleActionName).accept(tokenizer, enviroment); // Get the next command currentLine = getNextCommand(inputSource, commandNumber); @@ -138,11 +132,9 @@ public class DiceASTLanguageTest { long time = System.nanoTime(); // Prepare the command - IList<String> preparedTokens = DiceExpressionPreparer - .prepareCommand(currentLine); + IList<String> preparedTokens = DiceExpressionPreparer.prepareCommand(currentLine); - System.out.println("Command prepared in " - + (double) (System.nanoTime() - time) / 1000000000 + System.out.println("Command prepared in " + (double) (System.nanoTime() - time) / 1000000000 + " s"); try { @@ -152,14 +144,9 @@ public class DiceASTLanguageTest { // Create the AST builtAST = DiceASTParser.createFromString(preparedTokens); - System.out - .println( - "Command parsed in " - + (double) (System.nanoTime() - - time) / 1000000000 - + " s"); - } catch (InputMismatchException | IllegalStateException - | UnsupportedOperationException ex) { + System.out.println("Command parsed in " + + (double) (System.nanoTime() - time) / 1000000000 + " s"); + } catch (InputMismatchException | IllegalStateException | UnsupportedOperationException ex) { // Tell the user there was an error in parsing System.out.println("PARSING ERROR: " + ex.getLocalizedMessage()); @@ -176,40 +163,33 @@ public class DiceASTLanguageTest { time = System.nanoTime(); // Transform the AST - ITree<IDiceASTNode> transformedAST = transformAST(builtAST, - enviroment); + ITree<IDiceASTNode> transformedAST = transformAST(builtAST, enviroment); - System.out.println("Command transformed in " - + (double) (System.nanoTime() - time) / 1000000000 + System.out.println("Command transformed in " + (double) (System.nanoTime() - time) / 1000000000 + " s"); // Print out the transformed AST - System.out - .println("\tTransformed: " + transformedAST.toString()); - + System.out.println("\tTransformed: " + transformedAST.toString()); try { // Time the evaluation time = System.nanoTime(); // Evaluate the expression once - IResult sampleResult = DiceASTEvaluator.evaluateAST(transformedAST, - enviroment); + IResult sampleResult = DiceASTEvaluator.evaluateAST(transformedAST, enviroment); - System.out - .println( - "Command evaluated in " - + (double) (System.nanoTime() - - time) / 1000000000 - + " s"); + System.out.println("Command evaluated in " + + (double) (System.nanoTime() - time) / 1000000000 + " s"); - // Print out the result of evaluating the expression + // Print out the result of evaluating the + // expression System.out.println("\t\tSample Result: " + sampleResult); - + // Update the 'last' meta-variable enviroment.put("last", transformedAST); } catch (UnsupportedOperationException usex) { - // Tell the user there was an error in evaluation + // Tell the user there was an error in + // evaluation System.out.println("EVALUATION ERROR: " + usex.getLocalizedMessage()); // Get the next command @@ -219,7 +199,6 @@ public class DiceASTLanguageTest { continue; } - // Increase the number of commands commandNumber++; @@ -234,20 +213,16 @@ public class DiceASTLanguageTest { } // Transform a parsed AST - private static ITree<IDiceASTNode> transformAST( - ITree<IDiceASTNode> builtAST, + private static ITree<IDiceASTNode> transformAST(ITree<IDiceASTNode> builtAST, IMap<String, ITree<IDiceASTNode>> enviroment) { // Optimize the tree first - ITree<IDiceASTNode> optimizedTree = optimizer - .optimizeTree(builtAST, enviroment); + ITree<IDiceASTNode> optimizedTree = optimizer.optimizeTree(builtAST, enviroment); // Then, condense unnecessary operations - ITree<IDiceASTNode> condensedTree = OperationCondenser - .condense(optimizedTree); + ITree<IDiceASTNode> condensedTree = OperationCondenser.condense(optimizedTree); // Next, sanitize references - ITree<IDiceASTNode> sanitizedTree = DiceASTReferenceSanitizer - .sanitize(condensedTree, enviroment); + ITree<IDiceASTNode> sanitizedTree = DiceASTReferenceSanitizer.sanitize(condensedTree, enviroment); // Re-optimize the sanitized & condensed tree optimizedTree = optimizer.optimizeTree(sanitizedTree, enviroment); diff --git a/dice-lang/src/bjc/dicelang/v1/examples/DiceASTPragma.java b/dice-lang/src/bjc/dicelang/v1/examples/DiceASTPragma.java index 2fe9d30..a29b8b5 100644 --- a/dice-lang/src/bjc/dicelang/v1/examples/DiceASTPragma.java +++ b/dice-lang/src/bjc/dicelang/v1/examples/DiceASTPragma.java @@ -10,12 +10,12 @@ import bjc.utils.funcdata.IMap; /** * Alias for the type of a 'pragma' or special language command * - * To explain it, a pragma is a function that takes a tokenizer with the rest - * of the line, and an enviroment that contains variable bindings + * To explain it, a pragma is a function that takes a tokenizer with the rest of + * the line, and an enviroment that contains variable bindings + * * @author ben * */ -public interface DiceASTPragma extends - BiConsumer<FunctionalStringTokenizer, IMap<String, ITree<IDiceASTNode>>> { +public interface DiceASTPragma extends BiConsumer<FunctionalStringTokenizer, IMap<String, ITree<IDiceASTNode>>> { // Just an alias } diff --git a/dice-lang/src/bjc/dicelang/v1/examples/DiceExpressionParserTest.java b/dice-lang/src/bjc/dicelang/v1/examples/DiceExpressionParserTest.java index 68a0da5..adffc69 100644 --- a/dice-lang/src/bjc/dicelang/v1/examples/DiceExpressionParserTest.java +++ b/dice-lang/src/bjc/dicelang/v1/examples/DiceExpressionParserTest.java @@ -17,7 +17,7 @@ public class DiceExpressionParserTest { * Run the parser test * * @param args - * Unused CLI arguments + * Unused CLI arguments */ public static void main(String[] args) { /* @@ -39,8 +39,7 @@ public class DiceExpressionParserTest { int nTimes = Integer.parseInt(scn.nextLine()); - IDiceExpression dexp = DiceExpressionParser.parse(exp, - new HashMap<>()); + IDiceExpression dexp = DiceExpressionParser.parse(exp, new HashMap<>()); /* * Roll the dice a specified amount of times diff --git a/dice-lang/src/bjc/dicelang/v1/examples/DiceExpressionPreparer.java b/dice-lang/src/bjc/dicelang/v1/examples/DiceExpressionPreparer.java index f498f51..121847c 100644 --- a/dice-lang/src/bjc/dicelang/v1/examples/DiceExpressionPreparer.java +++ b/dice-lang/src/bjc/dicelang/v1/examples/DiceExpressionPreparer.java @@ -20,11 +20,11 @@ public class DiceExpressionPreparer { /** * The yard to use for shunting expressions */ - private static ShuntingYard<String> yard; + private static ShuntingYard<String> yard; - private static final int MATH_PREC = 20; - private static final int DICE_PREC = 10; - private static final int EXPR_PREC = 0; + private static final int MATH_PREC = 20; + private static final int DICE_PREC = 10; + private static final int EXPR_PREC = 0; // Do initialization for all parsers static { @@ -39,29 +39,29 @@ public class DiceExpressionPreparer { yard.addOp("*", 1 + MATH_PREC); yard.addOp("/", 1 + MATH_PREC); - yard.addOp("d", 0 + DICE_PREC); // dice operator: use for creating + yard.addOp("d", 0 + DICE_PREC); // dice operator: use for + // creating // variable size dice groups yard.addOp("c", 1 + DICE_PREC); // compound operator: use for // creating compound dice from expressions yard.addOp("=>", 0 + EXPR_PREC); // let operator: evaluate an // expression in the context of another - yard.addOp(":=", 1 + EXPR_PREC); // binding operator: Bind a name + yard.addOp(":=", 1 + EXPR_PREC); // binding operator: Bind a + // name // to a variable expression } /** - * Prepare a command, turning raw tokens into input for the tree - * builder + * Prepare a command, turning raw tokens into input for the tree builder * * @param currentLine - * The command to prepare + * The command to prepare * @return A stream of tokens representing the command */ public static IList<String> prepareCommand(String currentLine) { // Split the command into tokens - IList<String> tokens = FunctionalStringTokenizer - .fromString(currentLine).toList(); + IList<String> tokens = FunctionalStringTokenizer.fromString(currentLine).toList(); // The linked list to use for handling tokens Deque<IPair<String, String>> ops = new LinkedList<>(); @@ -75,8 +75,7 @@ public class DiceExpressionPreparer { ops.add(new Pair<>("=>", "=>")); // Expand infix single tokens to multiple infix tokens - IList<String> semiExpandedTokens = ListUtils.splitTokens(tokens, - ops); + IList<String> semiExpandedTokens = ListUtils.splitTokens(tokens, ops); // Reinitialize the list ops = new LinkedList<>(); @@ -88,8 +87,7 @@ public class DiceExpressionPreparer { ops.add(new Pair<>("]", "\\]")); // Deaffix ('s and ['s from tokens - IList<String> fullyExpandedTokens = ListUtils - .deAffixTokens(semiExpandedTokens, ops); + IList<String> fullyExpandedTokens = ListUtils.deAffixTokens(semiExpandedTokens, ops); // Remove blank tokens fullyExpandedTokens.removeIf((strang) -> strang.equals("")); diff --git a/dice-lang/src/bjc/dicelang/v1/examples/DiceLanguageState.java b/dice-lang/src/bjc/dicelang/v1/examples/DiceLanguageState.java index e402d73..e2a71b4 100644 --- a/dice-lang/src/bjc/dicelang/v1/examples/DiceLanguageState.java +++ b/dice-lang/src/bjc/dicelang/v1/examples/DiceLanguageState.java @@ -12,8 +12,7 @@ import bjc.utils.data.Pair; * @author ben * */ -public class DiceLanguageState - extends Pair<DiceExpressionParser, Map<String, IDiceExpression>> { +public class DiceLanguageState extends Pair<DiceExpressionParser, Map<String, IDiceExpression>> { /** * Create a new state @@ -25,12 +24,11 @@ public class DiceLanguageState * Create a new state with the desired parameters * * @param left - * The parser to use + * The parser to use * @param right - * The enviroment to use + * The enviroment to use */ - public DiceLanguageState(DiceExpressionParser left, - Map<String, IDiceExpression> right) { + public DiceLanguageState(DiceExpressionParser left, Map<String, IDiceExpression> right) { super(left, right); } } diff --git a/dice-lang/src/bjc/dicelang/v1/examples/DiceLanguageTest.java b/dice-lang/src/bjc/dicelang/v1/examples/DiceLanguageTest.java index 257e646..2d90318 100644 --- a/dice-lang/src/bjc/dicelang/v1/examples/DiceLanguageTest.java +++ b/dice-lang/src/bjc/dicelang/v1/examples/DiceLanguageTest.java @@ -28,7 +28,7 @@ public class DiceLanguageTest { * Main method * * @param args - * Unused CLI args + * Unused CLI args */ public static void main(String[] args) { Scanner scn = new Scanner(System.in); @@ -67,25 +67,24 @@ public class DiceLanguageTest { /** * @param ln - * Unused parameter, kept to comply with expected type sig + * Unused parameter, kept to comply with expected type + * sig */ private static void printEnv(String ln, DiceLanguageState stat) { System.out.println("Printing enviroment for debugging purposes."); - stat.doWith((dep, env) -> env.forEach((key, exp) -> System.out - .println("\tKey: " + key + "\tExp: " + exp.toString()))); + stat.doWith((dep, env) -> env.forEach( + (key, exp) -> System.out.println("\tKey: " + key + "\tExp: " + exp.toString()))); } private static void rollReference(String ln, DiceLanguageState stat) { String[] strangs = ln.split(" "); - System.out.println("\tRolling dice expression " + strangs[1] + " " - + strangs[2] + " times."); + System.out.println("\tRolling dice expression " + strangs[1] + " " + strangs[2] + " times."); int nRolls = Integer.parseInt(strangs[2]); - IDiceExpression dexp = stat - .merge((dep, env) -> env.get(strangs[1])); + IDiceExpression dexp = stat.merge((dep, env) -> env.get(strangs[1])); for (int i = 1; i <= nRolls; i++) { int roll = dexp.roll(); |
