001: /*=============================================================================
002: * Copyright Texas Instruments 2000. All Rights Reserved.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * $ProjectHeader: OSCRIPT 0.155 Fri, 20 Dec 2002 18:34:22 -0800 rclark $
019: */
020:
021: package oscript.interpreter;
022:
023: import oscript.util.*;
024: import oscript.fs.AbstractFile;
025: import oscript.fs.AbstractFileSystem;
026: import oscript.data.*;
027: import oscript.exceptions.*;
028: import oscript.syntaxtree.*;
029:
030: /**
031: *
032: * @author Rob Clark (rob@ti.com)
033: * <!--$Format: " * @version $Revision$"$-->
034: * @version 1.10
035: */
036: public class InterpretedNodeEvaluator extends oscript.NodeEvaluator {
037: private NodeToken lastNodeToken;
038: private Node node;
039: private int id;
040:
041: /**
042: * Class constructor.
043: *
044: * @param name name of the node, for debugging
045: * @param node the wrapped node
046: */
047: InterpretedNodeEvaluator(String name, Node node) {
048: this .id = name.endsWith(".os") ? -1 : Symbol.getSymbol(name)
049: .getId();
050: this .node = node;
051: }
052:
053: /**
054: * Get the file that this node was parsed from.
055: *
056: * @return the file
057: */
058: public AbstractFile getFile() {
059: try {
060: if (lastNodeToken != null)
061: return AbstractFileSystem.resolve(lastNodeToken.desc);
062: } catch (java.io.IOException e) {
063: }
064: return null;
065: }
066:
067: /**
068: * Get the function symbol (name), if this node evaluator is a function,
069: * otherwise return <code>-1</code>.
070: *
071: * @return the symbol, or <code>-1</code>
072: */
073: public int getId() {
074: return id;
075: }
076:
077: /**
078: * Evaluate, in the specified scope. If this is a function, the Arguments
079: * to the function, etc., are defined in the <code>scope</code> that the
080: * function is evaluated in.
081: *
082: * @param sf the stack frame to evaluate the node in
083: * @param scope the scope to evaluate the function in
084: * @return the result of evaluating the function
085: */
086: public Object evalNode(StackFrame sf, Scope scope)
087: throws PackagedScriptObjectException {
088: // construct evaluator:
089: EvaluateVisitor evaluator = new EvaluateVisitor(scope);
090:
091: // XXX hack... clean me up! prolly need to change the grammar so there
092: // is a function body: FunctionBody ::== Program
093: // so that we can better handle our functionly duties inside the
094: // visitor
095: Object result;
096:
097: try {
098: if (node instanceof Program)
099: result = evaluator.evaluateFunction((Program) node);
100: else
101: result = node.accept(evaluator, null);
102:
103: if (result instanceof Value)
104: result = ((Value) result).unhand();
105:
106: if (result == null)
107: result = Value.NULL;
108:
109: return result;
110: } finally {
111: lastNodeToken = evaluator.getLastNodeToken();
112: }
113: }
114:
115: /**
116: * Get the SMIT for the scope(s) created when invoking this node evaluator.
117: *
118: * @param perm <code>PRIVATE</code>, <code>PUBPROT</code>,
119: * <code>ALL</code>
120: */
121: public SymbolTable getSharedMemberIndexTable(int perm) {
122: if (smit[perm] == null)
123: smit[perm] = new OpenHashSymbolTable(3, 0.67f);
124: return smit[perm];
125: }
126:
127: private transient SymbolTable[] smit = new SymbolTable[3];
128: }
129:
130: /*
131: * Local Variables:
132: * tab-width: 2
133: * indent-tabs-mode: nil
134: * mode: java
135: * c-indentation-style: java
136: * c-basic-offset: 2
137: * eval: (c-set-offset 'substatement-open '0)
138: * eval: (c-set-offset 'case-label '+)
139: * eval: (c-set-offset 'inclass '+)
140: * eval: (c-set-offset 'inline-open '0)
141: * End:
142: */
|