com.go.tea.compiler
Class Scope

java.lang.Object
  |
  +--com.go.tea.compiler.Scope

public class Scope
extends java.lang.Object

A Scope encapsulates a set of declared variables and references to them. Scopes can be nested, and child scopes have access to variables in the parent scope.

Version:
28 , 00/11/16
Author:
Brian S O'Neill

Constructor Summary
Scope()
           
Scope(Scope parent)
           
 
Method Summary
 boolean bindToVariable(VariableRef ref)
          Attempt to bind variable reference to a variable in this scope or a parent scope.
 Variable declareVariable(Variable var)
          Declare a variable for use in this scope.
 Variable declareVariable(Variable var, boolean isPrivate)
          Declare a variable for use in this scope.
 void declareVariables(Variable[] vars)
          Declare new variables in this scope.
 void delete()
          Delete this scope by detaching it from its parent.
 Scope[] getChildren()
          Returns an empty array if this scope has no children.
 Variable getDeclaredVariable(java.lang.String name)
          Returns a declared variable by name.
 Variable getDeclaredVariable(java.lang.String name, boolean publicOnly)
          Returns a declared variable by name.
 Scope getEnclosingScope(Scope scope)
          Returns the innermost enclosing scope of this and the one given.
 VariableRef[] getLocalOutOfScopeVariableRefs()
          Returns all the references made from this scope to variables declared outside of this scope.
 VariableRef[] getLocalVariableRefs()
          Returns all the references made from this scope to variables declared in this scope or in a parent.
 VariableRef[] getOutOfScopeVariableRefs()
          Returns all the references made from this scope and all child scopes to variables declared outside of this scope.
 Scope getParent()
          Returns null if this scope has no parent.
 VariableRef[] getVariableRefs()
          Returns all the variable references made from this scope and all child scopes.
 Variable[] intersect(Scope scope)
          Returns the intersection of this scope against the one given.
 boolean isEnclosing(Scope scope)
          Returns true if this scope is the same as or a parent of the one given.
 Variable[] promote()
          Returns variables to promote from this scope to a parent scope.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Scope

public Scope()

Scope

public Scope(Scope parent)
Method Detail

getParent

public Scope getParent()
Returns null if this scope has no parent.

getChildren

public Scope[] getChildren()
Returns an empty array if this scope has no children.

declareVariable

public Variable declareVariable(Variable var)
Declare a variable for use in this scope. If no variable of this name and type has been defined, it is added to the shared set of pooled variables. Returns the actual Variable object that should be used.

declareVariable

public Variable declareVariable(Variable var,
                                boolean isPrivate)
Declare a variable for use in this scope. If no variable of this name and type has been defined, it is added to the shared set of pooled variables. Returns the actual Variable object that should be used instead.
Parameters:
isPrivate - when true, variable declaration doesn't leave this scope during an intersection or promotion

declareVariables

public void declareVariables(Variable[] vars)
Declare new variables in this scope. Entries in the array are replaced with actual Variable objects that should be used instead.

getDeclaredVariable

public Variable getDeclaredVariable(java.lang.String name)
Returns a declared variable by name. Search begins in this scope and moves up into parent scopes. If not found, null is returned. The returned variable may be private or public to a scope.
Returns:
Null if no declared variable found with the given name

getDeclaredVariable

public Variable getDeclaredVariable(java.lang.String name,
                                    boolean publicOnly)
Returns a declared variable by name. Search begins in this scope and moves up into parent scopes. If not found, null is returned. A public- only variable can be requested.
Returns:
Null if no declared variable found with the given name

bindToVariable

public boolean bindToVariable(VariableRef ref)
Attempt to bind variable reference to a variable in this scope or a parent scope. If the variable to bind to isn't available or doesn't exist, false is returned.
Returns:
true if reference has been bound

getVariableRefs

public VariableRef[] getVariableRefs()
Returns all the variable references made from this scope and all child scopes.
Returns:
non-null array of VariableRefs.

getLocalVariableRefs

public VariableRef[] getLocalVariableRefs()
Returns all the references made from this scope to variables declared in this scope or in a parent.
Returns:
non-null array of VariableRefs.

getOutOfScopeVariableRefs

public VariableRef[] getOutOfScopeVariableRefs()
Returns all the references made from this scope and all child scopes to variables declared outside of this scope.

getLocalOutOfScopeVariableRefs

public VariableRef[] getLocalOutOfScopeVariableRefs()
Returns all the references made from this scope to variables declared outside of this scope.

isEnclosing

public boolean isEnclosing(Scope scope)
Returns true if this scope is the same as or a parent of the one given.

getEnclosingScope

public Scope getEnclosingScope(Scope scope)
Returns the innermost enclosing scope of this and the one given. If no enclosing scope exists, null is returned.

intersect

public Variable[] intersect(Scope scope)
Returns the intersection of this scope against the one given. An intersection operates on each scope's locally declared variables, producing a list of variables that both scopes may inherit.

The list may contain undeclared variables, and so all returned variables must be re-declared in a common parent scope. This responsibility is left to the caller, intersect does not alter the internal state of either scope.

This method is designed specifically for combining the locally declared variables of the "then" and "else" parts of an if statement.

Returns:
variables representing the intersection

promote

public Variable[] promote()
Returns variables to promote from this scope to a parent scope. Promote is similar to intersect, except it operates on this scope and its parent (if it has one).

The list may contain undeclared variables, and so all returned variables must be re-declared in a common parent scope. This responsibility is left to the caller, promote does not alter the internal state of this scope or its parent.

This method is designed specifically for promoting the locally declared variables of a loop statement's body.

Returns:
variables to promote

delete

public void delete()
Delete this scope by detaching it from its parent.

toString

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