KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bsh > BSHForStatement


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
34
35 package bsh;
36
37 /**
38     Implementation of the for(;;) statement.
39 */

40 class BSHForStatement extends SimpleNode implements ParserConstants
41 {
42     public boolean hasForInit;
43     public boolean hasExpression;
44     public boolean hasForUpdate;
45
46     private SimpleNode forInit;
47     private SimpleNode expression;
48     private SimpleNode forUpdate;
49     private SimpleNode statement;
50
51     private boolean parsed;
52
53     BSHForStatement(int id) { super(id); }
54
55     public Object JavaDoc eval(CallStack callstack , Interpreter interpreter)
56         throws EvalError
57     {
58         int i = 0;
59         if(hasForInit)
60             forInit = ((SimpleNode)jjtGetChild(i++));
61         if(hasExpression)
62             expression = ((SimpleNode)jjtGetChild(i++));
63         if(hasForUpdate)
64             forUpdate = ((SimpleNode)jjtGetChild(i++));
65         if(i < jjtGetNumChildren()) // should normally be
66
statement = ((SimpleNode)jjtGetChild(i));
67
68         NameSpace enclosingNameSpace= callstack.top();
69         BlockNameSpace forNameSpace = new BlockNameSpace( enclosingNameSpace );
70
71         /*
72             Note: some interesting things are going on here.
73
74             1) We swap instead of push... The primary mode of operation
75             acts like we are in the enclosing namespace... (super must be
76             preserved, etc.)
77
78             2) We do *not* call the body block eval with the namespace
79             override. Instead we allow it to create a second subordinate
80             BlockNameSpace child of the forNameSpace. Variable propogation
81             still works through the chain, but the block's child cleans the
82             state between iteration.
83             (which is correct Java behavior... see forscope4.bsh)
84         */

85
86         // put forNameSpace it on the top of the stack
87
// Note: it's important that there is only one exit point from this
88
// method so that we can swap back the namespace.
89
callstack.swap( forNameSpace );
90
91         // Do the for init
92
if ( hasForInit )
93             forInit.eval( callstack, interpreter );
94
95         Object JavaDoc returnControl = Primitive.VOID;
96         while(true)
97         {
98             if ( hasExpression )
99             {
100                 boolean cond = BSHIfStatement.evaluateCondition(
101                     expression, callstack, interpreter );
102
103                 if ( !cond )
104                     break;
105             }
106
107             boolean breakout = false; // switch eats a multi-level break here?
108
if ( statement != null ) // not empty statement
109
{
110                 // do *not* invoke special override for block... (see above)
111
Object JavaDoc ret = statement.eval( callstack, interpreter );
112
113                 if (ret instanceof ReturnControl)
114                 {
115                     switch(((ReturnControl)ret).kind)
116                     {
117                         case RETURN:
118                             returnControl = ret;
119                             breakout = true;
120                             break;
121
122                         case CONTINUE:
123                             break;
124
125                         case BREAK:
126                             breakout = true;
127                             break;
128                     }
129                 }
130             }
131
132             if ( breakout )
133                 break;
134
135             if ( hasForUpdate )
136                 forUpdate.eval( callstack, interpreter );
137         }
138
139         callstack.swap( enclosingNameSpace ); // put it back
140
return returnControl;
141     }
142
143 }
144
Popular Tags