PageRenderTime 64ms CodeModel.GetById 58ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/bsh/BSHBlock.java

#
Java | 149 lines | 79 code | 20 blank | 50 comment | 15 complexity | 7f7c64b6a27bc41de4a37120524eca18 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
 34
 35package org.gjt.sp.jedit.bsh;
 36
 37class BSHBlock extends SimpleNode
 38{
 39	public boolean isSynchronized = false;
 40
 41	BSHBlock(int id) { super(id); }
 42
 43	public Object eval( CallStack callstack, Interpreter interpreter) 
 44		throws EvalError
 45	{
 46		return eval( callstack, interpreter, false );
 47	}
 48
 49	/**
 50		@param overrideNamespace if set to true the block will be executed
 51		in the current namespace (not a subordinate one).
 52		<p>
 53		If true *no* new BlockNamespace will be swapped onto the stack and 
 54		the eval will happen in the current
 55		top namespace.  This is used by BshMethod, TryStatement, etc.  
 56		which must intialize the block first and also for those that perform 
 57		multiple passes in the same block.
 58	*/
 59	public Object eval( 
 60		CallStack callstack, Interpreter interpreter, 
 61		boolean overrideNamespace ) 
 62		throws EvalError
 63	{
 64		Object syncValue = null;
 65		if ( isSynchronized ) 
 66		{
 67			// First node is the expression on which to sync
 68			SimpleNode exp = ((SimpleNode)jjtGetChild(0));
 69			syncValue = exp.eval(callstack, interpreter);
 70		}
 71
 72		Object ret;
 73		if ( isSynchronized ) // Do the actual synchronization
 74			synchronized( syncValue )
 75			{
 76				ret = evalBlock( 
 77					callstack, interpreter, overrideNamespace, null/*filter*/);
 78			}
 79		else
 80				ret = evalBlock( 
 81					callstack, interpreter, overrideNamespace, null/*filter*/ );
 82
 83		return ret;
 84	}
 85
 86	Object evalBlock( 
 87		CallStack callstack, Interpreter interpreter, 
 88		boolean overrideNamespace, NodeFilter nodeFilter ) 
 89		throws EvalError
 90	{	
 91		Object ret = Primitive.VOID;
 92		NameSpace enclosingNameSpace = null;
 93		if ( !overrideNamespace ) 
 94		{
 95			enclosingNameSpace= callstack.top();
 96			BlockNameSpace bodyNameSpace = 
 97				new BlockNameSpace( enclosingNameSpace );
 98
 99			callstack.swap( bodyNameSpace );
100		}
101
102		int startChild = isSynchronized ? 1 : 0;
103		int numChildren = jjtGetNumChildren();
104
105		try {
106			/*
107				Evaluate block in two passes: 
108				First do class declarations then do everything else.
109			*/
110			for(int i=startChild; i<numChildren; i++)
111			{
112				SimpleNode node = ((SimpleNode)jjtGetChild(i));
113
114				if ( nodeFilter != null && !nodeFilter.isVisible( node ) )
115					continue;
116
117				if ( node instanceof BSHClassDeclaration )
118					node.eval( callstack, interpreter );
119			}
120			for(int i=startChild; i<numChildren; i++)
121			{
122				SimpleNode node = ((SimpleNode)jjtGetChild(i));
123				if ( node instanceof BSHClassDeclaration )
124					continue;
125
126				// filter nodes
127				if ( nodeFilter != null && !nodeFilter.isVisible( node ) )
128					continue;
129
130				ret = node.eval( callstack, interpreter );
131
132				// statement or embedded block evaluated a return statement
133				if ( ret instanceof ReturnControl )
134					break;
135			}
136		} finally {
137			// make sure we put the namespace back when we leave.
138			if ( !overrideNamespace ) 
139				callstack.swap( enclosingNameSpace );
140		}
141		return ret;
142	}
143
144	public interface NodeFilter {
145		public boolean isVisible( SimpleNode node );
146	}
147
148}
149