org.norecess.citkit.symboltable
Class HashSymbolTable<A>

java.lang.Object
  extended by org.norecess.citkit.symboltable.HashSymbolTable<A>
All Implemented Interfaces:
SymbolTable<A>

public class HashSymbolTable<A>
extends java.lang.Object
implements SymbolTable<A>

Implements a symbol table for a compiler of Hobbes and perhaps other languages

To create a new, initial symbol table, use HashSymbolTable(). To create a chained symbol table (for nested scopes), use HashSymbolTable(SymbolTable).

Author:
Jeremy D. Frens

Constructor Summary
HashSymbolTable()
          Builds a new, initial symbol table.
HashSymbolTable(SymbolTable<A> previous)
          Builds a new symbol table using the specified symbol table as the previous symbol table for the previous scope.
 
Method Summary
 void add(java.lang.String name, A value)
          Adds a name with an associated value to the current symbol table..
 void add(Symbol variable, A value)
          Adds a new variable to the current symbol table.
 boolean equals(java.lang.Object other)
           
 A get(ISymbol variable)
          Returns the value associated with the specified variable.
 A get(java.lang.String name)
          Invokes get(ISymbol).
 java.util.Set<Symbol> getSymbols()
          Returns the symbols in the current scope.
 java.lang.String getVariablesAsString()
          Returns the variables of the scope.
 int hashCode()
           
 A set(java.lang.String name, A value)
          Sets a value associated with a name..
 A set(Symbol variable, A value)
          Changes the value associated with the specified variable.
 A setInCurrentScope(Symbol variable, A value)
          Changes the value associated with the specified variable only in the current scope.
 java.lang.String toString()
          The symbol table as a string.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

HashSymbolTable

public HashSymbolTable()
Builds a new, initial symbol table.


HashSymbolTable

public HashSymbolTable(SymbolTable<A> previous)
Builds a new symbol table using the specified symbol table as the previous symbol table for the previous scope.

Parameters:
previous - the previous symbol table.
Method Detail

equals

public boolean equals(java.lang.Object other)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

add

public void add(Symbol variable,
                A value)
Description copied from interface: SymbolTable
Adds a new variable to the current symbol table. This does not check the previous symbol table (for previous variable scopes) to see if this shadows an earlier declaration; an exception is thrown when the variable is already declared in the current symbol table.

Specified by:
add in interface SymbolTable<A>
Parameters:
variable - the new variable to add to the symbol table.
value - the new value to associate with the new variable.

add

public void add(java.lang.String name,
                A value)
Description copied from interface: SymbolTable
Adds a name with an associated value to the current symbol table..

Specified by:
add in interface SymbolTable<A>
See Also:
SymbolTable.add(Symbol, Object)

get

public A get(ISymbol variable)
Returns the value associated with the specified variable. The variable is searched for in all of the previous scopes.

Specified by:
get in interface SymbolTable<A>
Parameters:
variable - the variable to search for.
Returns:
the value associated with the variable; null if the variable isn't found.

get

public A get(java.lang.String name)
Invokes get(ISymbol).

Specified by:
get in interface SymbolTable<A>
See Also:
SymbolTable.get(ISymbol)

getSymbols

public java.util.Set<Symbol> getSymbols()
Returns the symbols in the current scope.

Specified by:
getSymbols in interface SymbolTable<A>

set

public A set(Symbol variable,
             A value)
Changes the value associated with the specified variable. The variable is searched for in all of the previous scopes; the old value is returned.

Specified by:
set in interface SymbolTable<A>
Parameters:
variable - the variable to search for.
value - the new value for the variable.
Returns:
the old value associated with the variable.

set

public A set(java.lang.String name,
             A value)
Sets a value associated with a name..

Specified by:
set in interface SymbolTable<A>
See Also:
SymbolTable.set(Symbol, Object)

setInCurrentScope

public A setInCurrentScope(Symbol variable,
                           A value)
Changes the value associated with the specified variable only in the current scope. If it's not already defined, that's an error. The old value is returned.

Parameters:
variable - the variable to search for.
value - the new value for the variable.
Returns:
the old value associated with the variable.
Throws:
InvalidVariableException - when the variable is undeclared (in this or any previous scope).

toString

public java.lang.String toString()
The symbol table as a string.

Overrides:
toString in class java.lang.Object

getVariablesAsString

public java.lang.String getVariablesAsString()
Returns the variables of the scope. Useful in debugging.

Specified by:
getVariablesAsString in interface SymbolTable<A>