KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bsh > BSHPrimaryExpression


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
35 package bsh;
36
37 class BSHPrimaryExpression extends SimpleNode
38 {
39     BSHPrimaryExpression(int id) { super(id); }
40
41     /**
42         Evaluate to a value object.
43     */

44     public Object JavaDoc eval( CallStack callstack, Interpreter interpreter)
45         throws EvalError
46     {
47         return eval( false, callstack, interpreter );
48     }
49
50     /**
51         Evaluate to a value object.
52     */

53     public LHS toLHS( CallStack callstack, Interpreter interpreter)
54         throws EvalError
55     {
56         Object JavaDoc obj = eval( true, callstack, interpreter );
57
58         if ( ! (obj instanceof LHS) )
59             throw new EvalError("Can't assign to:", this, callstack );
60         else
61             return (LHS)obj;
62     }
63
64     /*
65         Our children are a prefix expression and any number of suffixes.
66         <p>
67
68         We don't eval() any nodes until the suffixes have had an
69         opportunity to work through them. This lets the suffixes decide
70         how to interpret an ambiguous name (e.g. for the .class operation).
71     */

72     private Object JavaDoc eval( boolean toLHS,
73         CallStack callstack, Interpreter interpreter)
74         throws EvalError
75     {
76         Object JavaDoc obj = jjtGetChild(0);
77         int numChildren = jjtGetNumChildren();
78
79         for(int i=1; i<numChildren; i++)
80             obj = ((BSHPrimarySuffix)jjtGetChild(i)).doSuffix(
81                 obj, toLHS, callstack, interpreter);
82
83         /*
84             If the result is a Node eval() it to an object or LHS
85             (as determined by toLHS)
86         */

87         if ( obj instanceof SimpleNode )
88             if ( obj instanceof BSHAmbiguousName )
89                 if ( toLHS )
90                     obj = ((BSHAmbiguousName)obj).toLHS(
91                         callstack, interpreter);
92                 else
93                     obj = ((BSHAmbiguousName)obj).toObject(
94                         callstack, interpreter);
95             else
96                 // Some arbitrary kind of node
97
if ( toLHS )
98                     // is this right?
99
throw new EvalError("Can't assign to prefix.",
100                         this, callstack );
101                 else
102                     obj = ((SimpleNode)obj).eval(callstack, interpreter);
103
104         // return LHS or value object as determined by toLHS
105
if ( obj instanceof LHS )
106             if ( toLHS )
107                 return obj;
108             else
109                 try {
110                     return ((LHS)obj).getValue();
111                 } catch ( UtilEvalError e ) {
112                     throw e.toEvalError( this, callstack );
113                 }
114         else
115             return obj;
116     }
117 }
118
119
Popular Tags