+
+ static class Stack {
+ private static final int MAX_STACK_SIZE = 512;
+ private JS[] stack = new JS[64];
+ private int sp = 0;
+ public final void push(JS o) throws JSExn {
+ if(sp == stack.length) grow();
+ stack[sp++] = o;
+ }
+ public final JS peek() {
+ if(sp == 0) throw new RuntimeException("Stack underflow");
+ return stack[sp-1];
+ }
+ public final JS pop() {
+ if(sp == 0) throw new RuntimeException("Stack underflow");
+ return stack[--sp];
+ }
+ private void grow() throws JSExn {
+ if(stack.length >= MAX_STACK_SIZE) throw new JSExn("Stack overflow");
+ JS[] stack2 = new JS[stack.length * 2];
+ System.arraycopy(stack,0,stack2,0,stack.length);
+ }
+ // FIXME: Eliminate all uses of SWAP n>1 so we don't need this
+ public int size() { return sp; }
+ public void setElementAt(JS o, int i) { stack[i] = o; }
+ public JS elementAt(int i) { return stack[i]; }
+ }