swp-uebersetzerbau-ss13 / common

Shared files between teams.
1 stars 0 forks source link

Extending the SymbolTable interface #24

Closed fub-frank closed 11 years ago

fub-frank commented 11 years ago

The SymbolTable interface needs to be extended. The following features have been requested:

Proposal

public interface SymbolTable {
    /**
     * Get the symbol table of the previous scope. If there is no previous scope returns null.
     * @return The symbol table of the previous scope or null.
     */
    public SymbolTable getParentSymbolTable();

    /**
     * Get the root symbol table
     * @return the root symbol table
     */
     public SymbolTable getRootSymbolTable();

    /**
     * Checks if the given identifier is already declared.
     * This method checks the current symbol table and all parent
     * symbol tables until the identifier is found or the root table is reached.
     * @param identifier The identifier to check.
     * @return true if the identifier is declared, false otherwise
     */
    public Boolean isDeclared(String identifier);

    /**
     * Check if the given identifier is declared in this symbol table instance.
     * It does not check the identifier in the parent symbol tables up to the root.
     * @param identifier The identifier to check.
     * @return true if the identifier is declared, false otherwise
     */
     public Boolean isDeclaredInCurrentScope(String identifier);

    /**
     * Returns the Type of the given identifier. The identifier is searched
     * in this symbol table and in all parent symbol tables up to the root until it is found.
     * @param identifier The identifier to lookup.
     * @return The Type of the identifier. Returns null if identifier was not found.
     */
    public Type lookupType(String identifier);

    /**
     * Returns the Type of the given identifier. The identifier is searched
     * in this symbol table only. Parent symbol tables are not searched.
     * @param identifier The identifier to lookup.
     * @return The Type of the identifier. Returns null if identifier was not found.
     */
    public Type lookupTypeInCurrentScope(String identifier);

    /**
     * Insert the identifier <code>identifier</code> of type <code>type</code> into the symbol table.
     * The identifier is added into this symbol table. 
     * @param identifier The identifier to insert into the symbol table.
     * @param type The Type of the identifier.
     * @return Returns false if the identifier was already declared in the current symbol table (it does not look into parent tables), true otherwise.
     */
    public Boolean insert(String identifier, Type type);

    /**
     * Remove the <code>identifier</code> from the current symbol table.
     * @param identifier The identifier to remove.
     * @return true if the identifier was removed, false if it was not in the symbol table and therefore not removed.
     */
    public Boolean remove(String identifier);

    /**
     * Set the liveliness information of the identifier in the current symbol table.
     * @param identifier The identifier.
     * @param liveliness New liveliness information.
     */
    public void setLivelinessInformation(String identifier, Liveliness liveliness);

    /**
     * Get the liveliness information of the identifier from the current symbol table.
     * @param identifier The identifier to get the liveliniess informations for.
     * @return The liveliness information or null if none.
     */
    public Liveliness getLivelinessInformation(String identifier);

    /**
     * Get the next free temporary name.
     * The returned temporary name has to be globally unique.
     * @return the next free temporary name
     */
    public String getNextFreeTemporary();

    /**
     * Put the identifier into the root symbol table.
     * @param identifier The identifier to insert into the root symbol table.
     * @param type The Type of the identifier.
     */
    public void putTemporary(String identifier, Type type);

    /**
     * Save an alias for the identifier given in the current symbol table.
     * @param identifier The identifier to set the alias for
     * @param alias The alias to define for the identifier
     */
    public void setIdentifierAlias(String identifier, String alias)

    /**
     * Get an alias for the identifier from the symbol table.
     * The alias is searched in this symbol tables and all parent symbol tables
     * up to the root table. The first alias found is returned.
     * @param identifier The identifier to get the alias for
     * @return Return the alias if any found, return the identifier if no alias was found, return null if the identifier does not exist.
     */
    public String getIdentifierAlias(String identifier)

    /**
     * Returns the SymbolTable that declares the identifier. 
     * It checks this symbol table and all parents up to the root symbol table until the identifier is found.
     * @param identifier The identifier to look for
     * @return The SymbolTable that declares the identifier or null if the identifier is not declared
     */
    public SymbolTable getDeclaringSymbolTable(String identifier);
}
EdwarDDay commented 11 years ago

Instead of isDeclared and isDeclaredInCurrentScope you could write one function with the symbol table, which declared the identifier, as result, or null if there is none. This way, isDeclared can be used with a simple null check and isDeclaredInCurrentScope with an equality check to the current symbol table. And if you need you also get the right symbol table, in which the identifier is declared.

fub-frank commented 11 years ago

I think function names beginning with "is*" should always return boolean values by name convention. If above functionality is wished we can add a new function like

/**
 * Returns the SymbolTable that declares the identifier. 
 * It checks this symbol table and all parents up to the root symbol table until the identifier is found.
 * @param identifier The identifier to look for
 * @return The SymbolTable that declares the identifier or null if the identifier is not declared
 */
public SymbolTable getDeclaringSymbolTable(String identifier);

This way, isDeclared can be used with a simple null check and isDeclaredInCurrentScope with an equality check

I am not a fan of mixing different functionalities into one function.

// update of Course isDeclared and isDeclaredInCurrentScope can then be implemented by using the getDeclaringSymbolTable method. But this would be quite inperformant, as a normal lookup in a hashmap is neraly O(1)

fub-frank commented 11 years ago

if no more comments are posted on this issue by 12a.m. tonight i will close it and update the interfaces as described above.