PageRenderTime 185ms CodeModel.GetById 177ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/bsh/BSHBinaryExpression.java

#
Java | 215 lines | 85 code | 24 blank | 106 comment | 50 complexity | 1cd6163ce9a6cc2c07f58acccb65c87d MD5 | raw file
  1/*****************************************************************************
  2 *                                                                           *
  3 *  This file is part of the BeanShell Java Scripting distribution.          *
  4 *  Documentation and updates may be found at http://www.beanshell.org/      *
  5 *                                                                           *
  6 *  Sun Public License Notice:                                               *
  7 *                                                                           *
  8 *  The contents of this file are subject to the Sun Public License Version  *
  9 *  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 *****************************************************************************/
 33
 34
 35package bsh;
 36
 37/**
 38	Implement binary expressions...
 39	Note: this is too complicated... need some cleanup and simplification.
 40	@see Primitive.binaryOperation
 41*/
 42class 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		/*
 55			Doing instanceof?  Next node is a type.
 56		*/
 57        if (kind == INSTANCEOF)
 58        {
 59			// null object ref is not instance of any type
 60			if ( lhs == Primitive.NULL )
 61				return new Primitive(false);
 62
 63		/*
 64			// primitive (number or void) cannot be tested for instanceof
 65            if (lhs instanceof Primitive)
 66				throw new EvalError("Cannot be instance of primitive type." );
 67		*/
 68			// Primitive (number or void) is not an instanceof anything
 69            if (lhs instanceof Primitive)
 70				return new Primitive(false);
 71
 72			// General case - performe the instanceof based on assignability
 73			NameSpace namespace = callstack.top();
 74            Class rhs = ((BSHType)jjtGetChild(1)).getType(namespace);
 75            boolean ret = (Reflect.isAssignableFrom(rhs, lhs.getClass()));
 76            return new Primitive(ret);
 77        }
 78
 79
 80		// The following two boolean checks were tacked on.
 81		// This could probably be smoothed out.
 82
 83		/*
 84			Look ahead and short circuit evaluation of the rhs if:
 85				we're a boolean AND and the lhs is false.
 86		*/
 87		if ( kind == BOOL_AND || kind == BOOL_ANDX ) {
 88			Object obj = lhs;
 89			if ( isPrimitiveValue(lhs) )
 90				obj = ((Primitive)lhs).getValue();
 91			if ( obj instanceof Boolean && 
 92				( ((Boolean)obj).booleanValue() == false ) )
 93				return new Primitive(false);
 94		}
 95		/*
 96			Look ahead and short circuit evaluation of the rhs if:
 97				we're a boolean AND and the lhs is false.
 98		*/
 99		if ( kind == BOOL_OR || kind == BOOL_ORX ) {
100			Object obj = lhs;
101			if ( isPrimitiveValue(lhs) )
102				obj = ((Primitive)lhs).getValue();
103			if ( obj instanceof Boolean && 
104				( ((Boolean)obj).booleanValue() == true ) )
105				return new Primitive(true);
106		}
107
108		// end stuff that was tacked on for boolean short-circuiting.
109
110		/*
111			Are both the lhs and rhs either wrappers or primitive values?
112			do binary op
113		*/
114		boolean isLhsWrapper = isWrapper( lhs );
115        Object rhs = ((SimpleNode)jjtGetChild(1)).eval(callstack, interpreter);
116		boolean isRhsWrapper = isWrapper( rhs );
117		if ( 
118			( isLhsWrapper || isPrimitiveValue( lhs ) )
119			&& ( isRhsWrapper || isPrimitiveValue( rhs ) )
120		)
121        {
122			// Special case for EQ on two wrapper objects
123			if ( (isLhsWrapper && isRhsWrapper && kind == EQ)) 
124			{
125				/*  
126					Don't auto-unwrap wrappers (preserve identity semantics)
127					FALL THROUGH TO OBJECT OPERATIONS BELOW.
128				*/
129			} else
130				try {
131					return Primitive.binaryOperation(lhs, rhs, kind);
132				} catch ( TargetError e ) {
133					// this doesn't really help...  need to catch it higher?
134					e.reThrow( this );
135				}
136        }
137
138		/*
139			Do we have a mixture of primitive values and non-primitives ?  
140			(primitiveValue = not null, not void)
141			god, this is getting ugly...
142		*/
143	/*
144	Removing this restriction for now...
145
146		int primCount = 0;
147		if ( isPrimitiveValue( lhs ) )
148			++primCount;
149		if ( isPrimitiveValue( rhs ) )
150			++primCount;
151
152		if ( primCount > 1 )
153			// both primitive types, should have been handled above
154			throw new InterpreterError("should not be here");
155		else if ( primCount == 1 )
156			// mixture of one and the other
157			throw new EvalError( "Invalid use of primitive and non-primitive"
158				+" values in binary operation.");
159		// else fall through to handle both non-primitive types
160
161		// end check for primitive and non-primitive mix 
162	*/
163
164
165		/*
166			Treat lhs and rhs as arbitrary objects and do the operation.
167			(including NULL and VOID represented by their Primitive types)
168		*/
169		//System.out.println("binary op arbitrary obj: {"+lhs+"}, {"+rhs+"}");
170        switch(kind)
171        {
172            case EQ:
173                return new Primitive((lhs == rhs));
174
175            case NE:
176                return new Primitive((lhs != rhs));
177
178            case PLUS:
179                if(lhs instanceof String || rhs instanceof String)
180                    return lhs.toString() + rhs.toString();
181
182            // FALL THROUGH TO DEFAULT CASE!!!
183
184            default:
185                if(lhs instanceof Primitive || rhs instanceof Primitive)
186                    if(lhs == Primitive.VOID || rhs == Primitive.VOID)
187                        throw new EvalError(
188		"illegal use of undefined variable, class, or 'void' literal", this);
189                    else 
190					if(lhs == Primitive.NULL || rhs == Primitive.NULL)
191                        throw new EvalError(
192				"illegal use of null value or 'null' literal", this);
193
194                throw new EvalError("Operator: '" + tokenImage[kind] +
195                    "' inappropriate for objects", this);
196        }
197    }
198
199
200	/*
201		object is a non-null and non-void Primitive type
202	*/
203	private boolean isPrimitiveValue( Object obj ) {
204        return ( (obj instanceof Primitive) 
205			&& (obj != Primitive.VOID) && (obj != Primitive.NULL) );
206	}
207
208	/*
209		object is a java.lang wrapper for boolean, char, or number type
210	*/
211	private boolean isWrapper( Object obj ) {
212        return ( obj instanceof Boolean || 
213			obj instanceof Character || obj instanceof Number );
214	}
215}