Class Environment

java.lang.Object
net.hydromatic.morel.compile.Environment
Direct Known Subclasses:
Environments.EmptyEnvironment, Environments.MapEnvironment, Environments.SubEnvironment

public abstract class Environment extends Object
Environment for validation/compilation.

Every environment is immutable; when you call bind(net.hydromatic.morel.ast.Core.IdPat, java.lang.Object), a new environment is created that inherits from the previous environment. The new environment may obscure bindings in the old environment, but neither the new nor the old will ever change.

To create an empty environment, call Environments.empty().

See Also:
  • Constructor Details

    • Environment

      public Environment()
  • Method Details

    • visit

      abstract void visit(Consumer<Binding> consumer)
      Visits every variable binding in this environment.

      Bindings that are obscured by more recent bindings of the same name are visited, but after the more obscuring bindings.

    • asString

      public String asString()
      Converts this environment to a string.

      This method does not override the Object.toString() method; if we did, debuggers would invoke it automatically, burning lots of CPU and memory.

    • getOpt

      public abstract @Nullable Binding getOpt(String name)
      Returns the binding of name if bound, null if not.
    • getOpt

      public abstract @Nullable Binding getOpt(Core.NamedPat id)
      Returns the binding of id if bound, null if not.
    • bind

      public Environment bind(Core.IdPat id, Object value)
      Creates an environment that is the same as a given environment, plus one more variable.
    • bind

      protected Environment bind(Binding binding)
    • forEachType

      public void forEachType(TypeSystem typeSystem, BiConsumer<String,Type> consumer)
      Calls a consumer for each variable and its type. Does not visit obscured bindings.
    • forEachValue

      public void forEachValue(BiConsumer<String,Object> consumer)
      Calls a consumer for each variable and its value. Does not visit obscured bindings, or bindings to Unit.INSTANCE.
    • getValueMap

      public final Map<String,Binding> getValueMap()
      Returns a map of the values and bindings.
    • bindAll

      public final Environment bindAll(Iterable<Binding> bindings)
      Creates an environment that is the same as this, plus the given bindings.
    • nearestAncestorNotObscuredBy

      abstract Environment nearestAncestorNotObscuredBy(Set<Core.NamedPat> names)
      If this environment only defines bindings in the given set, returns its parent. Never returns null. The empty environment returns itself.
    • distance

      abstract int distance(int soFar, Core.NamedPat id)
    • plus

      public Environment plus(Environment env)
      Returns this environment plus the bindings in the given environment.