KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bsh > BSHWhileStatement


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     This class handles both while(){} statements and do{}while() statements.
39 */

40 class BSHWhileStatement extends SimpleNode implements ParserConstants
41 {
42     public boolean isDoStatement;
43
44     BSHWhileStatement(int id) { super(id); }
45
46     public Object JavaDoc eval( CallStack callstack, Interpreter interpreter)
47         throws EvalError
48     {
49         int numChild = jjtGetNumChildren();
50
51         // Order of body and condition is swapped for do / while
52
SimpleNode condExp, body = null;
53
54         if ( isDoStatement ) {
55             condExp = (SimpleNode)jjtGetChild(1);
56             body =(SimpleNode)jjtGetChild(0);
57         } else {
58             condExp = (SimpleNode)jjtGetChild(0);
59             if ( numChild > 1 ) // has body, else just for side effects
60
body =(SimpleNode)jjtGetChild(1);
61         }
62
63         boolean doOnceFlag = isDoStatement;
64         while(
65             doOnceFlag ||
66             BSHIfStatement.evaluateCondition(condExp, callstack, interpreter )
67         )
68         {
69             if ( body == null ) // no body?
70
continue;
71
72             Object JavaDoc ret = body.eval(callstack, interpreter);
73
74             boolean breakout = false;
75             if(ret instanceof ReturnControl)
76             {
77                 switch(((ReturnControl)ret).kind )
78                 {
79                     case RETURN:
80                         return ret;
81
82                     case CONTINUE:
83                         continue;
84
85                     case BREAK:
86                         breakout = true;
87                         break;
88                 }
89             }
90             if(breakout)
91                 break;
92
93             doOnceFlag = false;
94         }
95
96         return Primitive.VOID;
97     }
98
99 }
100
Popular Tags