KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > expr > NameLookup


1 // Copyright (c) 2003, 2006 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.expr;
5 import java.util.*;
6 import gnu.kawa.util.GeneralHashTable;
7 import gnu.kawa.util.HashNode;
8
9 /** Manages the set of declarations "currently" in scope. */
10
11 public class NameLookup extends GeneralHashTable
12 {
13   Language language;
14
15   public NameLookup (Language language)
16   {
17     this.language = language;
18   }
19
20   public void push (Declaration decl)
21   {
22     Object JavaDoc symbol = decl.getSymbol();
23     if (symbol == null)
24       return;
25     if (++num_bindings >= table.length)
26       rehash();
27     int hash = hash(symbol);
28     HashNode node = makeEntry(symbol, hash, decl);
29     int index = hash & mask;
30     node.next = table[index];
31     table[index] = node;
32   }
33
34   public boolean pop (Declaration decl)
35   {
36     Object JavaDoc symbol = decl.getSymbol();
37     if (symbol == null)
38       return false;
39     int hash = hash(symbol);
40     HashNode prev = null;
41     int index = hash & this.mask;
42     HashNode node = table[index];
43     while (node != null)
44       {
45     HashNode next = node.next;
46         if (node.getValue() == decl)
47       {
48         if (prev == null)
49           table[index] = next;
50         else
51           prev.next = next;
52         num_bindings--;
53         return true;
54       }
55     prev = node;
56     node = next;
57       }
58     return false;
59   }
60
61   public void push (ScopeExp exp)
62   {
63     for (Declaration decl = exp.firstDecl();
64          decl != null; decl = decl.nextDecl())
65       push(decl);
66   }
67
68   public void pop (ScopeExp exp)
69   {
70     for (Declaration decl = exp.firstDecl();
71          decl != null; decl = decl.nextDecl())
72       pop(decl);
73   }
74
75   public Declaration lookup (Object JavaDoc symbol, int namespace)
76   {
77     int hash = hash(symbol);
78     int index = hash & this.mask;
79     for (HashNode node = table[index];
80      node != null; node = node.next)
81       {
82         Declaration decl = (Declaration) node.getValue();
83     if (symbol.equals(decl.getSymbol())
84         && language.hasNamespace(decl, namespace))
85       return decl;
86       }
87     return null;
88   }
89
90   public Declaration lookup (Object JavaDoc symbol, boolean function)
91   {
92     return lookup(symbol, (function ? Language.FUNCTION_NAMESPACE
93                : Language.VALUE_NAMESPACE));
94   }
95 }
96
Popular Tags