01: /*****************************************************************************
02: * *
03: * This file is part of the BeanShell Java Scripting distribution. *
04: * Documentation and updates may be found at http://www.beanshell.org/ *
05: * *
06: * Sun Public License Notice: *
07: * *
08: * The contents of this file are subject to the Sun Public License Version *
09: * 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: *****************************************************************************/package org.gjt.sp.jedit.bsh;
33:
34: /**
35: This class handles both while(){} statements and do{}while() statements.
36: */
37: class BSHWhileStatement extends SimpleNode implements ParserConstants {
38: public boolean isDoStatement;
39:
40: BSHWhileStatement(int id) {
41: super (id);
42: }
43:
44: public Object eval(CallStack callstack, Interpreter interpreter)
45: throws EvalError {
46: int numChild = jjtGetNumChildren();
47:
48: // Order of body and condition is swapped for do / while
49: SimpleNode condExp, body = null;
50:
51: if (isDoStatement) {
52: condExp = (SimpleNode) jjtGetChild(1);
53: body = (SimpleNode) jjtGetChild(0);
54: } else {
55: condExp = (SimpleNode) jjtGetChild(0);
56: if (numChild > 1) // has body, else just for side effects
57: body = (SimpleNode) jjtGetChild(1);
58: }
59:
60: boolean doOnceFlag = isDoStatement;
61: while (doOnceFlag
62: || BSHIfStatement.evaluateCondition(condExp, callstack,
63: interpreter)) {
64: if (body == null) // no body?
65: continue;
66:
67: Object ret = body.eval(callstack, interpreter);
68:
69: boolean breakout = false;
70: if (ret instanceof ReturnControl) {
71: switch (((ReturnControl) ret).kind) {
72: case RETURN:
73: return ret;
74:
75: case CONTINUE:
76: continue;
77:
78: case BREAK:
79: breakout = true;
80: break;
81: }
82: }
83: if (breakout)
84: break;
85:
86: doOnceFlag = false;
87: }
88:
89: return Primitive.VOID;
90: }
91:
92: }
|