01: /*
02: * @(#)SymbolSet.java 1.4 05/06/13
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
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package pnuts.compiler;
10:
11: /**
12: * This class represents a variable scope made by functions,
13: * for/foreach statements, and so on.
14: *
15: * @see pnuts.compiler.Frame
16: * @see pnuts.compiler.LocalInfo
17: */
18: final class SymbolSet implements Cloneable {
19: SymbolSet parent;
20: String keys[] = new String[4];
21: LocalInfo info[] = new LocalInfo[4];
22: int count = 0;
23:
24: SymbolSet() {
25: }
26:
27: SymbolSet(SymbolSet parent) {
28: this .parent = parent;
29: }
30:
31: void add(String sym, int map) {
32: add(sym, map, 0);
33: }
34:
35: void add(String sym, int map, Frame frame) {
36: add(sym, new LocalInfo(sym, map, frame));
37: }
38:
39: void add(String sym, int map, int idx) {
40: ensureCapacity(count + 1);
41: sym = sym.intern();
42: keys[count] = sym;
43: info[count] = new LocalInfo(sym, map, idx, false);
44: count++;
45: }
46:
47: void add(String sym, LocalInfo i) {
48: ensureCapacity(count + 1);
49: keys[count] = sym;
50: info[count] = i;
51: count++;
52: }
53:
54: void ensureCapacity(int size) {
55: if (size > keys.length) {
56: String newKeys[] = new String[keys.length + size];
57: System.arraycopy(keys, 0, newKeys, 0, keys.length);
58: this .keys = newKeys;
59: LocalInfo newInfo[] = new LocalInfo[info.length + size];
60: System.arraycopy(info, 0, newInfo, 0, info.length);
61: this .info = newInfo;
62: }
63: }
64:
65: LocalInfo assoc(String key) {
66: return _assoc(key.intern());
67: }
68:
69: LocalInfo _assoc(String key) {
70: for (int i = 0; i < count; i++) {
71: if (keys[i] == key) {
72: return info[i];
73: }
74: }
75: if (parent != null) {
76: return parent._assoc(key);
77: } else {
78: return null;
79: }
80: }
81:
82: public Object clone() {
83: try {
84: SymbolSet ss = (SymbolSet) super .clone();
85: ss.keys = new String[keys.length];
86: System.arraycopy(ss.keys, 0, keys, 0, keys.length);
87: ss.info = new LocalInfo[info.length];
88: System.arraycopy(ss.info, 0, info, 0, info.length);
89: return ss;
90: } catch (Throwable t) {
91: throw new InternalError();
92: }
93: }
94: }
|