Package symbols

Class SymbolTable


  • public final class SymbolTable
    extends java.lang.Object
    The SymbolTable class is used to store Symbol objects that have been generated during compilation.

    As compilation proceeds variables may be declared, which results in the creation of Symbol Objects. These are passed to an instance of SymbolTable and stored within an internal HashMap, from which the Symbols can be retrieved.

    Since:
    1.0
    • Constructor Summary

      Constructors 
      Constructor Description
      SymbolTable()
      Constructs a SymbolTable object for storing Symbols that are declared during compilation.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(java.lang.String name, java.lang.Object value, ObjectType type)
      Generate and add a Symbol object to the SymbolTable.
      void add​(Symbol symbol)
      Add a Symbol object to the SymbolTable.
      void clear()
      Clears the SymbolTable, removing all stored Symbol objects.
      boolean contains​(java.lang.String name)
      Returns a boolean indicating whether the SymbolTable holds a particular Symbol.
      Symbol get​(java.lang.String name)
      Returns a Symbol contained within the SymbolTable.
      void print()
      Pretty prints the SymbolTable to the console.
      void remove​(java.lang.String name)
      Remove a Symbol from the SymbolTable by its name.
      void remove​(Symbol symbol)
      Remove a Symbol from the SymbolTable.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SymbolTable

        public SymbolTable()
        Constructs a SymbolTable object for storing Symbols that are declared during compilation.

        A HashMap is initialised on construction for storing the Symbol objects.

    • Method Detail

      • contains

        public boolean contains​(java.lang.String name)
        Returns a boolean indicating whether the SymbolTable holds a particular Symbol.

        If true, the SymbolTable contains the specified Symbol. If false, the Symbol is not present.

        Parameters:
        name - A String identifying the Symbol by name
        Returns:
        A boolean indicating if the SymbolTable contains a particular symbol
      • get

        public Symbol get​(java.lang.String name)
        Returns a Symbol contained within the SymbolTable.

        Returns null if the symbol does not exist within the SymbolTable.

        Parameters:
        name - A String identifying the Symbol by name
        Returns:
        The specified Symbol object
      • add

        public void add​(Symbol symbol)
        Add a Symbol object to the SymbolTable.
        Parameters:
        symbol - The symbol to be added
      • add

        public void add​(java.lang.String name,
                        java.lang.Object value,
                        ObjectType type)
        Generate and add a Symbol object to the SymbolTable.

        From the parameters, a Symbol object is generated and added to the SymbolTable.

        If a Symbol with the same name already exists within the SymbolTable, its value and type are updated.

        Parameters:
        name - The name of the symbol
        value - The value of the symbol
        type - The type of the symbol
      • remove

        public void remove​(Symbol symbol)
        Remove a Symbol from the SymbolTable.
        Parameters:
        symbol - The symbol to be removed
      • remove

        public void remove​(java.lang.String name)
        Remove a Symbol from the SymbolTable by its name.
        Parameters:
        name - The name of the symbol
      • clear

        public void clear()
        Clears the SymbolTable, removing all stored Symbol objects.
      • print

        public void print()
        Pretty prints the SymbolTable to the console.