|
@@ -13,10 +13,7 @@ import me.hammerle.exceptions.PreScriptException;
|
|
|
|
|
|
public class SnuviScript {
|
|
|
|
|
|
- private static ISnuviLogger LOGGER;
|
|
|
- private static SnuviParser PARSER;
|
|
|
-
|
|
|
- public static void main(String[] args) throws IOException {
|
|
|
+ public static void main(String[] args) {
|
|
|
Executable executable;
|
|
|
switch (args.length) {
|
|
|
case 0:
|
|
@@ -30,23 +27,10 @@ public class SnuviScript {
|
|
|
System.err.println("Illegal number of arguments.");
|
|
|
return;
|
|
|
}
|
|
|
- createLogger();
|
|
|
- createParser();
|
|
|
- registerTerminalMethods();
|
|
|
- executeScript(executable);
|
|
|
- }
|
|
|
-
|
|
|
- private static void createLogger() {
|
|
|
- LOGGER = new SnuviTerminalLogger();
|
|
|
- }
|
|
|
-
|
|
|
- private static void createParser() {
|
|
|
- PARSER = new SnuviParser(LOGGER, new SnuviSimpleScheduler());
|
|
|
- }
|
|
|
-
|
|
|
- private static void registerTerminalMethods() {
|
|
|
- PARSER.registerConsumer("print", (o, sc) -> System.out.println(ScriptUtils.connect(o, " ", 0)));
|
|
|
- PARSER.registerFunction("read", (o, sc) -> readLine());
|
|
|
+ ISnuviLogger logger = createLogger();
|
|
|
+ SnuviParser parser = createParser(logger);
|
|
|
+ registerTerminalMethods(logger, parser);
|
|
|
+ executeScript(executable, logger, parser);
|
|
|
}
|
|
|
|
|
|
private static String readLine() throws UncheckedIOException {
|
|
@@ -69,11 +53,25 @@ public class SnuviScript {
|
|
|
return new Executable(name, code);
|
|
|
}
|
|
|
|
|
|
- private static void executeScript(Executable executable) {
|
|
|
+ private static ISnuviLogger createLogger() {
|
|
|
+ return new FormattedSnuviLogger();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static SnuviParser createParser(ISnuviLogger logger) {
|
|
|
+ return new SnuviParser(logger, new SnuviSimpleScheduler());
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void registerTerminalMethods(ISnuviLogger logger, SnuviParser parser) {
|
|
|
+ parser.registerConsumer("print", (o, sc) -> System.out.println(ScriptUtils.connect(o, " ", 0)));
|
|
|
+ parser.registerFunction("read", (o, sc) -> readLine());
|
|
|
+ parser.registerConsumer("log", (o, sc) -> logger.printInfo(ScriptUtils.connect(o, " ", 0)));
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void executeScript(Executable executable, ISnuviLogger logger, SnuviParser parser) {
|
|
|
try {
|
|
|
- PARSER.startScript(executable.getName(), executable.getCode());
|
|
|
+ parser.startScript(executable.getName(), executable.getCode());
|
|
|
} catch (PreScriptException ex) {
|
|
|
- LOGGER.printException(ex, "none", executable.getName(), ex.getLine());
|
|
|
+ logger.printException(ex, "none", executable.getName(), ex.getLine());
|
|
|
}
|
|
|
}
|
|
|
}
|