PageRenderTime 50ms CodeModel.GetById 41ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/bsh/BSHAssignment.java

#
Java | 151 lines | 85 code | 27 blank | 39 comment | 27 complexity | 4d53c5b500cde50a21c99c701af80ab0 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
 37class BSHAssignment extends SimpleNode implements ParserConstants
 38{
 39    public int operator;
 40
 41    BSHAssignment(int id) { super(id); }
 42
 43    public Object eval(
 44		CallStack callstack, Interpreter interpreter) 
 45		throws EvalError
 46    {
 47        BSHLHSPrimaryExpression lhsNode = 
 48			(BSHLHSPrimaryExpression)jjtGetChild(0);
 49
 50if ( lhsNode == null )
 51	throw new InterpreterError( "Error, null LHSnode" );
 52
 53        LHS lhs = lhsNode.toLHS( callstack, interpreter);
 54        if ( lhs == null )
 55            throw new InterpreterError( "Error, null LHS" );
 56
 57        Object rhs = ((SimpleNode)jjtGetChild(1)).eval(callstack, interpreter);
 58        if ( rhs == Primitive.VOID )
 59            throw new EvalError("Void assignment.", this);
 60
 61        switch(operator)
 62        {
 63            case ASSIGN:
 64				try {
 65					//Interpreter.debug("lhs assign: "+rhs);
 66					return lhs.assign(rhs);
 67				} catch ( EvalError e ) {
 68					e.reThrow(this);
 69				}
 70
 71            case PLUSASSIGN:
 72                return lhs.assign(operation(lhs.getValue(), rhs, PLUS));
 73
 74            case MINUSASSIGN:
 75                return lhs.assign(operation(lhs.getValue(), rhs, MINUS));
 76
 77            case STARASSIGN:
 78                return lhs.assign(operation(lhs.getValue(), rhs, STAR));
 79
 80            case SLASHASSIGN:
 81                return lhs.assign(operation(lhs.getValue(), rhs, SLASH));
 82
 83            case ANDASSIGN:
 84            case ANDASSIGNX:
 85                return lhs.assign(operation(lhs.getValue(), rhs, BIT_AND));
 86
 87            case ORASSIGN:
 88            case ORASSIGNX:
 89                return lhs.assign(operation(lhs.getValue(), rhs, BIT_OR));
 90
 91            case XORASSIGN:
 92                return lhs.assign(operation(lhs.getValue(), rhs, XOR));
 93
 94            case MODASSIGN:
 95                return lhs.assign(operation(lhs.getValue(), rhs, MOD));
 96
 97            case LSHIFTASSIGN:
 98            case LSHIFTASSIGNX:
 99                return lhs.assign(operation(lhs.getValue(), rhs, LSHIFT));
100
101            case RSIGNEDSHIFTASSIGN:
102            case RSIGNEDSHIFTASSIGNX:
103                return lhs.assign(operation(lhs.getValue(), rhs, RSIGNEDSHIFT));
104
105            case RUNSIGNEDSHIFTASSIGN:
106            case RUNSIGNEDSHIFTASSIGNX:
107                return lhs.assign(operation(lhs.getValue(), rhs, RUNSIGNEDSHIFT));
108
109            default:
110                throw new InterpreterError("unimplemented operator in assignment BSH");
111        }
112    }
113
114    private Object operation(Object lhs, Object rhs, int kind) 
115		throws EvalError
116    {
117		/*
118			Implement String += value;
119			According to the JLS, value may be anything.
120			In BeanShell, we'll disallow VOID (undefined) values.
121			(or should we map them to the empty string?)
122		*/
123		if ( lhs instanceof String && rhs != Primitive.VOID ) {
124			if ( kind != PLUS )
125				throw new EvalError(
126					"Use of non + operator with String LHS", this);     
127
128			return (String)lhs + rhs;
129		}
130
131        if ( lhs instanceof Primitive || rhs instanceof Primitive )
132            if(lhs == Primitive.VOID || rhs == Primitive.VOID)
133                throw new EvalError(
134					"Illegal use of undefined object or 'void' literal", this);
135            else if ( lhs == Primitive.NULL || rhs == Primitive.NULL )
136                throw new EvalError(
137					"Illegal use of null object or 'null' literal", this);
138
139
140        if( (lhs instanceof Boolean || lhs instanceof Character ||
141             lhs instanceof Number || lhs instanceof Primitive) &&
142            (rhs instanceof Boolean || rhs instanceof Character ||
143             rhs instanceof Number || rhs instanceof Primitive) )
144        {
145            return Primitive.binaryOperation(lhs, rhs, kind);
146        }
147
148        throw new EvalError("Non primitive value in operator: " +
149            lhs.getClass() + " " + tokenImage[kind] + " " + rhs.getClass(), this);
150    }
151}