diff options
Diffstat (limited to 'dice-lang/src/bjc/dicelang/DiceLangConsole.java')
| -rw-r--r-- | dice-lang/src/bjc/dicelang/DiceLangConsole.java | 74 |
1 files changed, 56 insertions, 18 deletions
diff --git a/dice-lang/src/bjc/dicelang/DiceLangConsole.java b/dice-lang/src/bjc/dicelang/DiceLangConsole.java index 7cd6bb5..ebf9b59 100644 --- a/dice-lang/src/bjc/dicelang/DiceLangConsole.java +++ b/dice-lang/src/bjc/dicelang/DiceLangConsole.java @@ -35,9 +35,11 @@ public class DiceLangConsole { public DiceLangConsole(final String[] args) { commandNumber = 0; eng = new DiceLangEngine(); + if (!CLIArgsParser.parseArgs(args, eng)) { System.exit(1); } + Terminal.setupTerminal(); } @@ -68,18 +70,26 @@ public class DiceLangConsole { while (!comm.equals("quit") && !comm.equals("exit")) { if (comm.startsWith("pragma")) { /* Run pragmas. */ - final boolean success = handlePragma(comm.substring(7)); + final boolean success = handlePragma(comm.substring(7)); - if (success) System.out.println("Pragma completed succesfully"); - else System.out.println("Pragma execution failed"); + if (success) { + System.out.println("Pragma completed succesfully"); + } else { + System.out.println("Pragma execution failed"); + } } else { /* Run commands. */ - if(eng.debugMode) System.out.printf("\tRaw command: %s\n", comm); + if (eng.debugMode) { + System.out.printf("\tRaw command: %s\n", comm); + } final boolean success = eng.runCommand(comm); - if (success) System.out.println("Command completed succesfully"); - else System.out.println("Command execution failed"); + if (success) { + System.out.println("Command completed succesfully"); + } else { + System.out.println("Command execution failed"); + } commandNumber += 1; } @@ -94,15 +104,20 @@ public class DiceLangConsole { } private boolean handlePragma(final String pragma) { - if(eng.debugMode) System.out.println("\tRaw pragma: " + pragma); + if (eng.debugMode) { + System.out.println("\tRaw pragma: " + pragma); + } /* Grab the name from the arguments. */ String pragmaName = null; final int firstIndex = pragma.indexOf(' '); /* Handle argless pragmas. */ - if (firstIndex == -1) pragmaName = pragma; - else pragmaName = pragma.substring(0, firstIndex); + if (firstIndex == -1) { + pragmaName = pragma; + } else { + pragmaName = pragma.substring(0, firstIndex); + } /* * Run pragmas. @@ -111,19 +126,25 @@ public class DiceLangConsole { case "debug": System.out.println("\tDebug mode is now " + eng.toggleDebug()); break; + case "postfix": System.out.println("\tPostfix mode is now " + eng.togglePostfix()); break; + case "prefix": System.out.println("\tPrefix mode is now " + eng.togglePrefix()); break; + case "stepeval": System.out.println("\tStepeval mode is now" + eng.toggleStepEval()); break; + case "define": return defineMode(pragma.substring(7)); + case "help": return helpMode(pragma.substring(5)); + default: Errors.inst.printError(EK_CONS_INVPRAG, pragma); return false; @@ -138,22 +159,28 @@ public class DiceLangConsole { case "help": System.out.println("\tGet help on pragmas"); break; + case "debug": System.out.println("\tToggle debug mode. (Output stage results)"); break; + case "postfix": System.out.println("\tToggle postfix mode. (Don't shunt tokens)"); break; + case "prefix": System.out.println("\tToggle prefix mode. (Reverse token order instead of shunting)"); break; + case "stepeval": System.out.println("\tToggle stepeval mode. (Print out evaluation progress)"); break; + case "define": System.out.println("\tAdd a macro rewrite directive."); System.out.println("\tdefine <priority> <type> <recursion> <guard> <circular> <patterns>..."); break; + default: System.out.println("\tNo help available for pragma " + pragma); } @@ -175,7 +202,7 @@ public class DiceLangConsole { final int fifthIndex = defineText.indexOf(' ', fourthIndex + 1); final int sixthIndex = defineText.indexOf(' ', fifthIndex + 1); - /* + /* * Error if we got something we didn't need, or didn't get * something we need. */ @@ -209,28 +236,32 @@ public class DiceLangConsole { case "line": type = Define.Type.LINE; break; + case "token": type = Define.Type.TOKEN; break; + case "subline": type = Define.Type.LINE; subMode = true; break; + case "subtoken": type = Define.Type.TOKEN; subMode = true; break; + default: Errors.inst.printError(EK_CONS_INVDEFINE, "(unknown type)"); return false; } final boolean doRecur = defineText.substring(secondIndex + 1, thirdIndex) - .equalsIgnoreCase("true"); + .equalsIgnoreCase("true"); final boolean hasGuard = defineText.substring(thirdIndex + 1, fourthIndex) - .equalsIgnoreCase("true"); + .equalsIgnoreCase("true"); final boolean isCircular = defineText.substring(thirdIndex + 1, fourthIndex) - .equalsIgnoreCase("true"); + .equalsIgnoreCase("true"); final String pats = defineText.substring(fifthIndex + 1).trim(); final Matcher patMatcher = slashPattern.matcher(pats); @@ -241,6 +272,7 @@ public class DiceLangConsole { Errors.inst.printError(EK_CONS_INVDEFINE, "(no guard pattern)"); return false; } + guardPattern = patMatcher.group(1); } @@ -251,18 +283,24 @@ public class DiceLangConsole { final String searchPattern = patMatcher.group(1); final List<String> replacePatterns = new LinkedList<>(); + while (patMatcher.find()) { replacePatterns.add(patMatcher.group(1)); } final Define dfn = new Define(priority, subMode, doRecur, - isCircular, guardPattern, searchPattern, - replacePatterns); + isCircular, guardPattern, searchPattern, + replacePatterns); - if (dfn.inError) return false; + if (dfn.inError) { + return false; + } - if (type == Define.Type.LINE) eng.addLineDefine(dfn); - else eng.addTokenDefine(dfn); + if (type == Define.Type.LINE) { + eng.addLineDefine(dfn); + } else { + eng.addTokenDefine(dfn); + } return true; } |
