1 /***************************************************************************** 2 * Licensed to the Apache Software Foundation (ASF) under one * 3 * or more contributor license agreements. See the NOTICE file * 4 * distributed with this work for additional information * 5 * regarding copyright ownership. The ASF licenses this file * 6 * to you under the Apache License, Version 2.0 (the * 7 * "License"); you may not use this file except in compliance * 8 * with the License. You may obtain a copy of the License at * 9 * * 10 * http://www.apache.org/licenses/LICENSE-2.0 * 11 * * 12 * Unless required by applicable law or agreed to in writing, * 13 * software distributed under the License is distributed on an * 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * 15 * KIND, either express or implied. See the License for the * 16 * specific language governing permissions and limitations * 17 * under the License. * 18 * * 19 * This file is part of the BeanShell Java Scripting distribution. * 20 * Documentation and updates may be found at http://www.beanshell.org/ * 21 * Patrick Niemeyer (pat@pat.net) * 22 * Author of Learning Java, O'Reilly & Associates * 23 * * 24 *****************************************************************************/ 25 26 27 package bsh; 28 29 /** 30 Implementation of the for(;;) statement. 31 */ 32 class BSHForStatement extends SimpleNode implements ParserConstants 33 { 34 public boolean hasForInit; 35 public boolean hasExpression; 36 public boolean hasForUpdate; 37 38 private SimpleNode forInit; 39 private SimpleNode expression; 40 private SimpleNode forUpdate; 41 private SimpleNode statement; 42 43 private boolean parsed; 44 BSHForStatement(int id)45 BSHForStatement(int id) { super(id); } 46 eval(CallStack callstack , Interpreter interpreter)47 public Object eval(CallStack callstack , Interpreter interpreter) 48 throws EvalError 49 { 50 int i = 0; 51 if(hasForInit) 52 forInit = ((SimpleNode)jjtGetChild(i++)); 53 if(hasExpression) 54 expression = ((SimpleNode)jjtGetChild(i++)); 55 if(hasForUpdate) 56 forUpdate = ((SimpleNode)jjtGetChild(i++)); 57 if(i < jjtGetNumChildren()) // should normally be 58 statement = ((SimpleNode)jjtGetChild(i)); 59 60 NameSpace enclosingNameSpace= callstack.top(); 61 BlockNameSpace forNameSpace = new BlockNameSpace( enclosingNameSpace ); 62 63 /* 64 Note: some interesting things are going on here. 65 66 1) We swap instead of push... The primary mode of operation 67 acts like we are in the enclosing namespace... (super must be 68 preserved, etc.) 69 70 2) We do *not* call the body block eval with the namespace 71 override. Instead we allow it to create a second subordinate 72 BlockNameSpace child of the forNameSpace. Variable propagation 73 still works through the chain, but the block's child cleans the 74 state between iteration. 75 (which is correct Java behavior... see forscope4.bsh) 76 */ 77 78 // put forNameSpace it on the top of the stack 79 // Note: it's important that there is only one exit point from this 80 // method so that we can swap back the namespace. 81 callstack.swap( forNameSpace ); 82 83 // Do the for init 84 if ( hasForInit ) 85 forInit.eval( callstack, interpreter ); 86 87 Object returnControl = Primitive.VOID; 88 while(true) 89 { 90 if ( hasExpression ) 91 { 92 boolean cond = BSHIfStatement.evaluateCondition( 93 expression, callstack, interpreter ); 94 95 if ( !cond ) 96 break; 97 } 98 99 boolean breakout = false; // switch eats a multi-level break here? 100 if ( statement != null ) // not empty statement 101 { 102 // do *not* invoke special override for block... (see above) 103 Object ret = statement.eval( callstack, interpreter ); 104 105 if (ret instanceof ReturnControl) 106 { 107 switch(((ReturnControl)ret).kind) 108 { 109 case RETURN: 110 returnControl = ret; 111 breakout = true; 112 break; 113 114 case CONTINUE: 115 break; 116 117 case BREAK: 118 breakout = true; 119 break; 120 } 121 } 122 } 123 124 if ( breakout ) 125 break; 126 127 if ( hasForUpdate ) 128 forUpdate.eval( callstack, interpreter ); 129 } 130 131 callstack.swap( enclosingNameSpace ); // put it back 132 return returnControl; 133 } 134 135 } 136