|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectsymbolHandling.AbstractTable<T>
public abstract class AbstractTable<T extends AbstractSymbol>
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.
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 |
---|
public static StringTable stringtable
public static IdTable idtable
public static IntTable inttable
protected java.util.Vector<T extends AbstractSymbol> table
Constructor Detail |
---|
public AbstractTable()
Method Detail |
---|
protected abstract T getNewSymbol(java.lang.String s, int len, int index)
public T addString(java.lang.String s, int maxchars)
s
- the string to addmaxchars
- the length of the prefix
public T addString(java.lang.String s)
s
- the string to add
public T addInt(int i)
i
- the integer to add
public java.util.Enumeration<T> getSymbols()
Enumeration
public T lookup(int index)
index
- the index of the symbol
public T lookup(java.lang.String s)
s
- the string representation of the symbol
public java.lang.String toString()
toString
in class java.lang.Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |