public static Integer keystrokeInput() throws InterruptedException, IOException { Console console = System.console(); Reader reader = console.reader(); try { Runtime.getRuntime().exec(cmd).waitFor(); Integer keystroke = Integer.valueOf(reader.read()); Runtime.getRuntime().exec(undo_cmd).waitFor(); return keystroke; } catch (java.io.IOException e) { Runtime.getRuntime().exec(undo_cmd).waitFor(); return null; } }
public static Integer keystrokeInput(ArrayList<Integer> allowedKeystrokes) throws InterruptedException, IOException { Console console = System.console(); Reader reader = console.reader(); try { try { Runtime.getRuntime().exec(cmd).waitFor(); if (Thread.interrupted()) { throw new java.io.InterruptedIOException(); } } catch (InterruptedException u) { } Integer keystroke = Integer.valueOf(reader.read()); if (allowedKeystrokes.contains(keystroke) == false) { throw new java.io.IOException(); } Runtime.getRuntime().exec(undo_cmd).waitFor(); return keystroke; } catch (java.io.IOException e) { try { Runtime.getRuntime().exec(undo_cmd).waitFor(); if (Thread.interrupted()) { throw new java.lang.InterruptedException(); } } catch (InterruptedException o) { o.printStackTrace(); } return null; } }
@Override public Reader reader() throws ConsoleException { return console.reader(); }
public void interpreteAndRun(String lines, InterpreterLanguage lingo) { language = lingo; Console console = System.console(); if (compile(lines)) { // Run the code. int PROGRAM_COUNTER = 0; int TAPE_PTR = 0; while (PROGRAM_COUNTER >= 0 && PROGRAM_COUNTER < program.size()) { switch (program.get(PROGRAM_COUNTER)) { case MOVE_MEM_PTR_TO_NEXT_CELL: TAPE_PTR++; break; case MOVE_MEM_PTR_TO_PREV_CELL: TAPE_PTR--; break; case CELL_VALUE_INCREMENT: tape[TAPE_PTR]++; break; case CELL_VALUE_DECREMENT: tape[TAPE_PTR]--; break; case CELL_VALUE_OUTPUT_CHAR_ASCII: byte b = tape[TAPE_PTR]; char c = (char) b; System.out.print(c); System.out.flush(); break; case CELL_VALUE_READ_BYTE: try { int read = console.reader().read(); tape[TAPE_PTR] = (byte) read; } catch (Exception e) { } break; case WHILE_CELL_VALUE_NON_ZERO_START: if (tape[TAPE_PTR] == 0) { while (PROGRAM_COUNTER < program.size()) { Command com = program.get(PROGRAM_COUNTER); if (Command.WHILE_END == com) { break; } PROGRAM_COUNTER++; } } break; case WHILE_END: if (tape[TAPE_PTR] != 0) { while (PROGRAM_COUNTER >= 0) { Command com = program.get(PROGRAM_COUNTER); if (Command.WHILE_CELL_VALUE_NON_ZERO_START == com) { PROGRAM_COUNTER--; break; } PROGRAM_COUNTER--; } } break; } PROGRAM_COUNTER++; } } }