PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/bsh/BSHForStatement.java

#
Java | 143 lines | 68 code | 21 blank | 54 comment | 12 complexity | 4e396842da8bfcad16cf9192d0a57e77 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. /*****************************************************************************
  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. package bsh;
  34. /**
  35. Implementation of the for(;;) statement.
  36. */
  37. class BSHForStatement extends SimpleNode implements ParserConstants
  38. {
  39. public boolean hasForInit;
  40. public boolean hasExpression;
  41. public boolean hasForUpdate;
  42. private SimpleNode forInit;
  43. private SimpleNode expression;
  44. private SimpleNode forUpdate;
  45. private SimpleNode statement;
  46. private boolean parsed;
  47. BSHForStatement(int id) { super(id); }
  48. public Object eval(CallStack callstack , Interpreter interpreter)
  49. throws EvalError
  50. {
  51. int i = 0;
  52. if(hasForInit)
  53. forInit = ((SimpleNode)jjtGetChild(i++));
  54. if(hasExpression)
  55. expression = ((SimpleNode)jjtGetChild(i++));
  56. if(hasForUpdate)
  57. forUpdate = ((SimpleNode)jjtGetChild(i++));
  58. if(i < jjtGetNumChildren()) // should normally be
  59. statement = ((SimpleNode)jjtGetChild(i));
  60. NameSpace enclosingNameSpace= callstack.top();
  61. BlockNameSpace forNameSpace = new BlockNameSpace( enclosingNameSpace );
  62. /*
  63. Note: some interesting things are going on here.
  64. 1) We swap instead of push... The primary mode of operation
  65. acts like we are in the enclosing namespace... (super must be
  66. preserved, etc.)
  67. 2) We do *not* call the body block eval with the namespace
  68. override. Instead we allow it to create a second subordinate
  69. BlockNameSpace child of the forNameSpace. Variable propogation
  70. still works through the chain, but the block's child cleans the
  71. state between iteration.
  72. (which is correct Java behavior... see forscope4.bsh)
  73. */
  74. // put forNameSpace it on the top of the stack
  75. // Note: it's important that there is only one exit point from this
  76. // method so that we can swap back the namespace.
  77. callstack.swap( forNameSpace );
  78. // Do the for init
  79. if ( hasForInit )
  80. forInit.eval( callstack, interpreter );
  81. Object returnControl = Primitive.VOID;
  82. while(true)
  83. {
  84. if ( hasExpression )
  85. {
  86. boolean cond = BSHIfStatement.evaluateCondition(
  87. expression, callstack, interpreter );
  88. if ( !cond )
  89. break;
  90. }
  91. boolean breakout = false; // switch eats a multi-level break here?
  92. if ( statement != null ) // not empty statement
  93. {
  94. // do *not* invoke special override for block... (see above)
  95. Object ret = statement.eval( callstack, interpreter );
  96. if (ret instanceof ReturnControl)
  97. {
  98. switch(((ReturnControl)ret).kind)
  99. {
  100. case RETURN:
  101. returnControl = ret;
  102. breakout = true;
  103. break;
  104. case CONTINUE:
  105. break;
  106. case BREAK:
  107. breakout = true;
  108. break;
  109. }
  110. }
  111. }
  112. if ( breakout )
  113. break;
  114. if ( hasForUpdate )
  115. forUpdate.eval( callstack, interpreter );
  116. }
  117. callstack.swap( enclosingNameSpace ); // put it back
  118. return returnControl;
  119. }
  120. }