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 the deBruijn index conversion in the compiler.

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.
 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.
 void unwind(int n)
          Pop n items.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, 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)
Look up a variable's value in the environment.

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

search

public int search(T target)
Content-addressable mode. Used in compilation to determine the deBruijn indices from an environment populated by Var objects. 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

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