diff options
| author | jantuomi <jans.tuomi@gmail.com> | 2016-08-01 12:40:38 +0300 |
|---|---|---|
| committer | jantuomi <jans.tuomi@gmail.com> | 2016-08-01 12:40:38 +0300 |
| commit | 7fe8ef9ca40cabf54dd0d43a10bf7227efbae9b4 (patch) | |
| tree | 1fa902a481f09d6ea230c0fac942c256c75e1ed0 | |
| parent | 20a3fb57ae9ee1e70a2d57169742d481eac82fe9 (diff) | |
Add multiline input to REPL
Fix bug where REPL input is echoed back
Fix bug where an exclamation point would cause a JLine event exception
Input can now be continued on the next line with a '\' character at the end of the line. The character will be disregarded as whitespace in the parser.
Resolves #13
| -rw-r--r-- | src/main/com/jantuomi/tunkki/core/parser/ASTGenerator.java | 11 | ||||
| -rw-r--r-- | src/main/com/jantuomi/tunkki/core/runtime/Interpreter.java | 8 | ||||
| -rw-r--r-- | src/main/com/jantuomi/tunkki/repl/Repl.java | 35 |
3 files changed, 42 insertions, 12 deletions
diff --git a/src/main/com/jantuomi/tunkki/core/parser/ASTGenerator.java b/src/main/com/jantuomi/tunkki/core/parser/ASTGenerator.java index 29f5ba2..41ea0ec 100644 --- a/src/main/com/jantuomi/tunkki/core/parser/ASTGenerator.java +++ b/src/main/com/jantuomi/tunkki/core/parser/ASTGenerator.java @@ -29,13 +29,18 @@ public class ASTGenerator { return list; } - public void printTree(ASTNode root) { + public void printTree(ASTNode root) throws TunkkiError { System.out.println("### AST Tree begin ###"); - root.print(0); + try { + root.print(0); + } catch (NullPointerException ex) { + throw new TunkkiError(TunkkiError.ExceptionType.GeneralError, -1, "Malformed syntax tree."); + } + System.out.println("### AST Tree end ###"); } - public void printAllTrees(List<ASTNode> trees) { + public void printAllTrees(List<ASTNode> trees) throws TunkkiError { for (ASTNode tree : trees) { printTree(tree); } diff --git a/src/main/com/jantuomi/tunkki/core/runtime/Interpreter.java b/src/main/com/jantuomi/tunkki/core/runtime/Interpreter.java index 5674dab..0c986c3 100644 --- a/src/main/com/jantuomi/tunkki/core/runtime/Interpreter.java +++ b/src/main/com/jantuomi/tunkki/core/runtime/Interpreter.java @@ -21,8 +21,14 @@ public class Interpreter { } public static String execute(List<ASTNode> sequence) throws TunkkiError { String output = ""; + Datatype data; for (ASTNode node : sequence) { - Datatype data = node.evaluate(); + try { + data = node.evaluate(); + } + catch (NullPointerException ex) { + throw new TunkkiError(TunkkiError.ExceptionType.GeneralError, -1, "Malformed input."); + } if (data != null) { output = data.toString(); diff --git a/src/main/com/jantuomi/tunkki/repl/Repl.java b/src/main/com/jantuomi/tunkki/repl/Repl.java index 817d746..3b63b6e 100644 --- a/src/main/com/jantuomi/tunkki/repl/Repl.java +++ b/src/main/com/jantuomi/tunkki/repl/Repl.java @@ -1,6 +1,7 @@ package com.jantuomi.tunkki.repl; import com.jantuomi.tunkki.Tunkki; +import com.jantuomi.tunkki.core.parser.datatype.IntegerDatatype; import com.jantuomi.tunkki.exception.TunkkiError; import jline.console.*; @@ -23,17 +24,33 @@ public class Repl { ConsoleReader reader = new ConsoleReader(); reader.setBellEnabled(false); + reader.setExpandEvents(false); String line; + String command = ""; PrintWriter out = new PrintWriter(System.out); - - while ((line = readLine(reader, "")) != null) { - try { - tunkki.run(line); + boolean isMultilineInput = false; + while (true) { + if (isMultilineInput) { + line = readLine(reader, "... "); + } else { + line = readLine(reader, "tunkki> "); } - catch (TunkkiError ex) { - ex.printStackTrace(); - Thread.sleep(50); + + if (line.endsWith("\\")) { + isMultilineInput = true; + command += line.substring(0, line.length() - 1) + " "; + } else { + try { + isMultilineInput = false; + command += line; + tunkki.run(command); + } catch (TunkkiError ex) { + ex.printStackTrace(); + Thread.sleep(50); + } finally { + command = ""; + } } out.flush(); @@ -42,11 +59,13 @@ public class Repl { private String readLine(ConsoleReader reader, String promptMessage) throws IOException { - String line = reader.readLine(promptMessage + "\ntunkki> "); + System.out.print(promptMessage); + String line = reader.readLine(); return line.trim(); } private void showWelcomeText() { System.out.println(String.format("tunkki REPL version %s", tunkki.versionString())); + System.out.println("Copyright: Jan Tuomi 2016"); } } |
