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 BSHTypedVariableDeclaration extends SimpleNode {
035: public Modifiers modifiers;
036:
037: BSHTypedVariableDeclaration(int id) {
038: super (id);
039: }
040:
041: private BSHType getTypeNode() {
042: return ((BSHType) jjtGetChild(0));
043: }
044:
045: Class evalType(CallStack callstack, Interpreter interpreter)
046: throws EvalError {
047: BSHType typeNode = getTypeNode();
048: return typeNode.getType(callstack, interpreter);
049: }
050:
051: BSHVariableDeclarator[] getDeclarators() {
052: int n = jjtGetNumChildren();
053: int start = 1;
054: BSHVariableDeclarator[] bvda = new BSHVariableDeclarator[n
055: - start];
056: for (int i = start; i < n; i++) {
057: bvda[i - start] = (BSHVariableDeclarator) jjtGetChild(i);
058: }
059: return bvda;
060: }
061:
062: /**
063: evaluate the type and one or more variable declarators, e.g.:
064: int a, b=5, c;
065: */
066: public Object eval(CallStack callstack, Interpreter interpreter)
067: throws EvalError {
068: try {
069: NameSpace namespace = callstack.top();
070: BSHType typeNode = getTypeNode();
071: Class type = typeNode.getType(callstack, interpreter);
072:
073: BSHVariableDeclarator[] bvda = getDeclarators();
074: for (int i = 0; i < bvda.length; i++) {
075: BSHVariableDeclarator dec = bvda[i];
076:
077: // Type node is passed down the chain for array initializers
078: // which need it under some circumstances
079: Object value = dec.eval(typeNode, callstack,
080: interpreter);
081:
082: try {
083: namespace.setTypedVariable(dec.name, type, value,
084: modifiers);
085: } catch (UtilEvalError e) {
086: throw e.toEvalError(this , callstack);
087: }
088: }
089: } catch (EvalError e) {
090: e.reThrow("Typed variable declaration");
091: }
092:
093: return Primitive.VOID;
094: }
095:
096: public String getTypeDescriptor(CallStack callstack,
097: Interpreter interpreter, String defaultPackage) {
098: return getTypeNode().getTypeDescriptor(callstack, interpreter,
099: defaultPackage);
100: }
101: }
|