1 33 34 35 package bsh; 36 37 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 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()) statement = ((SimpleNode)jjtGetChild(i)); 67 68 NameSpace enclosingNameSpace= callstack.top(); 69 BlockNameSpace forNameSpace = new BlockNameSpace( enclosingNameSpace ); 70 71 85 86 callstack.swap( forNameSpace ); 90 91 if ( hasForInit ) 93 forInit.eval( callstack, interpreter ); 94 95 Object 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; if ( statement != null ) { 110 Object 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 ); return returnControl; 141 } 142 143 } 144 | Popular Tags |