1 33 34 package bsh; 35 36 import java.util.Vector ; 37 38 62 public class CallStack 63 { 64 private Vector stack = new Vector (2); 65 66 public CallStack() { } 67 68 public CallStack( NameSpace namespace ) { 69 push( namespace ); 70 } 71 72 public void clear() { 73 stack.removeAllElements(); 74 } 75 76 public void push( NameSpace ns ) { 77 stack.insertElementAt( ns, 0 ); 78 } 79 80 public NameSpace top() { 81 return get(0); 82 } 83 84 87 public NameSpace get(int depth) { 88 if ( depth >= depth() ) 89 return NameSpace.JAVACODE; 90 else 91 return (NameSpace)(stack.elementAt(depth)); 92 } 93 94 98 public void set(int depth, NameSpace ns) { 99 stack.setElementAt(ns, depth ); 100 } 101 102 public NameSpace pop() { 103 if ( depth() < 1 ) 104 throw new InterpreterError("pop on empty CallStack"); 105 NameSpace top = top(); 106 stack.removeElementAt(0); 107 return top; 108 } 109 110 114 public NameSpace swap( NameSpace newTop ) { 115 NameSpace oldTop = (NameSpace)(stack.elementAt(0)); 116 stack.setElementAt( newTop, 0 ); 117 return oldTop; 118 } 119 120 public int depth() { 121 return stack.size(); 122 } 123 124 public NameSpace [] toArray() { 125 NameSpace [] nsa = new NameSpace [ depth() ]; 126 stack.copyInto( nsa ); 127 return nsa; 128 } 129 130 public String toString() { 131 StringBuffer sb = new StringBuffer (); 132 sb.append("CallStack:\n"); 133 NameSpace [] nsa = toArray(); 134 for(int i=0; i<nsa.length; i++) 135 sb.append("\t"+nsa[i]+"\n"); 136 137 return sb.toString(); 138 } 139 140 144 public CallStack copy() { 145 CallStack cs = new CallStack(); 146 cs.stack = (Vector )this.stack.clone(); 147 return cs; 148 } 149 } 150 | Popular Tags |