PageRenderTime 164ms CodeModel.GetById 158ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/bsh/BSHEnhancedForStatement.java

#
Java | 106 lines | 78 code | 17 blank | 11 comment | 13 complexity | 1d8392d69d8292708ca56311a363e028 MD5 | raw file
  1package bsh;
  2
  3// Just testing...
  4import java.util.*;
  5
  6/**
  7	 Implementation of the enhanced for(:) statement.  
  8	 This statement uses BshIterable to support iteration over a wide variety
  9	 of iterable types.  Under JDK 1.1 this statement supports primitive and 
 10	 Object arrays, Vectors, and enumerations.  Under JDK 1.2 and later it 
 11	 additionally supports collections.
 12
 13	 @author Daniel Leuck 
 14	 @author Pat Niemeyer
 15*/
 16class BSHEnhancedForStatement extends SimpleNode implements ParserConstants 
 17{
 18	String varName;
 19
 20    BSHEnhancedForStatement(int id) { super(id); }
 21
 22    public Object eval( CallStack callstack , Interpreter interpreter )
 23		throws EvalError 
 24	{
 25		Class elementType = null;
 26		SimpleNode expression, statement=null;
 27
 28		NameSpace enclosingNameSpace = callstack.top();
 29		SimpleNode firstNode =((SimpleNode)jjtGetChild(0));
 30		int nodeCount = jjtGetNumChildren();
 31		
 32		if ( firstNode instanceof BSHType ) 
 33		{
 34			elementType=((BSHType)firstNode).getType( callstack, interpreter );
 35			expression=((SimpleNode)jjtGetChild(1));
 36			if ( nodeCount>2 )
 37				statement=((SimpleNode)jjtGetChild(2));
 38		} else 
 39		{
 40			expression=firstNode;
 41			if ( nodeCount>1 )
 42				statement=((SimpleNode)jjtGetChild(1));
 43		}
 44
 45		BlockNameSpace eachNameSpace = new BlockNameSpace( enclosingNameSpace );
 46		callstack.swap( eachNameSpace );
 47
 48		final Object iteratee = expression.eval( callstack, interpreter );
 49
 50		if ( iteratee == Primitive.NULL )
 51			throw new EvalError("The collection, array, map, iterator, or " +
 52				"enumeration portion of a for statement cannot be null.", 
 53				this, callstack );
 54
 55		CollectionManager cm = CollectionManager.getCollectionManager();
 56		if ( !cm.isBshIterable( iteratee ) )
 57			throw new EvalError("Can't iterate over type: "
 58				+iteratee.getClass(), this, callstack );
 59		BshIterator iterator = cm.getBshIterator( iteratee );
 60		
 61		Object returnControl = Primitive.VOID;
 62        while( iterator.hasNext() )
 63        {
 64			try {
 65			if ( elementType != null )
 66				eachNameSpace.setTypedVariable(
 67					varName, elementType, iterator.next(), false );
 68			else
 69				eachNameSpace.setVariable( varName, iterator.next(), false );
 70			} catch ( UtilEvalError e ) {
 71				throw e.toEvalError(
 72					"for loop iterator variable:"+ varName, this, callstack );
 73			}
 74
 75            boolean breakout = false; // switch eats a multi-level break here?
 76            if ( statement != null ) // not empty statement
 77            {
 78                Object ret = statement.eval( callstack, interpreter );
 79
 80                if (ret instanceof ReturnControl)
 81                {
 82                    switch(((ReturnControl)ret).kind)
 83                    {
 84                        case RETURN:
 85							returnControl = ret;
 86							breakout = true;
 87                            break;
 88
 89                        case CONTINUE:
 90                            break;
 91
 92                        case BREAK:
 93                            breakout = true;
 94                            break;
 95                    }
 96                }
 97            }
 98
 99            if (breakout)
100                break;
101        }
102
103		callstack.swap(enclosingNameSpace);
104        return returnControl;
105    }
106}