summaryrefslogtreecommitdiff
path: root/dice-lang/src/bjc/dicelang/DiceLangConsole.java
diff options
context:
space:
mode:
authorBenjamin J. Culkin <bjculkin@mix.wvu.edu>2017-10-08 16:38:35 -0300
committerBenjamin J. Culkin <bjculkin@mix.wvu.edu>2017-10-08 16:38:35 -0300
commit054643900e7b857cafe123b0b4c03f10a95520ed (patch)
treec289fc397fe79ea0a6792e3f2f39a05ed1315936 /dice-lang/src/bjc/dicelang/DiceLangConsole.java
parentf40e5a873420d70d01ff7e01b77bdbd64faab00e (diff)
Update
Diffstat (limited to 'dice-lang/src/bjc/dicelang/DiceLangConsole.java')
-rw-r--r--dice-lang/src/bjc/dicelang/DiceLangConsole.java144
1 files changed, 44 insertions, 100 deletions
diff --git a/dice-lang/src/bjc/dicelang/DiceLangConsole.java b/dice-lang/src/bjc/dicelang/DiceLangConsole.java
index 29b6f1a..7cd6bb5 100644
--- a/dice-lang/src/bjc/dicelang/DiceLangConsole.java
+++ b/dice-lang/src/bjc/dicelang/DiceLangConsole.java
@@ -19,10 +19,11 @@ import jline.Terminal;
*
*/
public class DiceLangConsole {
+ /* The number of commands executed so far. */
private int commandNumber;
-
+ /* The engine that executes commands. */
private final DiceLangEngine eng;
-
+ /* The place to read input from. */
private ConsoleReader read;
/**
@@ -33,13 +34,10 @@ public class DiceLangConsole {
*/
public DiceLangConsole(final String[] args) {
commandNumber = 0;
-
eng = new DiceLangEngine();
-
if (!CLIArgsParser.parseArgs(args, eng)) {
System.exit(1);
}
-
Terminal.setupTerminal();
}
@@ -47,9 +45,7 @@ public class DiceLangConsole {
* Run the console.
*/
public void run() {
- /*
- * Set up console.
- */
+ /* Set up console. */
try {
read = new ConsoleReader();
} catch (final IOException ioex) {
@@ -58,12 +54,9 @@ public class DiceLangConsole {
}
System.out.println("dice-lang v0.2");
-
String comm = null;
- /*
- * Read initial command.
- */
+ /* Read initial command. */
try {
comm = read.readLine(String.format("(%d) dice-lang> ", commandNumber));
} catch (final IOException ioex) {
@@ -71,35 +64,22 @@ public class DiceLangConsole {
return;
}
- /*
- * Run commands.
- */
+ /* Run commands. */
while (!comm.equals("quit") && !comm.equals("exit")) {
if (comm.startsWith("pragma")) {
- /*
- * Run pragmas.
- */
+ /* Run pragmas. */
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);
+ /* Run commands. */
+ 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;
}
@@ -114,23 +94,15 @@ 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.
- */
+ /* 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);
- }
+ /* Handle argless pragmas. */
+ if (firstIndex == -1) pragmaName = pragma;
+ else pragmaName = pragma.substring(0, firstIndex);
/*
* Run pragmas.
@@ -139,25 +111,19 @@ 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;
@@ -166,62 +132,52 @@ public class DiceLangConsole {
return true;
}
- /*
- * Run a help mode.
- */
+ /* Run a help mode. */
private static boolean helpMode(final String pragma) {
switch (pragma.trim()) {
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);
}
- // Help always works
+ /* Help always works */
return true;
}
- /*
- * Matches slash-delimited strings (like /text/ or /text\/text/).
- */
+ /* Matches slash-delimited strings (like /text/ or /text\/text/). */
private final Pattern slashPattern = Pattern.compile("/((?:\\\\.|[^/\\\\])*)/");
+ /* Parse a define macro. */
private boolean defineMode(final String defineText) {
- /*
- * Grab all of the separator spaces.
- */
- final int firstIndex = defineText.indexOf(' ');
- final int secondIndex = defineText.indexOf(' ', firstIndex + 1);
- final int thirdIndex = defineText.indexOf(' ', secondIndex + 1);
- final int fourthIndex = defineText.indexOf(' ', thirdIndex + 1);
- final int fifthIndex = defineText.indexOf(' ', fourthIndex + 1);
- final int sixthIndex = defineText.indexOf(' ', fifthIndex + 1);
-
- /*
- * Error if we got something we didn't need.
+ /* Grab all of the separator spaces. */
+ final int firstIndex = defineText.indexOf(' ');
+ final int secondIndex = defineText.indexOf(' ', firstIndex + 1);
+ final int thirdIndex = defineText.indexOf(' ', secondIndex + 1);
+ final int fourthIndex = defineText.indexOf(' ', thirdIndex + 1);
+ 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.
*/
if (firstIndex == -1) {
Errors.inst.printError(EK_CONS_INVDEFINE, "(no priority)");
@@ -244,7 +200,6 @@ public class DiceLangConsole {
}
final int priority = Integer.parseInt(defineText.substring(0, firstIndex));
-
final String defineType = defineText.substring(firstIndex + 1, secondIndex);
Define.Type type;
@@ -254,36 +209,31 @@ 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");
- final boolean hasGuard = defineText.substring(thirdIndex + 1,
- fourthIndex).equalsIgnoreCase("true");
- final boolean isCircular = defineText.substring(thirdIndex + 1,
- fourthIndex).equalsIgnoreCase("true");
+ final boolean doRecur = defineText.substring(secondIndex + 1, thirdIndex)
+ .equalsIgnoreCase("true");
+ final boolean hasGuard = defineText.substring(thirdIndex + 1, fourthIndex)
+ .equalsIgnoreCase("true");
+ final boolean isCircular = defineText.substring(thirdIndex + 1, fourthIndex)
+ .equalsIgnoreCase("true");
final String pats = defineText.substring(fifthIndex + 1).trim();
final Matcher patMatcher = slashPattern.matcher(pats);
-
String guardPattern = null;
if (hasGuard) {
@@ -291,7 +241,6 @@ public class DiceLangConsole {
Errors.inst.printError(EK_CONS_INVDEFINE, "(no guard pattern)");
return false;
}
-
guardPattern = patMatcher.group(1);
}
@@ -302,22 +251,18 @@ 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);
+ final Define dfn = new Define(priority, subMode, doRecur,
+ isCircular, guardPattern, searchPattern,
+ replacePatterns);
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;
}
@@ -330,7 +275,6 @@ public class DiceLangConsole {
*/
public static void main(final String[] args) {
final DiceLangConsole console = new DiceLangConsole(args);
-
console.run();
}
}