Commits

Roman Dawydkin committed 05bf550

Console shell for interpreter.

Comments (0)

Files changed (3)

+@echo off
+java -cp build/classes toy.ConsoleShell

src/toy/ConsoleShell.java

+package toy;
+
+import java.io.Console;
+import java.io.PrintWriter;
+
+public class ConsoleShell
+{
+	class ConsoleMessagePrinter implements MessagePrinter
+	{
+		public void printError(String msg) {
+			final PrintWriter writer = console.writer();
+			writer.print("! ");
+			writer.println(msg);
+		}
+
+		public void printLine(String msg) {
+			final PrintWriter writer = console.writer();
+			writer.print(": ");
+			writer.println(msg);
+		}
+	}
+
+	final Console console;
+	private Interpreter interpreter;
+
+	public static void main(String[] args) {
+		new ConsoleShell().run();
+	}
+
+	public ConsoleShell() {
+		console = System.console();
+	}
+
+	private void run() {
+		if (console == null) {
+			System.err.println("Cannot allocate console");
+			System.exit(1);
+		}
+		interpreter = new Interpreter(new ConsoleMessagePrinter());
+		for (;;) {
+			final String line = console.readLine("> ");
+			if (line == null) {
+				break;
+			}
+			interpreter.eval(line);
+		}
+	}
+}

src/toy/Interpreter.java

 					return;
 				}
 			} catch (Exception ex) {
-				ex.printStackTrace();
+				// ex.printStackTrace();
 				printer.printError(ex.toString());
 			}
 		}