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

    suggestedName

    The 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.

    type

    A string expressing the symbol’s type.

    context

    The context in which the symbol is used.

    id

    A 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

    name

    The 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

    suggestedName

    The 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

    name

    The 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 returns nil.

  • 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

    id

    The id of the symbol to search for.

    context

    The 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()