Fixed lots of bugs with increment operations and +=/etc...
[IRC.git] / Robust / src / IR / SymbolTable.java
index 7203183e1a0c27f32b58bcfea3a19cb22a5bf19c..2c8bd2c5cd4119d6c374ec1f678a71f943159f45 100644 (file)
@@ -6,9 +6,11 @@ public class SymbolTable {
 
     private Hashtable table;
     private SymbolTable parent;
+    private HashSet valueset;
   
     public SymbolTable() {
        table = new Hashtable();
+       valueset = new HashSet();
        this.parent = null;
     }
 
@@ -17,33 +19,48 @@ public class SymbolTable {
        this.parent = parent;
     }
 
-    //public void add(String name, Descriptor d) {
-       //table.put(name, d);
-    //}
-
     public void add(Descriptor d) {
-       table.put(d.getSymbol(), d);
+       add(d.getSymbol(), d);
     }
     
     public void add(String name, Descriptor d) {
-       table.put(name, d);
-       
+       if (!table.containsKey(name))
+           table.put(name, new HashSet());
+       HashSet hs=(HashSet)table.get(name);
+       hs.add(d);
+       valueset.add(d);
     }
 
-    public void dump() {
-       Enumeration e = getDescriptors();
-       while (e.hasMoreElements()) {
-           Descriptor d = (Descriptor) e.nextElement();
-           System.out.println(d.getSymbol());
-       }
-       if (parent != null) {
-           System.out.println("parent:");
-           parent.dump();
+    public Set getSet(String name) {
+       return getPSet(name);
+    }
+
+    private HashSet getPSet(String name) {
+       HashSet hs=null;
+       if (parent!=null)
+           hs=parent.getPSet(name);
+       else
+           hs=new HashSet();
+       if (table.containsKey(name)) {
+           hs.addAll((HashSet)table.get(name));
        }
+       return hs;
     }
-    
+
+    public Set getSetFromSameScope(String name) {
+       return getPSetFromSameScope(name);
+    }
+
+    private HashSet getPSetFromSameScope(String name) {
+       if (table.containsKey(name)) {
+           HashSet hs=(HashSet)table.get(name);
+           return hs;
+       } else
+           return new HashSet();
+    }
+
     public Descriptor get(String name) {
-       Descriptor d = (Descriptor) table.get(name);
+       Descriptor d = getFromSameScope(name);
        if (d == null && parent != null) {
            return parent.get(name);
        } else {
@@ -52,54 +69,48 @@ public class SymbolTable {
     }
 
     public Descriptor getFromSameScope(String name) {
-       return (Descriptor)table.get(name);
+       if (table.containsKey(name)) {
+           HashSet hs=(HashSet) table.get(name);
+           return (Descriptor) hs.iterator().next();
+       } else
+           return null;
+
     }
     
     public Enumeration getNames() {
        return table.keys();
     }
 
-    public Enumeration getDescriptors() {
-       return table.elements();
+    public Iterator getNamesIterator() {
+       return table.keySet().iterator();
     }
 
-    public Iterator descriptors() {
-        return table.values().iterator();
+    public Set getValueSet() {
+       return valueset;
     }
 
-    public Vector getAllDescriptors() {
-       Vector d;
-       if (parent == null) {
-           d = new Vector();
-       } else {
-           d = parent.getAllDescriptors();
-       }
+    public Iterator getDescriptorsIterator() {
+       return getValueSet().iterator();
+    }
 
-       Enumeration e = getDescriptors();
-       while(e.hasMoreElements()) {
-           d.addElement(e.nextElement());
-       }
+    public Set getAllValueSet() {
+       HashSet hs=null;
+       if (parent!=null)
+           hs=(HashSet) parent.getAllValueSet();
+       else
+           hs=new HashSet();
+       hs.addAll(valueset);
+       return hs;
+    }
 
-       return d;
+    public Iterator getAllDescriptorsIterator() {
+       return getAllValueSet().iterator();
     }
 
     public boolean contains(String name) {
         return (get(name) != null);
     }
            
-       
-    public int size() {
-       return table.size();
-    }
-
-    public int sizeAll() {
-       if (parent != null) {
-           return parent.sizeAll() + table.size();
-       } else {
-           return table.size();
-       }
-    }
-
     public SymbolTable getParent() {
        return parent;
     }
@@ -108,32 +119,6 @@ public class SymbolTable {
        this.parent = parent;
     }
 
-    /**
-     * Adds contents of st2.table to this.table and returns a
-     * Vector of shared names, unless there are no shared names,
-     * in which case returns null.
-     */
-    public Vector merge(SymbolTable st2) {
-        Vector v = new Vector();
-        Enumeration names = st2.table.keys();
-
-        while (names.hasMoreElements()) {
-            Object o = names.nextElement();
-
-            if (table.containsKey(o)) {
-                v.addElement(o);
-            } else {
-                table.put(o, st2.table.get(o));
-            }
-        }
-
-        if (v.size() == 0) {
-            return null;
-        } else {
-            return v;
-        }
-    }
-
     public String toString() {
         return "ST: " + table.toString();               
     }