KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bsh > BSHVariableDeclarator


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 /**
38     name [ = initializer ]
39     evaluate name and return optional initializer
40 */

41 class BSHVariableDeclarator extends SimpleNode
42 {
43     // The token.image text of the name... never changes.
44
public String JavaDoc name;
45
46     BSHVariableDeclarator(int id) { super(id); }
47
48     /**
49         Evaluate the optional initializer value.
50         (The name was set at parse time.)
51
52         A variable declarator can be evaluated with or without preceding
53         type information. Currently the type info is only used by array
54         initializers in the case where there is no explicitly declared type.
55
56         @param typeNode is the BSHType node. Its info is passed through to any
57         variable intializer children for the case where the array initializer
58         does not declare the type explicitly. e.g.
59             int [] a = { 1, 2 };
60         typeNode may be null to indicate no type information available.
61     */

62     public Object JavaDoc eval(
63         BSHType typeNode, CallStack callstack, Interpreter interpreter)
64         throws EvalError
65     {
66         // null value means no value
67
Object JavaDoc value = null;
68
69         if ( jjtGetNumChildren() > 0 )
70         {
71             SimpleNode initializer = (SimpleNode)jjtGetChild(0);
72
73             /*
74                 If we have type info and the child is an array initializer
75                 pass it along... Else use the default eval style.
76                 (This allows array initializer to handle the problem...
77                 allowing for future enhancements in loosening types there).
78             */

79             if ( (typeNode != null)
80                 && initializer instanceof BSHArrayInitializer
81             )
82                 value = ((BSHArrayInitializer)initializer).eval(
83                     typeNode.getBaseType(), typeNode.getArrayDims(),
84                     callstack, interpreter);
85             else
86                 value = initializer.eval( callstack, interpreter);
87         }
88
89         if ( value == Primitive.VOID )
90             throw new EvalError("Void initializer.", this, callstack );
91
92         return value;
93     }
94
95     public String JavaDoc toString() { return "BSHVariableDeclarator "+name; }
96 }
97
Popular Tags