import java.io.InputStreamReader; import java.io.FileNotFoundException; import java.io.BufferedReader; import java.io.IOException; /** * This is a front end for executing a SaM program from a console */ public class SamText implements Video { public static void main(String[] args) { String fname = null; Program prg; SamText txt = new SamText(); Sys sys = new Sys(); Processor cpu = sys.cpu(); Memory mem = sys.mem(); sys.setVideo(txt); int il = -1, tl = -1; //limits /* Argument parser */ boolean err = false; for (int a = 0; a < args.length; a++) { if (args[a].equals("+tl") && args.length > a + 1) //time limit try { tl = Integer.parseInt(args[++a]); } catch (NumberFormatException e) { err = true; break; } else if (args[a].equals("+il") && args.length > a + 1) //instruction limit try { il = Integer.parseInt(args[++a]); } catch (NumberFormatException e) { err = true; break; } else if (args[a].equals("+il") || args[a].equals("+tl")) //limit without argument { err = true; break; } else if (fname == null) //filename { fname = args[a]; } else { err = true; break; } // 2nd string } if (err) { System.err.println("Usage: java SamText "); System.err.println("If the options are omitted, the program runs without limits."); System.err.println("If the filename is omitted, System.in is used for input. "); System.err.println("Options: +tl : Time limit in milliseconds."); System.err.println("Options: +il : Instruction limit."); return; } try { if (fname != null) prg = SamAssembler.assemble(fname); else { System.out.println("Type SAM Code, EOF to end. "); System.out.println("(CTRL-D on Unix/Minix/Linux/OS X, CTRL-Z on DOS/Windows/Longhorn)" ); System.out.println("============================"); prg = SamAssembler.assemble(new InputStreamReader(System.in)); } System.out.println("Program assembled."); cpu.load(prg); System.out.println("Program loaded. Executing."); System.out.println("=========================="); final boolean ilim = (il < 0) ? false : true; //instruction limit on? final boolean tlim = (tl < 0) ? false : true; //time limit on? /* Execute */ if (!ilim && !tlim) cpu.run(); else { long start = 0; if (tlim) start = System.currentTimeMillis(); while (cpu.get(Processor.HALT) == 0) { cpu.step(); if (ilim && il-- == 0) throw new SystemException("Program exceeded instruction limit. Terminating."); if (tlim && (System.currentTimeMillis() - start > tl)) throw new SystemException("Program exceeded time limit. Terminating."); } } /* Exit code */ if (mem.getType(0) == Memory.FLOAT) System.out.println("Exit Status: " + Float.intBitsToFloat(mem.getMem(0))); else if (mem.getType(0) == Memory.CH) System.out.println("Exit Status: " + (char)mem.getMem(0)); else System.out.println("Exit Status: " + mem.getMem(0)); } catch (AssemblerException e) { System.err.println("Assembler Error: " + e); return; } catch (TokenizerException e) { System.err.println("Tokenizer Error: " + e); return; } catch (FileNotFoundException e) { System.err.println("File not found: " + e); return; } catch (SystemException e) { System.err.println("Stack Machine Error: " + e); return; } catch (Exception e) { System.err.println("Internal Error, please report to the SaM Development Group " + e); e.printStackTrace(System.err); return; } } /* Video interface methods */ private final static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); public int readInt() { while (true) { try { System.out.print("Processor Input (enter integer): "); return Integer.parseInt(in.readLine()); } catch (NumberFormatException e) { continue; } catch (IOException e) { return 0; } } } public String readString() { try { System.out.print("Processor Input (enter string): "); return in.readLine(); } catch (IOException e) { return ""; } } public char readChar() { while (true) { try { System.out.print("Processor Input (enter character): "); String s = in.readLine(); if(s != null && s.length() >= 1) return s.charAt(0); } catch (IOException e) { return 0; } } } public float readFloat() { while (true) { try { System.out.print("Processor Input (enter float): "); return Float.parseFloat(in.readLine()); } catch (NumberFormatException e) { continue; } catch (IOException e) { return 0; } } } public void writeInt(int a) { System.out.println("Processor Output: " + a); } public void writeFloat(float a) { System.out.println("Processor Output: " + a); } public void writeChar(char a) { System.out.println("Processor Output: " + a); } public void writeString(String a) { System.out.println("Processor Output: " + a); } }