01: // Copyright (c) 2003, 2006 Per M.A. Bothner.
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.expr;
05:
06: import java.util.*;
07: import gnu.kawa.util.GeneralHashTable;
08: import gnu.kawa.util.HashNode;
09:
10: /** Manages the set of declarations "currently" in scope. */
11:
12: public class NameLookup extends GeneralHashTable {
13: Language language;
14:
15: public NameLookup(Language language) {
16: this .language = language;
17: }
18:
19: public void push(Declaration decl) {
20: Object symbol = decl.getSymbol();
21: if (symbol == null)
22: return;
23: if (++num_bindings >= table.length)
24: rehash();
25: int hash = hash(symbol);
26: HashNode node = makeEntry(symbol, hash, decl);
27: int index = hash & mask;
28: node.next = table[index];
29: table[index] = node;
30: }
31:
32: public boolean pop(Declaration decl) {
33: Object symbol = decl.getSymbol();
34: if (symbol == null)
35: return false;
36: int hash = hash(symbol);
37: HashNode prev = null;
38: int index = hash & this .mask;
39: HashNode node = table[index];
40: while (node != null) {
41: HashNode next = node.next;
42: if (node.getValue() == decl) {
43: if (prev == null)
44: table[index] = next;
45: else
46: prev.next = next;
47: num_bindings--;
48: return true;
49: }
50: prev = node;
51: node = next;
52: }
53: return false;
54: }
55:
56: public void push(ScopeExp exp) {
57: for (Declaration decl = exp.firstDecl(); decl != null; decl = decl
58: .nextDecl())
59: push(decl);
60: }
61:
62: public void pop(ScopeExp exp) {
63: for (Declaration decl = exp.firstDecl(); decl != null; decl = decl
64: .nextDecl())
65: pop(decl);
66: }
67:
68: public Declaration lookup(Object symbol, int namespace) {
69: int hash = hash(symbol);
70: int index = hash & this .mask;
71: for (HashNode node = table[index]; node != null; node = node.next) {
72: Declaration decl = (Declaration) node.getValue();
73: if (symbol.equals(decl.getSymbol())
74: && language.hasNamespace(decl, namespace))
75: return decl;
76: }
77: return null;
78: }
79:
80: public Declaration lookup(Object symbol, boolean function) {
81: return lookup(symbol, (function ? Language.FUNCTION_NAMESPACE
82: : Language.VALUE_NAMESPACE));
83: }
84: }
|