Package net.hydromatic.morel.compile
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:
TypeResolver.TypeEnv,EvalEnv
-
-
Constructor Summary
Constructors Constructor Description Environment()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description StringasString()Converts this environment to a string.Environmentbind(Core.IdPat id, Object value)Creates an environment that is the same as a given environment, plus one more variable.protected Environmentbind(Binding binding)EnvironmentbindAll(Iterable<Binding> bindings)Creates an environment that is the same as this, plus the given bindings.(package private) abstract intdistance(int soFar, Core.NamedPat id)voidforEachType(BiConsumer<String,Type> consumer)Calls a consumer for each variable and its type.voidforEachValue(BiConsumer<String,Object> consumer)Calls a consumer for each variable and its value.abstract @Nullable BindinggetOpt(String name)Returns the binding ofnameif bound, null if not.abstract @Nullable BindinggetOpt(Core.NamedPat id)Returns the binding ofidif bound, null if not.Map<String,Binding>getValueMap()Returns a map of the values and bindings.(package private) abstract EnvironmentnearestAncestorNotObscuredBy(Set<Core.NamedPat> names)If this environment only defines bindings in the given set, returns its parent.Environmentplus(Environment env)Returns this environment plus the bindings in the given environment.(package private) abstract voidvisit(Consumer<Binding> consumer)Visits every variable binding in this environment.
-
-
-
Method Detail
-
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 ofnameif bound, null if not.
-
getOpt
public abstract @Nullable Binding getOpt(Core.NamedPat id)
Returns the binding ofidif 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(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 toUnit.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.
-
-