// Joseph Bergin// Pace University// May 1996, April 1999//	CPU defines a stack oriented simulated cpu.  Variables have names that are a //	single char.  Variables must be declared before they can be used. //	All operations are done on the run time stack.  We may push an integer literal// 	or the current value of a variable.  We may pop a value to a variable.//	The arithmetic operations always pop the stack enough times to get the required//	number of operands.  The values are used in the computation.  Note that the first //	value popped is the rightmost operand for the operation. Thus//		aCpu.push(3);//		aCpu.push(5);//		aCpu.sub();//	will leave -2 at the top of the stack and the 3 and 5 will have been previously //	popped, so will no longer be in the stack.  //	The machine will halt on stack errors or on encountering an undeclared variable.//  This simple language has no jumps, so only simple programs may be written. import java.util.Random;import java.lang.Math;import java.awt.*;import java.applet.*;import java.util.*;import java.io.*;class SimpleCPUModel extends Thread{	public SimpleCPUModel(Prompter input, SimpleCPU control)	{	sysin = input;		controller = control;	}	public void declare(int c)	// declare a variable // initialized to zero	{	Integer C = new Integer(c);		if(!_storage.containsKey(C)) _storage.put(C, new Integer(0));	}	public void pushv(int c)		// push the current value of a variable	{	Integer C = new Integer(c);		Integer val = (Integer)_storage.get(C);		if(val == null)		 	System.out.println("No such variable: " + c);		else			_runStack.push(val);	}	public void pushi(int i)		// push a literal	{	_runStack.push(new Integer(i));	}	public void pop(int c)		// pop a value to a declared variable	{	Integer C = new Integer(c);		Integer val = (Integer)_storage.get(C);		if(val == null)		 	System.out.println("No such variable: " + c);		else			_storage.put(C, _runStack.pop());	}	public void add()		// add two top values(popping) and push the result	{	Integer second = (Integer)_runStack.pop();		Integer first = (Integer)_runStack.pop();		pushi(first.intValue() + second.intValue());	}	public void sub()		// top value is RIGHT operand	{	Integer second = (Integer)_runStack.pop();		Integer first = (Integer)_runStack.pop();		pushi(first.intValue() - second.intValue());	}	public void negate()	// negate the top of the stack (in place)	{	Integer first = (Integer)_runStack.pop();		pushi( - first.intValue() );	}	public void output()	// copy top of run time stack to output	{	System.out.println(((Integer)_runStack.peek()).intValue());		controller.show(((Integer)_runStack.peek()).intValue());	}	public void input()		// push a value from the input	{	int x = (int)sysin.getLong("An Int is required.");		pushi(x);	}	public void toss()		// discard top of run time stack	{	_runStack.pop();	}	public void dup()		// duplicate top of run time stack	{	_runStack.push(_runStack.peek());	}	public void halt()		// Halts your program (with a message).	{			System.out.println("Memory dump");		for(Enumeration E = _storage.keys(); E.hasMoreElements();)		{	Object k = E.nextElement();			Object v = _storage.get(k);				System.out.println(k.toString() + " " + v.toString());		}			System.out.println("CPU halted.");	}		// Two extensions to be made by students.		public boolean pos()	// true if top of stack is positive	{	return ((Integer)_runStack.peek()).intValue() > 0;	}	public boolean zero()	// true if top of stack is zero	{	return((Integer)_runStack.peek()).intValue() == 0;	}	/* These enable two permitted forms		if(pos()){ 		...		}  // conditional forward jump		and		do { 		...		} while (pos()); // conditional backward jum;		*/		private Hashtable _storage = new Hashtable(); // Address, Value pairs	private Stack _runStack = new Stack(); // of Integer objects	private Prompter sysin;	private SimpleCPU controller;		public void run()	{		int 	a = 0,					b = 1;	// Give names to our variables		declare		(a);		declare		(b);		input		();		output		();		pop			(a);		input		();		output		();		pop			(b);		pushv		(a);		dup			();		pushv		(b);		div			();		pushv		(b);		mult		();		sub			();		output		();		input();		halt		();	}}public class SimpleCPU extends Applet{	private Prompter sysin;	private SimpleCPUModel model;	private TextArea theOutput;		public static void main(String argv[])	{	Frame f = new Frame("CPU test");		f.setLayout(new FlowLayout());		SimpleCPU scpu = new SimpleCPU();		scpu.init();		f.add("Center", scpu);		f.setSize(100,100);		f.show();	}		public void show(int x)	{	theOutput.append('\n' + Integer.toString(x) );		repaint();	}		public void init()	{	theOutput = new TextArea("The Output\n", 20, 20);		theOutput.setEditable(false);		add(theOutput);		Component c = this;		while(!(c instanceof Frame)) c = c.getParent();		sysin = new Prompter((Frame)c);		model = new SimpleCPUModel(sysin, this);		model.start();	}}