SymbolTable

class SymbolTable

A data structure to organize symbols.

  • Adds a symbol with a name derived from the suggested name. Returns the symbol.

    Creates a new symbol to be added to the symbol table. Generates a unique ID by using the makeUniqueName function which takes in a suggestedName and adds increasing numbers at the end to make it unique. After the unique name is generated the symbol is added to the symbolsByID and symbolsByName arrays.

    Declaration

    func addSymbolWithSuggestedName(_ suggestedName: String, type: String, context: String, id: String) -> Symbol

    Parameters

    suggestedName

    The name of the symbol.

    type

    The type if the symbol.

    context

    The context of the symbol.

    id

    The ID of the symbol.

    Return Value

    Returns a symbol generated based on the arguments provided to the function.

  • Returns true if symbolTable has symbol with name provided in name argument.

    body

    Declaration

    func hasSymbolWithName(_ name: String) -> Array

    Parameters

    name

    The name to be checked for in symbol table.

    Return Value

    True if symbolTable has a symbol with the name provided in the name argument. False otherwise.

  • Takes a suggested name and adds increasing numbers at the end to make it unique.

    body

    Declaration

    func makeUniqueName(_ originalName: String) -> String

    Parameters

    originalName

    The name given to the Symbol before it is made unique.

    Return Value

    A Symbol with a unique name.

  • Returns the symbol with the name provided in the name argument.

    Iterates through the the Array of symbols and returns the index with the matching name property.

    Declaration

    func symbolWithName(_ name: String) -> Symbol?

    Parameters

    name

    The name used to search for a Symbol.

    Return Value

    A Symbol with the same name as provided in the name argument.

  • Returns the symbol with a matching ID string as provided in the arguments.

    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?) -> type

    Parameters

    id

    ID of the Symbol to be searched for.

    context

    Context of the symbol to be searched for.

    Return Value

    The symbol with a matching id string as the one provided in the arguments.

  • Removes all symbols from self

    Iterates through the symbols array and removes all instances of symbolsById and all symbolsByName.

    Declaration

    func removeAllSymbols()