/* June 2010 */ import java.io.*; public class Machine { private int peep; private StringBuffer workspace; private Stack stack; /** the maximum length of the tape */ private static int LENGTH = 100; /** the tape where attributes are stored */ private StringBuffer[] tape; /** pointer to the current tape element */ private int pointer; /** the text input stream */ private Reader input; /** whether the end of the file has been reached **/ private boolean eof; /** the result of the last test instruction */ private boolean flag; public Machine(InputStream in) { Reader reader = new InputStreamReader(in); this.input = new BufferedReader(reader); this.eof = false; this.flag = false; this.workspace = new StringBuffer(""); this.stack = new Stack(""); this.pointer = 0; this.tape = new StringBuffer[LENGTH]; for (int ii = 0; ii < this.tape.length; ii++) { this.tape[ii] = new StringBuffer(); } try { this.peep = this.input.read(); } catch (java.io.IOException ex) { System.out.println("read error"); System.exit(-1); } } /** set the flag if the workspace equals the text */ public void testEquals(String sText) { this.flag = false; if (this.workspace.toString().equals(sText)) this.flag = true; } /** set the flag if the workspace ends with the text */ public void testEndsWith(String sText) { this.flag = false; if (this.workspace.toString().endsWith(sText)) this.flag = true; } /** set the flag if the workspace ends with the text */ public void testBeginsWith(String sText) { this.flag = false; if (this.workspace.toString().startsWith(sText)) this.flag = true; } /** set the flag if the workspace matches a term in a dictionary */ public void testDictionary(String sFileName) { } public String getWorkSpace() { return this.workspace.toString(); } public Character getPeep() { return new Character((char)this.peep); } public int getPeepCode() { return this.peep; } /** get the peep character in a visible format */ public String getVisiblePeep() { if (this.peep == '\n') { return "\\n"; } if (this.peep == '\r') { return "\\r"; } if (this.peep == '\t') { return "\\t"; } if (this.peep == ' ') { return "\\s"; } if (this.peep == -1) { return "-E"; } return (new Character((char)this.peep)).toString(); } // Workspace functions public void clear() { this.workspace.setLength(0); } /** clip one character from the end of the workspace */ public void clip() { if (this.workspace.length() == 0) return; this.workspace.delete(this.workspace.length() - 1, this.workspace.length()); } /** clip one character from the beginning of the workspace */ public void clop() { this.workspace.delete(0, 1); } public void add(String suffix) { this.workspace.append(suffix); } /** pop the first token from the stack into the workspace */ public void pop() { //System.out.println("pop " + this.stack.pop()); this.workspace.insert(0, this.stack.pop()); this.decrementTape(); } /** push the first token from the workspace to the stack */ public void push() { String sItem; int iFirstStar = this.workspace.indexOf("*"); if (iFirstStar != -1) { sItem = this.workspace.toString().substring(0, iFirstStar + 1); this.workspace.delete(0, iFirstStar + 1); } else { sItem = this.workspace.toString(); this.workspace.setLength(0); } this.stack.push(sItem); this.incrementTape(); } public Stack getStack() { return this.stack; } public String[] stackArray() { return this.stack.toArray(); } // Tape functions public int getPointer() { return this.pointer; } /** put the workspace in the current tape cell, overwrites */ public void put() { this.tape[this.pointer].setLength(0); this.tape[this.pointer].append(this.workspace); } /** append the current cell to the workspace */ public void get() { this.workspace.append(this.tape[this.pointer]); } /** increment the tape pointer by one */ public void incrementTape() { this.pointer++; if (this.pointer > this.LENGTH - 1) this.pointer = this.LENGTH - 1; } /** decrement the tape pointer by one */ public void decrementTape() { this.pointer--; if (this.pointer < 0) this.pointer = 0; } public void printState() { StringBuffer sReturn = new StringBuffer( "\n P:" + this.getPeep() + "|" + this.getVisiblePeep() + "|" + this.getPeepCode() + " W:" + this.workspace.toString() + "\n"); sReturn.append(" FLAG:" + this.flag + " EOF:" + this.eof + "\n"); sReturn.append(" S:" + this.stack.toString() + "\n"); sReturn.append(" T[" + this.pointer + "]:"); //String[] aaStack = this.stack.toArray(); for (int ii = 0; ii < this.pointer + 10; ii++) { sReturn.append(this.tape[ii] + "|"); } sReturn.append("..."); System.out.println(sReturn); } /** A test method to print out the whole input stream */ public void readAll() { int r; try { while ((r = this.input.read()) != -1) { char ch = (char) r; System.out.println("Do something with " + ch); } } catch (IOException ex) { } } /** read one character from the input stream and update the machine. */ public void read() { int iChar; try { if (this.eof) { System.exit(0); } this.workspace.append((char)this.peep); this.peep = this.input.read(); if (this.peep == -1) { this.eof = true; } } catch (IOException ex) { System.out.println("Error reading input stream" + ex); System.exit(-1); } } /** reads the input stream until the workspace end with text */ public void until(String sSuffix) { this.read(); while (!this.workspace.toString().endsWith(sSuffix) && !this.eof) { this.read(); } } /** show legal while classes */ public String getWhileClasses() { return "[1] digits, [a] letters, [a1], alphanumeric, [ ] space"; } /** reads the input stream until the workspace end with text */ public void whilePeep(String sClass) { if (sClass.equals("[1]")) { while (Character.isDigit(this.peep)) { this.read(); } return; } if (sClass.equals("[a1]")) { while (Character.isLetterOrDigit(this.peep)) { this.read(); } return; } if (sClass.equals("[a]")) { while (Character.isLetter(this.peep)) { this.read(); } return; } if (sClass.equals("[ ]")) { while (Character.isWhitespace(this.peep)) { this.read(); } return; } int iType = -1; int iPeepType; iPeepType = Character.getType((char)this.peep); System.out.println("type=" + iPeepType); while (iPeepType == iType) { this.read(); iPeepType = Character.getType((char)this.peep); } } public static void main(String[] args) { Machine mm = new Machine(System.in); mm.read(); } }