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