PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms 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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. package org.gjt.sp.jedit.bsh;
  2. // Just testing...
  3. /**
  4. Implementation of the enhanced for(:) statement.
  5. This statement uses BshIterable to support iteration over a wide variety
  6. of iterable types. Under JDK 1.1 this statement supports primitive and
  7. Object arrays, Vectors, and enumerations. Under JDK 1.2 and later it
  8. additionally supports collections.
  9. @author Daniel Leuck
  10. @author Pat Niemeyer
  11. */
  12. class BSHEnhancedForStatement extends SimpleNode implements ParserConstants
  13. {
  14. String varName;
  15. BSHEnhancedForStatement(int id) { super(id); }
  16. public Object eval( CallStack callstack , Interpreter interpreter )
  17. throws EvalError
  18. {
  19. Class elementType = null;
  20. SimpleNode expression, statement=null;
  21. NameSpace enclosingNameSpace = callstack.top();
  22. SimpleNode firstNode =((SimpleNode)jjtGetChild(0));
  23. int nodeCount = jjtGetNumChildren();
  24. if ( firstNode instanceof BSHType )
  25. {
  26. elementType=((BSHType)firstNode).getType( callstack, interpreter );
  27. expression=((SimpleNode)jjtGetChild(1));
  28. if ( nodeCount>2 )
  29. statement=((SimpleNode)jjtGetChild(2));
  30. } else
  31. {
  32. expression=firstNode;
  33. if ( nodeCount>1 )
  34. statement=((SimpleNode)jjtGetChild(1));
  35. }
  36. BlockNameSpace eachNameSpace = new BlockNameSpace( enclosingNameSpace );
  37. callstack.swap( eachNameSpace );
  38. final Object iteratee = expression.eval( callstack, interpreter );
  39. if ( iteratee == Primitive.NULL )
  40. throw new EvalError("The collection, array, map, iterator, or " +
  41. "enumeration portion of a for statement cannot be null.",
  42. this, callstack );
  43. CollectionManager cm = CollectionManager.getCollectionManager();
  44. if ( !cm.isBshIterable( iteratee ) )
  45. throw new EvalError("Can't iterate over type: "
  46. +iteratee.getClass(), this, callstack );
  47. BshIterator iterator = cm.getBshIterator( iteratee );
  48. Object returnControl = Primitive.VOID;
  49. while( iterator.hasNext() )
  50. {
  51. try {
  52. if ( elementType != null )
  53. eachNameSpace.setTypedVariable(
  54. varName/*name*/, elementType/*type*/,
  55. iterator.next()/*value*/, new Modifiers()/*none*/ );
  56. else
  57. eachNameSpace.setVariable( varName, iterator.next(), false );
  58. } catch ( UtilEvalError e ) {
  59. throw e.toEvalError(
  60. "for loop iterator variable:"+ varName, this, callstack );
  61. }
  62. boolean breakout = false; // switch eats a multi-level break here?
  63. if ( statement != null ) // not empty statement
  64. {
  65. Object ret = statement.eval( callstack, interpreter );
  66. if (ret instanceof ReturnControl)
  67. {
  68. switch(((ReturnControl)ret).kind)
  69. {
  70. case RETURN:
  71. returnControl = ret;
  72. breakout = true;
  73. break;
  74. case CONTINUE:
  75. break;
  76. case BREAK:
  77. breakout = true;
  78. break;
  79. }
  80. }
  81. }
  82. if (breakout)
  83. break;
  84. }
  85. callstack.swap(enclosingNameSpace);
  86. return returnControl;
  87. }
  88. }