PageRenderTime 59ms CodeModel.GetById 50ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/bsh/CallStack.java

#
Java | 123 lines | 52 code | 14 blank | 57 comment | 3 complexity | 21d721d2e6ff716f746a4712b1081ea1 MD5 | raw file
  1/*****************************************************************************
  2 *                                                                           *
  3 *  This file is part of the BeanShell Java Scripting distribution.          *
  4 *  Documentation and updates may be found at http://www.beanshell.org/      *
  5 *                                                                           *
  6 *  Sun Public License Notice:                                               *
  7 *                                                                           *
  8 *  The contents of this file are subject to the Sun Public License Version  *
  9 *  1.0 (the "License"); you may not use this file except in compliance with *
 10 *  the License. A copy of the License is available at http://www.sun.com    * 
 11 *                                                                           *
 12 *  The Original Code is BeanShell. The Initial Developer of the Original    *
 13 *  Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright     *
 14 *  (C) 2000.  All Rights Reserved.                                          *
 15 *                                                                           *
 16 *  GNU Public License Notice:                                               *
 17 *                                                                           *
 18 *  Alternatively, the contents of this file may be used under the terms of  *
 19 *  the GNU Lesser General Public License (the "LGPL"), in which case the    *
 20 *  provisions of LGPL are applicable instead of those above. If you wish to *
 21 *  allow use of your version of this file only under the  terms of the LGPL *
 22 *  and not to allow others to use your version of this file under the SPL,  *
 23 *  indicate your decision by deleting the provisions above and replace      *
 24 *  them with the notice and other provisions required by the LGPL.  If you  *
 25 *  do not delete the provisions above, a recipient may use your version of  *
 26 *  this file under either the SPL or the LGPL.                              *
 27 *                                                                           *
 28 *  Patrick Niemeyer (pat@pat.net)                                           *
 29 *  Author of Learning Java, O'Reilly & Associates                           *
 30 *  http://www.pat.net/~pat/                                                 *
 31 *                                                                           *
 32 *****************************************************************************/
 33
 34package bsh;
 35
 36import java.util.Vector;
 37
 38/**
 39	A stack of namespaces representing the call path.
 40	The top of the stack is always the current namespace of evaluation.
 41
 42	This is necessary to support this this.caller magic reference and will
 43	also be used to provide additional debug/tracking and error reporting
 44	information in the future.
 45
 46	Note: it would be awefully nice to use the java.util.Stack here.
 47	Sigh... have to stay 1.1 compatible.
 48
 49	We don't want to serialize this, do we?  It should be ephemeral, like
 50	the interpreter reference I think.
 51*/
 52public class CallStack /*implements java.io.Serializable*/
 53{
 54	private Vector stack = new Vector(2);
 55
 56	public void clear() {
 57		stack.removeAllElements();
 58	}
 59
 60	public void push( NameSpace ns ) {
 61		stack.insertElementAt( ns, 0 );
 62	}
 63
 64	public NameSpace top() {
 65		return get(0);
 66	}
 67
 68	/**
 69		zero based.
 70	*/
 71	public NameSpace get(int depth) {
 72		if ( depth >= depth() )
 73			return NameSpace.JAVACODE;
 74		else
 75			return (NameSpace)(stack.elementAt(depth));
 76	}
 77	
 78	/**
 79		This is kind of crazy, but used by the setNameSpace command.
 80		zero based.
 81	*/
 82	public void set(int depth, NameSpace ns) {
 83		stack.setElementAt(ns, depth );
 84	}
 85
 86	public NameSpace pop() {
 87		if ( depth() < 1 )
 88			throw new InterpreterError("pop on empty CallStack");
 89		NameSpace top = top();
 90		stack.removeElementAt(0);
 91		return top;
 92	}
 93
 94	/**
 95		Swap in the value as the new top of the stack and return the old
 96		value.
 97	*/
 98	public NameSpace swap( NameSpace newTop ) {
 99		NameSpace oldTop = (NameSpace)(stack.elementAt(0));
100		stack.setElementAt( newTop, 0 );
101		return oldTop;
102	}
103
104	public int depth() {
105		return stack.size();
106	}
107
108	public NameSpace [] toArray() {
109		NameSpace [] nsa = new NameSpace [ depth() ];
110		stack.copyInto( nsa );
111		return nsa;
112	}
113
114	public String toString() {
115		StringBuffer sb = new StringBuffer();
116		sb.append("CallStack:\n");
117		NameSpace [] nsa = toArray();
118		for(int i=0; i<nsa.length; i++)
119			sb.append("\t"+nsa[i]+"\n");
120
121		return sb.toString();
122	}
123}