1 33 34 35 package bsh; 36 37 42 class BSHBinaryExpression extends SimpleNode 43 implements ParserConstants 44 { 45 public int kind; 46 47 BSHBinaryExpression(int id) { super(id); } 48 49 public Object eval( CallStack callstack, Interpreter interpreter) 50 throws EvalError 51 { 52 Object lhs = ((SimpleNode)jjtGetChild(0)).eval(callstack, interpreter); 53 54 57 if (kind == INSTANCEOF) 58 { 59 if ( lhs == Primitive.NULL ) 61 return new Primitive(false); 62 63 Class rhs = ((BSHType)jjtGetChild(1)).getType( 64 callstack, interpreter ); 65 70 76 if ( lhs instanceof Primitive ) 77 if ( rhs == bsh.Primitive.class ) 78 return new Primitive(true); 79 else 80 return new Primitive(false); 81 82 boolean ret = Types.isJavaBaseAssignable( rhs, lhs.getClass() ); 84 return new Primitive(ret); 85 } 86 87 88 91 95 if ( kind == BOOL_AND || kind == BOOL_ANDX ) { 96 Object obj = lhs; 97 if ( isPrimitiveValue(lhs) ) 98 obj = ((Primitive)lhs).getValue(); 99 if ( obj instanceof Boolean && 100 ( ((Boolean )obj).booleanValue() == false ) ) 101 return new Primitive(false); 102 } 103 107 if ( kind == BOOL_OR || kind == BOOL_ORX ) { 108 Object obj = lhs; 109 if ( isPrimitiveValue(lhs) ) 110 obj = ((Primitive)lhs).getValue(); 111 if ( obj instanceof Boolean && 112 ( ((Boolean )obj).booleanValue() == true ) ) 113 return new Primitive(true); 114 } 115 116 118 122 boolean isLhsWrapper = isWrapper( lhs ); 123 Object rhs = ((SimpleNode)jjtGetChild(1)).eval(callstack, interpreter); 124 boolean isRhsWrapper = isWrapper( rhs ); 125 if ( 126 ( isLhsWrapper || isPrimitiveValue( lhs ) ) 127 && ( isRhsWrapper || isPrimitiveValue( rhs ) ) 128 ) 129 { 130 if ( (isLhsWrapper && isRhsWrapper && kind == EQ)) 132 { 133 137 } else 138 try { 139 return Primitive.binaryOperation(lhs, rhs, kind); 140 } catch ( UtilEvalError e ) { 141 throw e.toEvalError( this, callstack ); 142 } 143 } 144 176 177 181 switch(kind) 183 { 184 case EQ: 185 return new Primitive((lhs == rhs)); 186 187 case NE: 188 return new Primitive((lhs != rhs)); 189 190 case PLUS: 191 if(lhs instanceof String || rhs instanceof String ) 192 return lhs.toString() + rhs.toString(); 193 194 196 default: 197 if(lhs instanceof Primitive || rhs instanceof Primitive) 198 if ( lhs == Primitive.VOID || rhs == Primitive.VOID ) 199 throw new EvalError( 200 "illegal use of undefined variable, class, or 'void' literal", 201 this, callstack ); 202 else 203 if ( lhs == Primitive.NULL || rhs == Primitive.NULL ) 204 throw new EvalError( 205 "illegal use of null value or 'null' literal", this, callstack); 206 207 throw new EvalError("Operator: '" + tokenImage[kind] + 208 "' inappropriate for objects", this, callstack ); 209 } 210 } 211 212 215 private boolean isPrimitiveValue( Object obj ) { 216 return ( (obj instanceof Primitive) 217 && (obj != Primitive.VOID) && (obj != Primitive.NULL) ); 218 } 219 220 223 private boolean isWrapper( Object obj ) { 224 return ( obj instanceof Boolean || 225 obj instanceof Character || obj instanceof Number ); 226 } 227 } 228 | Popular Tags |