/*********************************************************************************** * Freeman Lo * * This interpreter.java file interpretes assembler code which generated from * the Compiler.java and does its math stuff to get an answer to print off to screen. * *************************************************************************************/ import java.io.*; import java.util.*; class Overflow extends Exception { } class Underflow extends Exception { } class Interpreter { public static void main ( String [] args ) throws IOException { // this allows the use of the redirection symbol < BufferedReader br=null; br = new BufferedReader( new InputStreamReader(System.in)); // this allows the use of the redirection symbol > BufferedWriter bw = null; bw = new BufferedWriter( new OutputStreamWriter(System.out)); Scanner scan = new Scanner(System.in); String line="",numString="",tempString=""; int num1=0,num2=0,total=0; StringTokenizer token=null; Object x=null; Object y=null; // manually creating the stack obj Stack stak = new Stack(); // max_size = 64 unless i specify while ( (line=br.readLine()) != null ) { // if it sees the mul, add, div, sub if ( line.equalsIgnoreCase("mul") == true || line.equalsIgnoreCase("div") == true || line.equalsIgnoreCase("add") == true || line.equalsIgnoreCase("sub") == true || line.equalsIgnoreCase("prtpop") == true ) { if ( line.equalsIgnoreCase("add") == true ) { x = stak.topAndPop(); // top most y = stak.topAndPop(); // second most token = new StringTokenizer(x.toString()); token.nextToken(); numString = token.nextToken(); num1 = Integer.parseInt(numString); token = new StringTokenizer(y.toString()); token.nextToken(); numString = token.nextToken(); num2 = Integer.parseInt(numString); // calculate then push it back to the stack total = num1 + num2; tempString = "push " + total; try { stak.push(tempString); } catch ( Overflow of){ } } else if ( line.equalsIgnoreCase("sub") == true ) { x = stak.topAndPop(); // top most y = stak.topAndPop(); // second most token = new StringTokenizer(x.toString()); token.nextToken(); numString = token.nextToken(); num1 = Integer.parseInt(numString); token = new StringTokenizer(y.toString()); token.nextToken(); numString = token.nextToken(); num2 = Integer.parseInt(numString); // calculate then push it back to the stack total = num2 - num1; tempString = "push " + total; try { stak.push(tempString); } catch ( Overflow of){ } } else if ( line.equalsIgnoreCase("mul") == true ) { x = stak.topAndPop(); // top most y = stak.topAndPop(); // second most token = new StringTokenizer(x.toString()); token.nextToken(); numString = token.nextToken(); num1 = Integer.parseInt(numString); token = new StringTokenizer(y.toString()); token.nextToken(); numString = token.nextToken(); num2 = Integer.parseInt(numString); // calculate then push it back to the stack total = num1 * num2; tempString = "push " + total; try { stak.push(tempString); } catch ( Overflow of){ } } else if ( line.equalsIgnoreCase("div") == true ) { x = stak.topAndPop(); // top most y = stak.topAndPop(); // second most token = new StringTokenizer(x.toString()); token.nextToken(); numString = token.nextToken(); num1 = Integer.parseInt(numString); token = new StringTokenizer(y.toString()); token.nextToken(); numString = token.nextToken(); num2 = Integer.parseInt(numString); // calculate then push it back to the stack total = num2 / num1; tempString = "push " + total; try { stak.push(tempString); } catch ( Overflow of){ } } else if ( line.equalsIgnoreCase("prtpop") == true ) { System.out.println(total); } } // end big if else {// add to stack try { stak.push(line); } catch(Overflow of) { } } }// end while } // end main } // end class