symbolHandling
Class AbstractTable<T extends AbstractSymbol>

java.lang.Object
  extended by symbolHandling.AbstractTable<T>
Direct Known Subclasses:
IdTable, IntTable, StringTable

public abstract class AbstractTable<T extends AbstractSymbol>
extends java.lang.Object

Abstract string table implementation.

All compilers manage large numbers of strings such as program identifiers, numerical constants, and string constants. Often, many of these strings are the same. For example, each identifier typically occurs many times in a program. To ensure that string constants are stored compactly and manipulated efficiently, a specialized data structure, the string table, is employed.

A string table is a lookup table that maintains a single copy of each string. The Cool string table class provides methods for inserting and querying string tables in a variety of ways. While production compilers use hashed data structures to implement string tables, the Cool string tables are implemented as vectors. The components of Cool string tables are of any type T to be specified during instanciation, which has to be derived from AbstractSymbol. Each AbstractSymbol stores a string, and an integer index unique to the string.

An important point about the structure of the Cool compiler is that there are actually three distinct string tables: one for string constants (AbstractTable.stringtable), one for integer constants (AbstractTable.inttable), and one for identifiers (AbstractTable.idtable). The code generator must distinguish integer constants and string constants from each other and from identifiers, because special code is produced for each string constant and each integer constant in the program. Having three distinct string tables makes this distinction easy. Note that each of the three tables has a different element type (StringSymbol, IntSymbol, and IdSymbol), each of which is a derived class of AbstractSymbol.

Because string tables store only one copy of each string, comparing whether two IntSymbols, StrSymbols, or IdSymbols x and y represent the same string can be done simply by comparing the two references x == y. Note that it does not make sense to compare entries from different string tables (e.g., IdSymbols with StringSymbols as these are guaranteed to be different even if the strings are the same.

See Also:
AbstractSymbol, StringSymbol, IdSymbol, IntSymbol

Field Summary
static IdTable idtable
          Global string table of identifiers
static IntTable inttable
          Global string table of integer constants
static StringTable stringtable
          Global string table of string constants
protected  java.util.Vector<T> table
          Vector of table entries
 
Constructor Summary
AbstractTable()
           
 
Method Summary
 T addInt(int i)
          Adds the string representation of the specified integer to this string table
 T addString(java.lang.String s)
          Adds the specified string to this string table
 T addString(java.lang.String s, int maxchars)
          Adds prefix of the specified length to this string table
protected abstract  T getNewSymbol(java.lang.String s, int len, int index)
          Creates a new symbol of the appropriate type
 java.util.Enumeration<T> getSymbols()
          Returns an enumeration of symbols in this string table
 T lookup(int index)
          Looks up a symbol in this string table by its index A fatal error is signalled if the index is out of bounds.
 T lookup(java.lang.String s)
          Looks up a symbol in this string table by its string representation A fatal error is signalled if the string is not in the table
 java.lang.String toString()
          Produces a printable representation of the string table
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

stringtable

public static StringTable stringtable
Global string table of string constants


idtable

public static IdTable idtable
Global string table of identifiers


inttable

public static IntTable inttable
Global string table of integer constants


table

protected java.util.Vector<T extends AbstractSymbol> table
Vector of table entries

Constructor Detail

AbstractTable

public AbstractTable()
Method Detail

getNewSymbol

protected abstract T getNewSymbol(java.lang.String s,
                                  int len,
                                  int index)
Creates a new symbol of the appropriate type


addString

public T addString(java.lang.String s,
                   int maxchars)
Adds prefix of the specified length to this string table

Parameters:
s - the string to add
maxchars - the length of the prefix
Returns:
the symbol for the string s

addString

public T addString(java.lang.String s)
Adds the specified string to this string table

Parameters:
s - the string to add
Returns:
the symbol for the string s

addInt

public T addInt(int i)
Adds the string representation of the specified integer to this string table

Parameters:
i - the integer to add
Returns:
the symbol for the integer i

getSymbols

public java.util.Enumeration<T> getSymbols()
Returns an enumeration of symbols in this string table

Returns:
an enumeration of symbols
See Also:
Enumeration

lookup

public T lookup(int index)
Looks up a symbol in this string table by its index A fatal error is signalled if the index is out of bounds.

Parameters:
index - the index of the symbol
Returns:
a symbol corresponding to the index

lookup

public T lookup(java.lang.String s)
Looks up a symbol in this string table by its string representation A fatal error is signalled if the string is not in the table

Parameters:
s - the string representation of the symbol
Returns:
a symbol corresponding to the string

toString

public java.lang.String toString()
Produces a printable representation of the string table

Overrides:
toString in class java.lang.Object