01: /*****************************************************************************
02: * *
03: * This file is part of the BeanShell Java Scripting distribution. *
04: * Documentation and updates may be found at http://www.beanshell.org/ *
05: * *
06: * Sun Public License Notice: *
07: * *
08: * The contents of this file are subject to the Sun Public License Version *
09: * 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: *****************************************************************************/package bsh;
33:
34: /**
35: name [ = initializer ]
36: evaluate name and return optional initializer
37: */
38: class BSHVariableDeclarator extends SimpleNode {
39: // The token.image text of the name... never changes.
40: public String name;
41:
42: BSHVariableDeclarator(int id) {
43: super (id);
44: }
45:
46: /**
47: Evaluate the optional initializer value.
48: (The name was set at parse time.)
49:
50: A variable declarator can be evaluated with or without preceding
51: type information. Currently the type info is only used by array
52: initializers in the case where there is no explicitly declared type.
53:
54: @param typeNode is the BSHType node. Its info is passed through to any
55: variable intializer children for the case where the array initializer
56: does not declare the type explicitly. e.g.
57: int [] a = { 1, 2 };
58: typeNode may be null to indicate no type information available.
59: */
60: public Object eval(BSHType typeNode, CallStack callstack,
61: Interpreter interpreter) throws EvalError {
62: // null value means no value
63: Object value = null;
64:
65: if (jjtGetNumChildren() > 0) {
66: SimpleNode initializer = (SimpleNode) jjtGetChild(0);
67:
68: /*
69: If we have type info and the child is an array initializer
70: pass it along... Else use the default eval style.
71: (This allows array initializer to handle the problem...
72: allowing for future enhancements in loosening types there).
73: */
74: if ((typeNode != null)
75: && initializer instanceof BSHArrayInitializer)
76: value = ((BSHArrayInitializer) initializer)
77: .eval(typeNode.getBaseType(), typeNode
78: .getArrayDims(), callstack, interpreter);
79: else
80: value = initializer.eval(callstack, interpreter);
81: }
82:
83: if (value == Primitive.VOID)
84: throw new EvalError("Void initializer.", this , callstack);
85:
86: return value;
87: }
88:
89: public String toString() {
90: return "BSHVariableDeclarator " + name;
91: }
92: }
|