01: /*
02: * @(#)StackFrame.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution of
07: * this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package pnuts.lang;
10:
11: class StackFrame {
12: StackFrame parent;
13:
14: SymbolTable symbolTable;
15:
16: StackFrame() {
17: this .symbolTable = new SymbolTable();
18: }
19:
20: StackFrame(String locals[], StackFrame parent) {
21: this .parent = parent;
22: this .symbolTable = new SymbolTable();
23: openLocal(locals);
24: }
25:
26: public void openLocal(String locals[]) {
27: symbolTable = new SymbolTable(symbolTable);
28:
29: for (int i = 0; i < locals.length; i++) {
30: symbolTable.set(locals[i], null);
31: }
32: }
33:
34: public void closeLocal() {
35: symbolTable = symbolTable.parent;
36: }
37:
38: public NamedValue lookup(String sym) {
39: return symbolTable.lookup(sym);
40: }
41:
42: public void assign(String sym, Object value) {
43: symbolTable.assign(sym, value);
44: }
45:
46: public void declare(String sym, Object value) {
47: SymbolTable t = symbolTable;
48: while (t.parent != null) {
49: t = t.parent;
50: }
51: t.set(sym, value);
52: }
53:
54: public final void bind(String sym, Object value) {
55: symbolTable.set(sym, value);
56: }
57:
58: SymbolTable makeLexicalScope() {
59: SymbolTable tab = new SymbolTable();
60: makeLexicalScope(tab);
61: return tab;
62: }
63:
64: private void makeLexicalScope(SymbolTable out) {
65: makeLexicalScope(symbolTable, out);
66: }
67:
68: private void makeLexicalScope(SymbolTable env, SymbolTable out) {
69: if (env.parent != null) {
70: makeLexicalScope(env.parent, out);
71: }
72: for (int i = 0, j = 0; i < env.count; j++) {
73: Binding b = env.table[j];
74: while (b != null) {
75: out.assign(b.name, b);
76: i++;
77: b = b.chain;
78: }
79: }
80: }
81:
82: /*
83: * void dump(){ symbolTable.dump(); if (parent != null){ parent.dump(); } }
84: */
85: }
|