orc.env
Class Env<T>

java.lang.Object
  extended by orc.env.Env<T>
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public final class Env<T>
extends java.lang.Object
implements java.io.Serializable, java.lang.Cloneable

Generic indexed environment, used primarily at runtime. Env is also content addressable, so it can be used for deBruijn index conversions in the compiler. Env allows null values, a capability used in the typechecker to distinguish bound types from free ones.

Currently this is implemented as a simple linked-list of bindings, which provides O(n) lookups and O(1) copies. This sounds inefficient, but in practice turns out to be just as good or better than more complicated schemes for O(1) lookup.

Author:
dkitchin, quark
See Also:
Serialized Form

Constructor Summary
Env()
           
 
Method Summary
 void add(T item)
          Push one item.
 void addAll(java.util.List<T> items)
          Push multiple items, in the order they appear in the list.
 Env<T> clone()
          Create an independent copy of the environment.
 Env<T> extend(T item)
          Create an independent copy of the environment, extended with a new item.
 Env<T> extendAll(java.util.List<T> items)
          Create an independent copy of the environment, extended with a list of new item.
 java.util.List<T> items()
          Return a list of items in the order they were pushed.
 T lookup(int index)
          Look up a variable's value in the environment.
 int search(T target)
          Content-addressable mode.
 java.lang.String toString()
           
 void unwind(int n)
          Pop n items.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Env

public Env()
Method Detail

add

public void add(T item)
Push one item.


addAll

public void addAll(java.util.List<T> items)
Push multiple items, in the order they appear in the list.


items

public java.util.List<T> items()
Return a list of items in the order they were pushed.


lookup

public T lookup(int index)
         throws LookupFailureException
Look up a variable's value in the environment.

Parameters:
index - Stack depth (a deBruijn index)
Returns:
The bound item
Throws:
LookupFailureException

search

public int search(T target)
           throws SearchFailureException
Content-addressable mode. Used in compilation to determine the deBruijn indices from an environment populated by variables in a different representation. Assuming no error is raised, search and lookup are inverses: search(lookup(i)) = i lookup(search(o)) = o

Parameters:
target - The item
Returns:
The index of the target item
Throws:
SearchFailureException

unwind

public void unwind(int n)
Pop n items.


clone

public Env<T> clone()
Create an independent copy of the environment.

Overrides:
clone in class java.lang.Object

extend

public Env<T> extend(T item)
Create an independent copy of the environment, extended with a new item.


extendAll

public Env<T> extendAll(java.util.List<T> items)
Create an independent copy of the environment, extended with a list of new item. The extensions occur in list order, so the last item of the list will be the most recent binding.


toString

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