01: /*
02: * @(#)BranchEnv.java 1.1 05/06/21
03: *
04: * Copyright (c) 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package pnuts.compiler;
10:
11: import java.util.ArrayList;
12: import java.util.HashMap;
13: import java.util.Iterator;
14: import java.util.Map;
15:
16: class BranchEnv {
17: BranchEnv parent;
18: ArrayList scopes;
19: SymbolSet scope;
20: Map symbolToLocalInfo;
21:
22: BranchEnv(BranchEnv parent, SymbolSet ss) {
23: this .parent = parent;
24: this .scopes = new ArrayList();
25: this .symbolToLocalInfo = new HashMap();
26: addBranch(ss);
27: }
28:
29: void addBranch(SymbolSet ss) {
30: this .scope = new SymbolSet(ss);
31: scopes.add(scope);
32: }
33:
34: void close() {
35: if (parent != null) {
36: parent.symbolToLocalInfo.putAll(symbolToLocalInfo);
37: for (Iterator it = symbolToLocalInfo.entrySet().iterator(); it
38: .hasNext();) {
39: Map.Entry entry = (Map.Entry) it.next();
40: String sym = (String) entry.getKey();
41: LocalInfo info = (LocalInfo) entry.getValue();
42: parent.scope.add(sym, info);
43: }
44: }
45: }
46:
47: LocalInfo lookup(String key) {
48: LocalInfo info = (LocalInfo) symbolToLocalInfo.get(key);
49: if (info != null) {
50: return info;
51: } else if (parent != null) {
52: return parent.lookup(key);
53: } else {
54: return null;
55: }
56: }
57:
58: LocalInfo assoc(String key) {
59: LocalInfo info = scope.assoc(key);
60: if (info != null) {
61: return info;
62: } else if (parent != null) {
63: return parent.assoc(key);
64: } else {
65: return null;
66: }
67: }
68:
69: void declare(String symbol, int key, int idx) {
70: // if (!symbolToLocalInfo.containsKey(symbol)){
71: scope.add(symbol, key, idx);
72: LocalInfo info = scope.info[scope.count - 1];
73: symbolToLocalInfo.put(symbol, info);
74: // }
75: }
76:
77: void declare(String symbol, int key) {
78: // if (!symbolToLocalInfo.containsKey(symbol)){
79: scope.add(symbol, key);
80: LocalInfo info = scope.info[scope.count - 1];
81: symbolToLocalInfo.put(symbol, info);
82: // }
83: }
84: }
|