checking in changes
[IRC.git] / Robust / src / IR / SymbolTable.java
1 package IR;
2
3 import java.util.*;
4
5 public class SymbolTable {
6
7     private Hashtable table;
8     private SymbolTable parent;
9   
10     public SymbolTable() {
11         table = new Hashtable();
12         this.parent = null;
13     }
14
15     public SymbolTable(SymbolTable parent) {
16         table = new Hashtable();
17         this.parent = parent;
18     }
19
20     public void add(Descriptor d) {
21         add(d.getSymbol(), d);
22     }
23     
24     public void add(String name, Descriptor d) {
25         table.put(name, d);
26     }
27
28     public void dump() {
29         Enumeration e = getDescriptors();
30         while (e.hasMoreElements()) {
31             Descriptor d = (Descriptor) e.nextElement();
32             System.out.println(d.getSymbol());
33         }
34         if (parent != null) {
35             System.out.println("parent:");
36             parent.dump();
37         }
38     }
39     
40     public Descriptor get(String name) {
41         Descriptor d = (Descriptor) table.get(name);
42         if (d == null && parent != null) {
43             return parent.get(name);
44         } else {
45             return d;
46         }
47     }
48
49     public Descriptor getFromSameScope(String name) {
50         return (Descriptor)table.get(name);
51     }
52     
53     public Enumeration getNames() {
54         return table.keys();
55     }
56
57     public Iterator getNamesIterator() {
58         return table.keySet().iterator();
59     }
60
61     public Enumeration getDescriptors() {
62         return table.elements();
63     }
64
65     public Iterator getDescriptorsIterator() {
66         return table.values().iterator();
67     }
68
69     public Iterator descriptors() {
70         return table.values().iterator();
71     }
72
73     public Vector getAllDescriptors() {
74         Vector d;
75         if (parent == null) {
76             d = new Vector();
77         } else {
78             d = parent.getAllDescriptors();
79         }
80
81         Enumeration e = getDescriptors();
82         while(e.hasMoreElements()) {
83             d.addElement(e.nextElement());
84         }
85
86         return d;
87     }
88
89     public boolean contains(String name) {
90         return (get(name) != null);
91     }
92             
93         
94     public int size() {
95         return table.size();
96     }
97
98     public int sizeAll() {
99         if (parent != null) {
100             return parent.sizeAll() + table.size();
101         } else {
102             return table.size();
103         }
104     }
105
106     public SymbolTable getParent() {
107         return parent;
108     }
109     
110     public void setParent(SymbolTable parent) {
111         this.parent = parent;
112     }
113
114     /**
115      * Adds contents of st2.table to this.table and returns a
116      * Vector of shared names, unless there are no shared names,
117      * in which case returns null.
118      */
119     public Vector merge(SymbolTable st2) {
120         Vector v = new Vector();
121         Enumeration names = st2.table.keys();
122
123         while (names.hasMoreElements()) {
124             Object o = names.nextElement();
125
126             if (table.containsKey(o)) {
127                 v.addElement(o);
128             } else {
129                 table.put(o, st2.table.get(o));
130             }
131         }
132
133         if (v.size() == 0) {
134             return null;
135         } else {
136             return v;
137         }
138     }
139
140     public String toString() {
141         return "ST: " + table.toString();               
142     }
143 }