001: /*****************************************************************************
002: * *
003: * This file is part of the BeanShell Java Scripting distribution. *
004: * Documentation and updates may be found at http://www.beanshell.org/ *
005: * *
006: * Sun Public License Notice: *
007: * *
008: * The contents of this file are subject to the Sun Public License Version *
009: * 1.0 (the "License"); you may not use this file except in compliance with *
010: * the License. A copy of the License is available at http://www.sun.com *
011: * *
012: * The Original Code is BeanShell. The Initial Developer of the Original *
013: * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
014: * (C) 2000. All Rights Reserved. *
015: * *
016: * GNU Public License Notice: *
017: * *
018: * Alternatively, the contents of this file may be used under the terms of *
019: * the GNU Lesser General Public License (the "LGPL"), in which case the *
020: * provisions of LGPL are applicable instead of those above. If you wish to *
021: * allow use of your version of this file only under the terms of the LGPL *
022: * and not to allow others to use your version of this file under the SPL, *
023: * indicate your decision by deleting the provisions above and replace *
024: * them with the notice and other provisions required by the LGPL. If you *
025: * do not delete the provisions above, a recipient may use your version of *
026: * this file under either the SPL or the LGPL. *
027: * *
028: * Patrick Niemeyer (pat@pat.net) *
029: * Author of Learning Java, O'Reilly & Associates *
030: * http://www.pat.net/~pat/ *
031: * *
032: *****************************************************************************/package bsh;
033:
034: class BSHPrimaryExpression extends SimpleNode {
035: BSHPrimaryExpression(int id) {
036: super (id);
037: }
038:
039: /**
040: Evaluate to a value object.
041: */
042: public Object eval(CallStack callstack, Interpreter interpreter)
043: throws EvalError {
044: return eval(false, callstack, interpreter);
045: }
046:
047: /**
048: Evaluate to a value object.
049: */
050: public LHS toLHS(CallStack callstack, Interpreter interpreter)
051: throws EvalError {
052: Object obj = eval(true, callstack, interpreter);
053:
054: if (!(obj instanceof LHS))
055: throw new EvalError("Can't assign to:", this , callstack);
056: else
057: return (LHS) obj;
058: }
059:
060: /*
061: Our children are a prefix expression and any number of suffixes.
062: <p>
063:
064: We don't eval() any nodes until the suffixes have had an
065: opportunity to work through them. This lets the suffixes decide
066: how to interpret an ambiguous name (e.g. for the .class operation).
067: */
068: private Object eval(boolean toLHS, CallStack callstack,
069: Interpreter interpreter) throws EvalError {
070: Object obj = jjtGetChild(0);
071: int numChildren = jjtGetNumChildren();
072:
073: for (int i = 1; i < numChildren; i++)
074: obj = ((BSHPrimarySuffix) jjtGetChild(i)).doSuffix(obj,
075: toLHS, callstack, interpreter);
076:
077: /*
078: If the result is a Node eval() it to an object or LHS
079: (as determined by toLHS)
080: */
081: if (obj instanceof SimpleNode)
082: if (obj instanceof BSHAmbiguousName)
083: if (toLHS)
084: obj = ((BSHAmbiguousName) obj).toLHS(callstack,
085: interpreter);
086: else
087: obj = ((BSHAmbiguousName) obj).toObject(callstack,
088: interpreter);
089: else
090: // Some arbitrary kind of node
091: if (toLHS)
092: // is this right?
093: throw new EvalError("Can't assign to prefix.", this ,
094: callstack);
095: else
096: obj = ((SimpleNode) obj).eval(callstack, interpreter);
097:
098: // return LHS or value object as determined by toLHS
099: if (obj instanceof LHS)
100: if (toLHS)
101: return obj;
102: else
103: try {
104: return ((LHS) obj).getValue();
105: } catch (UtilEvalError e) {
106: throw e.toEvalError(this, callstack);
107: }
108: else
109: return obj;
110: }
111: }
|