org.norecess.citkit.environment
Class Environment<T>

java.lang.Object
  extended by org.norecess.citkit.environment.Environment<T>
All Implemented Interfaces:
java.io.Serializable, IEnvironment<T>

public class Environment<T>
extends java.lang.Object
implements IEnvironment<T>

Implements a variable environment for a Hobbes interpreter. Variables must be defined in the source before they can be used or re-set; this is handled by add(ISymbol, Object). Variables are retrieved with get(ISymbol). Variable are reset with a new value through set(ISymbol, Object).

The InvalidVariableException class is used as a general "you did something wrong with a variable" problem. It applies in three situations: (1) a variable is redeclared in the same scope; (2) an undeclared variable is accessed; and (3) an undeclared variable's value is re-set.

See Also:
Serialized Form

Constructor Summary
Environment()
           
Environment(IEnvironment<T> previous)
           
Environment(java.util.Map<ISymbol,T> currentScope, IEnvironment<T> previous)
           
 
Method Summary
 void add(ISymbol variable, T value)
          Adds a new variable to the current environment.
 Environment<T> create()
          Creates a nested environment.
 void define(ISymbol variable, T value)
          Defines the variable in the top level environment.
 boolean equals(java.lang.Object o)
           
 T get(ISymbol variable)
          Returns the value associated with the specified variable.
 int hashCode()
           
 T set(ISymbol variable, T value)
          Changes the value associated with the specified variable.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Environment

public Environment()

Environment

public Environment(IEnvironment<T> previous)

Environment

public Environment(java.util.Map<ISymbol,T> currentScope,
                   IEnvironment<T> previous)
Method Detail

equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

define

public void define(ISymbol variable,
                   T value)
Description copied from interface: IEnvironment
Defines the variable in the top level environment.

Specified by:
define in interface IEnvironment<T>
Parameters:
variable - the variable to define.
value - the value to set it to.

add

public void add(ISymbol variable,
                T value)
         throws InvalidVariableException
Description copied from interface: IEnvironment
Adds a new variable to the current environment. Presently, this does not check the previous environments to see if this shadows an earlier declaration; if this were implemented, it'd be a warning, not an error.

Specified by:
add in interface IEnvironment<T>
Parameters:
variable - the new variable to add to the environment.
value - the new value to associate with the new variable.
Throws:
InvalidVariableException - when the variable is already declared in the current scope.

get

public T get(ISymbol variable)
      throws InvalidVariableException
Description copied from interface: IEnvironment
Returns the value associated with the specified variable. The variable is searched for in all of the previous scopes.

Specified by:
get in interface IEnvironment<T>
Parameters:
variable - the variable to search for.
Returns:
the value associated with the variable.
Throws:
InvalidVariableException - when the variable is undeclared (in this or any previous scope).

set

public T set(ISymbol variable,
             T value)
      throws InvalidVariableException
Description copied from interface: IEnvironment
Changes the value associated with the specified variable. The variable is searched for in all of the previous scopes; the old value is returned.

Specified by:
set in interface IEnvironment<T>
Parameters:
variable - the variable to search for.
value - the new value for the variable.
Returns:
the old value associated with the variable.
Throws:
InvalidVariableException - when the variable is undeclared (in this or any previous scope).

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

create

public Environment<T> create()
Description copied from interface: IEnvironment
Creates a nested environment.

Specified by:
create in interface IEnvironment<T>