1 33 34 package bsh; 35 36 class BSHSwitchStatement 37 extends SimpleNode 38 implements ParserConstants 39 { 40 41 public BSHSwitchStatement(int id) { super(id); } 42 43 public Object eval( CallStack callstack, Interpreter interpreter ) 44 throws EvalError 45 { 46 int numchild = jjtGetNumChildren(); 47 int child = 0; 48 SimpleNode switchExp = ((SimpleNode)jjtGetChild(child++)); 49 Object switchVal = switchExp.eval( callstack, interpreter ); 50 51 55 BSHSwitchLabel label; 57 Object node; 58 ReturnControl returnControl=null; 59 60 if ( child >= numchild ) 62 throw new EvalError("Empty switch statement.", this, callstack ); 63 label = ((BSHSwitchLabel)jjtGetChild(child++)); 64 65 while ( child < numchild && returnControl == null ) 67 { 68 if ( label.isDefault 70 || primitiveEquals( 71 switchVal, label.eval( callstack, interpreter ), 72 callstack, switchExp ) 73 ) 74 { 75 while ( child < numchild ) 77 { 78 node = jjtGetChild(child++); 79 if ( node instanceof BSHSwitchLabel ) 80 continue; 81 Object value = 83 ((SimpleNode)node).eval( callstack, interpreter ); 84 85 if ( value instanceof ReturnControl ) { 87 returnControl = (ReturnControl)value; 88 break; 89 } 90 } 91 } else 92 { 93 while ( child < numchild ) 95 { 96 node = jjtGetChild(child++); 97 if ( node instanceof BSHSwitchLabel ) { 98 label = (BSHSwitchLabel)node; 99 break; 100 } 101 } 102 } 103 } 104 105 if ( returnControl != null && returnControl.kind == RETURN ) 106 return returnControl; 107 else 108 return Primitive.VOID; 109 } 110 111 115 private boolean primitiveEquals( 116 Object switchVal, Object targetVal, 117 CallStack callstack, SimpleNode switchExp ) 118 throws EvalError 119 { 120 if ( switchVal instanceof Primitive || targetVal instanceof Primitive ) 121 try { 122 Object result = Primitive.binaryOperation( 124 switchVal, targetVal, ParserConstants.EQ ); 125 result = Primitive.unwrap( result ); 126 return result.equals( Boolean.TRUE ); 127 } catch ( UtilEvalError e ) { 128 throw e.toEvalError( 129 "Switch value: "+switchExp.getText()+": ", 130 this, callstack ); 131 } 132 else 133 return switchVal.equals( targetVal ); 134 } 135 } 136 137 | Popular Tags |