aboutsummaryrefslogtreecommitdiffstats
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/com/jantuomi/tunkki/core/parser/ASTGenerator.java11
-rw-r--r--src/main/com/jantuomi/tunkki/core/runtime/Interpreter.java8
-rw-r--r--src/main/com/jantuomi/tunkki/repl/Repl.java35
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");
}
}