SymbolTable
class SymbolTable
A data structure used to register and track a set of abstract symbols in order to avoid naming collisions.
-
Creates a new symbol with the suggested name and adds it to the symbol table.
The symbols unique name is generated using the
makeUniqueName
function.Declaration
func addSymbolWithSuggestedName(_ suggestedName: String, type: String, context: String, id: String) -> Symbol
Parameters
suggestedNameThe suggested name used to derive a unique name for the symbol. If there is no preexisting symbol in the symbol table with this name, the suggested name is used as is. Otherwise a variation of the suggested name is used.
typeA string expressing the symbol’s type.
contextThe context in which the symbol is used.
idA string used to uniquely identify the symbol.
Return Value
Returns the newly created symbol.
-
Checks if a symbol in the symbol table has the provided name.
body
Declaration
func hasSymbolWithName(_ name: String) -> Array
Parameters
nameThe name to check for in the symbol table.
Return Value
true
if symbolTable contains a symbol with the provided name,false
otherwise. -
Generates a unique name based on a suggested name.
If necessary, the suggested name is made unique by appending a numeric suffix to it.
Declaration
func makeUniqueName(_ suggestedName: String) -> String
Parameters
suggestedNameThe suggested name to use for a symbol.
Return Value
A unique symbol name based on the
suggestedName
. -
Searches the symbol table for a symbol with the provided name.
Declaration
func symbolWithName(_ name: String) -> Symbol?
Parameters
nameThe name of the symbol to search for.
Return Value
The Symbol with the name
name
if it exists in the symbol table, otherwise this method returnsnil
. -
Search the symbol table for a symbol with the provided id and context.
Combines the values of the context and id arguments to create an id string that will be used to search for a symbol with a matching id string.
Declaration
func symbolForID(_ id: String, _ context: Symbol?) -> symbol
Parameters
idThe id of the symbol to search for.
contextThe context in which the symbol to search for is used.
Return Value
The symbol with the given id and context if it exists in the symbol table, otherwise this method returns
nil
. -
Removes all symbols from the symbol table.
Declaration
func removeAllSymbols()