PageRenderTime 65ms CodeModel.GetById 54ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 106 lines | 78 code | 17 blank | 11 comment | 13 complexity | 703a75db8f40c12aedba78b63b3f9c53 MD5 | raw file
  1package org.gjt.sp.jedit.bsh;
  2
  3// Just testing...
  4
  5/**
  6	 Implementation of the enhanced for(:) statement.  
  7	 This statement uses BshIterable to support iteration over a wide variety
  8	 of iterable types.  Under JDK 1.1 this statement supports primitive and 
  9	 Object arrays, Vectors, and enumerations.  Under JDK 1.2 and later it 
 10	 additionally supports collections.
 11
 12	 @author Daniel Leuck 
 13	 @author Pat Niemeyer
 14*/
 15class BSHEnhancedForStatement extends SimpleNode implements ParserConstants 
 16{
 17	String varName;
 18
 19    BSHEnhancedForStatement(int id) { super(id); }
 20
 21    public Object eval( CallStack callstack , Interpreter interpreter )
 22		throws EvalError 
 23	{
 24		Class elementType = null;
 25		SimpleNode expression, statement=null;
 26
 27		NameSpace enclosingNameSpace = callstack.top();
 28		SimpleNode firstNode =((SimpleNode)jjtGetChild(0));
 29		int nodeCount = jjtGetNumChildren();
 30		
 31		if ( firstNode instanceof BSHType ) 
 32		{
 33			elementType=((BSHType)firstNode).getType( callstack, interpreter );
 34			expression=((SimpleNode)jjtGetChild(1));
 35			if ( nodeCount>2 )
 36				statement=((SimpleNode)jjtGetChild(2));
 37		} else 
 38		{
 39			expression=firstNode;
 40			if ( nodeCount>1 )
 41				statement=((SimpleNode)jjtGetChild(1));
 42		}
 43
 44		BlockNameSpace eachNameSpace = new BlockNameSpace( enclosingNameSpace );
 45		callstack.swap( eachNameSpace );
 46
 47		final Object iteratee = expression.eval( callstack, interpreter );
 48
 49		if ( iteratee == Primitive.NULL )
 50			throw new EvalError("The collection, array, map, iterator, or " +
 51				"enumeration portion of a for statement cannot be null.", 
 52				this, callstack );
 53
 54		CollectionManager cm = CollectionManager.getCollectionManager();
 55		if ( !cm.isBshIterable( iteratee ) )
 56			throw new EvalError("Can't iterate over type: "
 57				+iteratee.getClass(), this, callstack );
 58		BshIterator iterator = cm.getBshIterator( iteratee );
 59		
 60		Object returnControl = Primitive.VOID;
 61        while( iterator.hasNext() )
 62        {
 63			try {
 64			if ( elementType != null )
 65				eachNameSpace.setTypedVariable(
 66					varName/*name*/, elementType/*type*/,
 67					iterator.next()/*value*/, new Modifiers()/*none*/ );
 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}