diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2018-05-30 15:56:45 -0300 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2018-05-30 15:56:45 -0300 |
| commit | a7ba845df20d1bec77b4044738f1ff4fc1128201 (patch) | |
| tree | 2d9c173fe691ce125de49a3fb6af48e87ba66bab /base/src/bjc | |
| parent | baacb867ef6cff87e45d964a9a622001c38e26ec (diff) | |
Updates
Some minor updates
Diffstat (limited to 'base/src/bjc')
| -rw-r--r-- | base/src/bjc/dicelang/DiceLangEngine.java | 70 | ||||
| -rw-r--r-- | base/src/bjc/dicelang/Shunter.java | 7 | ||||
| -rw-r--r-- | base/src/bjc/dicelang/cli/CLIArgsParser.java | 1 | ||||
| -rw-r--r-- | base/src/bjc/dicelang/cli/DiceLangConsole.java | 56 |
4 files changed, 92 insertions, 42 deletions
diff --git a/base/src/bjc/dicelang/DiceLangEngine.java b/base/src/bjc/dicelang/DiceLangEngine.java index 2abf066..1104ee4 100644 --- a/base/src/bjc/dicelang/DiceLangEngine.java +++ b/base/src/bjc/dicelang/DiceLangEngine.java @@ -103,9 +103,12 @@ public class DiceLangEngine { /* Initialize operator expander. */ opExpander = new ConfigurableTokenSplitter(true); + /* Add grouping operators */ opExpander.addMultiDelimiters("(", ")"); opExpander.addMultiDelimiters("[", "]"); opExpander.addMultiDelimiters("{", "}"); + + /* Add simple operators */ opExpander.addSimpleDelimiters(":="); opExpander.addSimpleDelimiters("=>"); opExpander.addSimpleDelimiters("//"); @@ -115,24 +118,25 @@ public class DiceLangEngine { opExpander.addSimpleDelimiters("-"); opExpander.addSimpleDelimiters("*"); opExpander.addSimpleDelimiters("/"); + opExpander.compile(); /* Initialize literal IDs */ nextLiteral = 1; /* Initial mode settings. */ - debugMode = true; + debugMode = true; postfixMode = false; - prefixMode = false; - stepEval = false; + prefixMode = false; + stepEval = false; /* Create components. */ shunt = new Shunter(); parsr = new Parser(); streamEng = new StreamEngine(); - tokenzer = new Tokenizer(this); - eval = new Evaluator(this); + tokenzer = new Tokenizer(this); + eval = new Evaluator(this); } /** Sort defns by priority. */ @@ -211,7 +215,11 @@ public class DiceLangEngine { return stepEval; } - /* Matches double-angle bracketed strings. */ + /* + * Matches double-angle bracketed strings. + * + * These are used for tokens that aren't expanded. + */ private final Pattern nonExpandPattern = Pattern.compile("<<([^\\>]*(?:\\>(?:[^\\>])*)*)>>"); /** @@ -265,8 +273,12 @@ public class DiceLangEngine { /* Apply token defns */ for (final Define dfn : tokenDefns.toIterable()) { /* - * @NOTE What happens with a define that produces multiple tokens from one + * @NOTE + * + * What happens with a define that produces multiple tokens from one * token? + * + * At the moment, nothing. */ newTok = dfn.apply(newTok); } @@ -338,7 +350,12 @@ public class DiceLangEngine { /* Expand token groups */ final IList<Token> readyTokens = shuntedTokens.flatMap(tk -> { if (tk.type == Token.Type.TOKGROUP || tk.type == Token.Type.TAGOP || tk.type == Token.Type.TAGOPR) { - LOG.finer(String.format("Expanding token group to: %s\n", tk.tokenValues.toString())); + String msg = String.format("Expanding token group to: %s\n", tk.tokenValues.toString()); + LOG.finer(msg); + + if(debugMode) + System.out.print(msg); + return tk.tokenValues; } else { return new FunctionalList<>(tk); @@ -387,7 +404,7 @@ public class DiceLangEngine { /* Run the tokens through the stream engine */ final IList<String> streamToks = new FunctionalList<>(); - final boolean succ = streamEng.doStreams(command.split(" "), streamToks); + final boolean succ = streamEng.doStreams(command.split(" "), streamToks); if (!succ) { return null; @@ -397,7 +414,9 @@ public class DiceLangEngine { if (debugMode) { String msg = String.format("\tCommand after stream commands: %s\n", newComm); + LOG.fine(msg); + System.out.print(msg); } @@ -408,7 +427,9 @@ public class DiceLangEngine { if (debugMode) { String msg = String.format("\tCommand after line defines: %s\n", newComm); + LOG.fine(msg); + System.out.print(msg); } @@ -429,8 +450,13 @@ public class DiceLangEngine { final String descVal = TokenUtils.descapeString(litVal); stringLiterals.put(litName, descVal); - if (debugMode) - LOG.finer(String.format("Replaced string literal '%s' with literal no. %d", descVal, nextLiteral)); + if (debugMode) { + String msg = String.format("Replaced string literal '%s' with literal no. %d", descVal, nextLiteral); + + System.out.printf("\t\tDEBUG(1): %s\n", msg); + + LOG.finer(msg); + } nextLiteral += 1; @@ -443,7 +469,9 @@ public class DiceLangEngine { if (debugMode) { String msg = String.format("\tCommand after destringing: %s\n", destringedCommand); + LOG.fine(msg); + System.out.print(msg); /* Print the string table if it exists. */ @@ -457,7 +485,7 @@ public class DiceLangEngine { } /* Split the command into tokens */ - final String strang = destringedCommand.toString(); + final String strang = destringedCommand.toString(); IList<String> tokens = FunctionalStringTokenizer.fromString(strang).toList(); /* Temporarily remove non-expanding tokens */ @@ -468,8 +496,13 @@ public class DiceLangEngine { if (nonExpandMatcher.matches()) { final String tkName = "nonExpandToken" + nextLiteral++; nonExpandedTokens.put(tkName, nonExpandMatcher.group(1)); + String msg = String.format("Pulled non-expander '%s' to '%s'", nonExpandMatcher.group(1), tkName); + + if(debugMode) + System.out.printf("\t\tDEBUG(1): %s\n", msg); + + LOG.finer(msg); - LOG.finer(String.format("Pulled non-expander '%s' to '%s'", nonExpandMatcher.group(1), tkName)); return tkName; } @@ -478,7 +511,9 @@ public class DiceLangEngine { if (debugMode) { String msg = String.format("\tCommand after removal of non-expanders: %s\n", tokens.toString()); + LOG.fine(msg); + System.out.print(msg); } @@ -487,7 +522,9 @@ public class DiceLangEngine { if (debugMode) { String msg = String.format("\tCommand after token expansion: %s\n", fullyExpandedTokens.toString()); + LOG.fine(msg); + System.out.print(msg); } @@ -502,7 +539,9 @@ public class DiceLangEngine { if (debugMode) { String msg = String.format("\tCommand after non-expander reinsertion: %s\n", fullyExpandedTokens.toString()); + LOG.fine(msg); + System.out.print(msg); } @@ -570,7 +609,10 @@ public class DiceLangEngine { if (debugMode) { System.out.printf("\t\tEvaluates to %s", res); - if (res.type == EvaluatorResult.Type.DICE) { + if(res == null) { + // This means we got a null + // These shouldn't happen + } else if (res.type == EvaluatorResult.Type.DICE) { String value = ((DiceEvaluatorResult) res).diceVal.value(); System.out.println("\t\t (sample roll " + value + ")"); diff --git a/base/src/bjc/dicelang/Shunter.java b/base/src/bjc/dicelang/Shunter.java index 79ab97a..8284da4 100644 --- a/base/src/bjc/dicelang/Shunter.java +++ b/base/src/bjc/dicelang/Shunter.java @@ -80,12 +80,12 @@ public class Shunter { /* Setup operators. */ /* Math operators. */ - ops.put(ADD, 0 + MATH_PREC); + ops.put(ADD, 0 + MATH_PREC); ops.put(SUBTRACT, 0 + MATH_PREC); ops.put(MULTIPLY, 1 + MATH_PREC); - ops.put(IDIVIDE, 1 + MATH_PREC); - ops.put(DIVIDE, 1 + MATH_PREC); + ops.put(IDIVIDE, 1 + MATH_PREC); + ops.put(DIVIDE, 1 + MATH_PREC); /* Dice operators. */ ops.put(DICEGROUP, 0 + DICE_PREC); @@ -166,6 +166,7 @@ public class Shunter { if(unaryStack.size() != 0) { if(isUnary(tk)) { unaryStack.add(tk); + return true; } diff --git a/base/src/bjc/dicelang/cli/CLIArgsParser.java b/base/src/bjc/dicelang/cli/CLIArgsParser.java index b4c0c7c..963ac8e 100644 --- a/base/src/bjc/dicelang/cli/CLIArgsParser.java +++ b/base/src/bjc/dicelang/cli/CLIArgsParser.java @@ -161,6 +161,7 @@ public class CLIArgsParser { /* Load a series of defines from a file. */ private static int defineFile(final int i, final String[] args, final DiceLangEngine eng) { + /* :DefineRefactor */ if(i >= args.length - 1) { Errors.inst.printError(EK_CLI_MISARG, "define-file"); return -1; diff --git a/base/src/bjc/dicelang/cli/DiceLangConsole.java b/base/src/bjc/dicelang/cli/DiceLangConsole.java index 18b6f7f..75fee3b 100644 --- a/base/src/bjc/dicelang/cli/DiceLangConsole.java +++ b/base/src/bjc/dicelang/cli/DiceLangConsole.java @@ -40,7 +40,6 @@ public class DiceLangConsole { pragmas = new FunctionalMap<>(); pragmas.put("debug", new DiceLangPragma() { - @Override public String getDescription() { return "Toggle debug mode, which includes a bunch more output during various stages of compilation and interpretation"; @@ -94,7 +93,7 @@ public class DiceLangConsole { /* Read initial command. */ try { - comm = read.readLine(String.format("(%d) dice-lang> ", commandNumber)); + comm = read.readLine(String.format("(%d-%s) dice-lang> ", commandNumber, multiLine ? "M" : "S")); } catch(final IOException ioex) { System.out.println("ERROR: I/O failed"); return; @@ -109,9 +108,9 @@ public class DiceLangConsole { final boolean success = handlePragma(comm.substring(7)); if(success) { - System.out.println("Pragma completed succesfully"); + System.out.println("\tPragma completed succesfully"); } else { - System.out.println("Pragma execution failed"); + System.out.println("\tPragma execution failed"); } } else { if(multiLine) { @@ -124,42 +123,37 @@ public class DiceLangConsole { comm = String.format("%s %s", comm, nLine); } while(true); + + if(eng.debugMode) + System.out.printf("\tDEBUG: Read multiline command:\n%s\n", comm); + } catch(IOException ioex) { - System.out.println("ERROR: I/O failed"); + System.out.println("\tERROR: I/O failed"); return; } } /* Run commands. */ if(eng.debugMode) { - System.out.printf("\tRaw command: %s\n", comm); + System.out.printf("\tDEBUG: Raw command: %s\n", comm); } final boolean success = eng.runCommand(comm); if(success) { - System.out.println("Command completed succesfully"); + System.out.println("\tCommand completed succesfully"); } else { - System.out.println("Command execution failed"); + System.out.println("\tCommand execution failed"); } commandNumber += 1; } - - /* Read the next command. */ - try { - comm = read.readLine(String.format("(%d) dice-lang> ", commandNumber)); - } catch(final IOException ioex) { - System.out.println("ERROR: I/O failed"); - return; - } } while(true); - } /* Handle running pragmas. */ private boolean handlePragma(final String pragma) { if(eng.debugMode) { - System.out.println("\tRaw pragma: " + pragma); + System.out.println("\tDEBUG: Raw pragma: " + pragma); } /* Grab the name from the arguments. */ @@ -178,6 +172,10 @@ public class DiceLangConsole { * @TODO 10/09/17 Ben Culkin :PragmaRefactor * * Swap to using something that makes it easier to add pragmas. + * + * @NOTE 5/30/18 + * We have part of this, see the implementation of + * DiceLangPragma above, we just need to use it. */ switch(pragmaName) { case "debug": @@ -190,13 +188,14 @@ public class DiceLangConsole { System.out.println("\tPrefix mode is now " + eng.togglePrefix()); break; case "stepeval": - System.out.println("\tStepeval mode is now" + eng.toggleStepEval()); + System.out.println("\tStepeval mode is now " + eng.toggleStepEval()); break; case "define": return defineMode(pragma.substring(7)); case "help": return helpMode(pragma.substring(5)); case "multi-line": + System.out.println("\tMulti-line mode is now " + !multiLine); multiLine = !multiLine; break; default: @@ -244,6 +243,10 @@ public class DiceLangConsole { private final Pattern slashPattern = Pattern.compile("/((?:\\\\.|[^/\\\\])*)/"); /* Parse a define macro. */ + /* + * @TODO 5/30/18 :DefineRefactor + * Adjust this to use a better set-up. + */ private boolean defineMode(final String defineText) { /* Grab all of the separator spaces. */ final int firstIndex = defineText.indexOf(' '); @@ -278,7 +281,7 @@ public class DiceLangConsole { } /* Get the priority and define type. */ - final int priority = Integer.parseInt(defineText.substring(0, firstIndex)); + final int priority = Integer.parseInt(defineText.substring(0, firstIndex)); final String defineType = defineText.substring(firstIndex + 1, secondIndex); Define.Type type; @@ -293,11 +296,11 @@ public class DiceLangConsole { type = Define.Type.TOKEN; break; case "subline": - type = Define.Type.LINE; + type = Define.Type.LINE; subMode = true; break; case "subtoken": - type = Define.Type.TOKEN; + type = Define.Type.TOKEN; subMode = true; break; default: @@ -307,15 +310,17 @@ public class DiceLangConsole { /* Do we want this to be a recursive pattern? */ final boolean doRecur = defineText.substring(secondIndex + 1, thirdIndex).equalsIgnoreCase("true"); + /* Do we want this pattern to have a guard? */ final boolean hasGuard = defineText.substring(thirdIndex + 1, fourthIndex).equalsIgnoreCase("true"); + /* Do we want this pattern to use circular replacements. */ final boolean isCircular = defineText.substring(thirdIndex + 1, fourthIndex).equalsIgnoreCase("true"); /* The part of the string that contains patterns. */ - final String pats = defineText.substring(fifthIndex + 1).trim(); + final String pats = defineText.substring(fifthIndex + 1).trim(); final Matcher patMatcher = slashPattern.matcher(pats); - String guardPattern = null; + String guardPattern = null; if(hasGuard) { /* Grab the guard pattern. */ @@ -333,7 +338,7 @@ public class DiceLangConsole { return false; } - final String searchPattern = patMatcher.group(1); + final String searchPattern = patMatcher.group(1); final List<String> replacePatterns = new LinkedList<>(); while(patMatcher.find()) { @@ -366,6 +371,7 @@ public class DiceLangConsole { */ public static void main(final String[] args) { final DiceLangConsole console = new DiceLangConsole(args); + console.run(); } } |
