Command Parser.

This commit is contained in:
Seth J. Morabito committed 2008-12-05 18:03:00 -08:00
1 parent 7f31c4c950
commit a74322459a
3 files changed
+101 -2

No files matched your search

@@ -0,0 +1,70 @@
package com.loomcom.j6502;
import java.io.*;
public class CommandParser {
private BufferedReader m_in;
private BufferedWriter m_out;
public CommandParser(InputStream in, OutputStream out) {
m_in = new BufferedReader(new InputStreamReader(in));
m_out = new BufferedWriter(new OutputStreamWriter(out));
}
public void run() {
try {
String command = null;
greeting();
prompt();
while (!shouldQuit(command = readLine())) {
dispatch(command);
prompt();
}
writeLine("Goodbye!");
} catch (IOException ex) {
System.err.println("Error: " + ex.toString());
System.exit(1);
}
}
/**
* Dispatch the command.
*/
public void dispatch(String command) throws IOException {
writeLine("You entered: " + command);
}
/*******************************************************************
* Private
*******************************************************************/
private void greeting() throws IOException {
writeLine("Welcome to the j6502 Simulator!");
}
private void prompt() throws IOException {
m_out.write("> ");
m_out.flush();
}
private String readLine() throws IOException {
String line = m_in.readLine();
if (line == null) { return null; }
return line.trim();
}
private void writeLine(String line) throws IOException {
m_out.write(line);
m_out.newLine();
m_out.flush();
}
/**
* Returns true if the line is a quit.
*/
private boolean shouldQuit(String line) {
return (line == null || "q".equals(line.toLowerCase()));
}
}