PageRenderTime 130ms CodeModel.GetById 126ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/bsh/BSHVariableDeclarator.java

#
Java | 96 lines | 28 code | 10 blank | 58 comment | 6 complexity | 62f7639fb3f97a5a8cb5f1c8bf237bb4 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	name [ = initializer ]
39	evaluate name and return optional initializer
40*/
41class BSHVariableDeclarator extends SimpleNode
42{
43	// The token.image text of the name... never changes.
44    public String 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 eval( 
63		BSHType typeNode, CallStack callstack, Interpreter interpreter)  
64		throws EvalError
65    {
66		// null value means no value
67		Object 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 toString() { return "BSHVariableDeclarator "+name; }
96}