import java.io.*; /** A class to test the machine from the command line */ public class mtest { public static void printHelp() { StringBuffer text = new StringBuffer(); text.append( "h - display commands \n" + "r - read one character from the input stream \n" + "R - reset the input stream \n" + "u xx - read the input until the workspace ends with 'xx'\n" + "w - read the input while the peep is \n" + "W - show the legal character classes usable with 'w'\n" + "teq - test if the workspace is equal to the text \n" + "te - test if the workspace ends with the text \n" + "ts - test if the workspace start with the text \n" + "pp - print the machine \n" + "ps - print the machine storage \n" + "++ - increment the tape pointer \n" + "-- - decrement the tape pointer \n" + "g - append the current tape cell to the workspace \n" + "G - put the workspace into the current tape cell \n" + "d - clear the workspace \n" + "clip - clip one character from the end of the workspace \n" + "clop - clip one character from the beginning of the workspace \n" + "a xx - add 'xx' to the workspace \n" + "p - pop the stack into the workspace \n" + "P - push the workspace onto \n" + "q - exit \n" ); System.out.print(text); } public static void main(String[] args) throws Exception { String text = new String("on1234 \t e*two*\"quote\"023three*vvv"); InputStream is = new ByteArrayInputStream(text.getBytes()); is.mark(1000); //getBytes("UTF-8"); System.out.println("Testing the machine"); System.out.println("with text: " + text); Machine mm = new Machine(is); String input = new String("h"); String parameter = new String(""); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); while (!input.equals("q")) { if (input.equals("r")) { mm.read(); } if (input.equals("R")) { is.reset(); } if (input.startsWith("u")) { parameter = input.substring(1).trim(); //System.out.println("p:" + parameter); mm.until(parameter); } if (input.startsWith("w")) { parameter = input.substring(1).trim(); mm.whilePeep(parameter); } if (input.equals("W")) { System.out.println(mm.getWhileClasses()); } if (input.startsWith("teq")) { parameter = input.substring(3).trim(); mm.testEquals(parameter); } else if (input.startsWith("te")) { parameter = input.substring(2).trim(); mm.testEndsWith(parameter); } else if (input.startsWith("ts")) { parameter = input.substring(2).trim(); mm.testBeginsWith(parameter); } if (input.equals("pp")) { mm.printState(); } if (input.equals("ps")) { //mm.printStorage(); } if (input.equals("++")) { mm.incrementTape(); } if (input.equals("--")) { mm.decrementTape(); } if (input.equals("g")) { mm.get(); } if (input.equals("G")) { mm.put(); } if (input.equals("clip")) { mm.clip(); } if (input.equals("clop")) { mm.clop(); } if (input.startsWith("a")) { parameter = input.substring(1).trim(); mm.add(parameter); } if (input.equals("d")) { mm.clear(); } //Stack commands if (input.equals("p")) { mm.pop(); } if (input.equals("P")) { mm.push(); } if (input.equals("h")) { mtest.printHelp(); } System.out.println("\nparam:" + parameter + "\ntext:" + text); mm.printState(); System.out.print(">"); input = in.readLine().trim(); } System.out.println("goodbye!"); } }