Class Environment

java.lang.Object
org.jfuncmachine.compiler.classgen.Environment

public class Environment extends Object
A map of the local variables available within a method. Environments can be chained, so that within a method, if there is a block defined that adds local variables within a particular scope, an environment can be chained to the existing one defining just those variables added by the block. When the block is no longer in scope, the chained environment is discarded so its variables are no longer visible. This class also assists in identifying variables captured by a lambda.
  • Constructor Details

    • Environment

      public Environment(MethodDef currentMethodDef)
      Create a new environment for the current method definition
      Parameters:
      currentMethodDef - The current method definition
    • Environment

      public Environment(Environment parent)
      Create a new environment as the child of an existing environment
      Parameters:
      parent - The parent environment
  • Method Details

    • allocate

      public EnvVar allocate(String name, Type type)
      Allocate a new environment variable
      Parameters:
      name - The name of the variable
      type - The type of the variable
      Returns:
      An object containing the name, type and index of the variable
    • allocate

      public EnvVar allocate(Type type)
      Allocate an anonymous variable.
      Parameters:
      type - The type of the anonymous variable
      Returns:
      An object containing the name, type and index of the variable
    • free

      public void free(int loc)
      Free a previously-allocated variable by location
      Parameters:
      loc - The location of the variable to release
    • free

      public void free(EnvVar envVar)
      Free a previously allocated variable
      Parameters:
      envVar - The variable to free
    • getVar

      public EnvVar getVar(String name)
      Retrieves a variable by name
      Parameters:
      name - The name of the variable to retrieve
      Returns:
      An object containing the name, type, and index of the variable
    • putBinding

      public void putBinding(String key, Binding binding)
      Stores the binding associated with a particular variable name
      Parameters:
      key - The variable name associated with the binding
      binding - The binding the key is associated with
    • getBinding

      public Binding getBinding(String key)
      Retrieves the binding that a particular variable is associated with
      Parameters:
      key - The variable name to look up
      Returns:
      The binding associated with the variable identified by key
    • getCurrentMethod

      public MethodDef getCurrentMethod()
      Return the current method being processed
      Returns:
      The current method being processed
    • startCaptureAnalysis

      public void startCaptureAnalysis()
      Start a capture analysis to find variables captured by a closure
    • getCaptured

      public Set<EnvVar> getCaptured()
      Return a set of the variables captured during the analysis
      Returns:
      A set containing the values identified as captured
    • checkCaptured

      public void checkCaptured(String name)
      Check to see if the named variable is captured
      Parameters:
      name - The name to check
    • checkCaptured

      public void checkCaptured(String name, Environment headOfCapture)
      Check to see if a variable has been captured. This version of checkCaptured is above the head of capture, so any variables checked are definitely captured, so they are added to the capture list in the headOfCapture environment
      Parameters:
      name - The variable name to check
      headOfCapture - The environment that contains the list of captured variables