1 33 34 35 package bsh; 36 37 class BSHIfStatement extends SimpleNode 38 { 39 BSHIfStatement(int id) { super(id); } 40 41 public Object eval(CallStack callstack, Interpreter interpreter) 42 throws EvalError 43 { 44 Object ret = null; 45 46 if( evaluateCondition( 47 (SimpleNode)jjtGetChild(0), callstack, interpreter ) ) 48 ret = ((SimpleNode)jjtGetChild(1)).eval(callstack, interpreter); 49 else 50 if(jjtGetNumChildren() > 2) 51 ret = ((SimpleNode)jjtGetChild(2)).eval(callstack, interpreter); 52 53 if(ret instanceof ReturnControl) 54 return ret; 55 else 56 return Primitive.VOID; 57 } 58 59 public static boolean evaluateCondition( 60 SimpleNode condExp, CallStack callstack, Interpreter interpreter) 61 throws EvalError 62 { 63 Object obj = condExp.eval(callstack, interpreter); 64 if(obj instanceof Primitive) { 65 if ( obj == Primitive.VOID ) 66 throw new EvalError("Condition evaluates to void type", 67 condExp, callstack ); 68 obj = ((Primitive)obj).getValue(); 69 } 70 71 if(obj instanceof Boolean ) 72 return ((Boolean )obj).booleanValue(); 73 else 74 throw new EvalError( 75 "Condition must evaluate to a Boolean or boolean.", 76 condExp, callstack ); 77 } 78 } 79 | Popular Tags |