Environment¶
Internal documentation for the amalgam.environment module.
- class amalgam.environment.Environment(bindings: Bindings = None, parent: Environment = None, name: str = 'unknown', engine: Engine = None)[source]¶
Class that manages and represents nested execution environments.
- bindings¶
A mapping of
strkeys toamalgams.Amalgamvalues.- Type
Dict[str, Amalgam]
- parent¶
The parent
Environmentinstance to search into, forming a linked list.- Type
Optional[Environment]
- level¶
The current length of the
Environmentlinked list. If aparentis provided, sets the current value to the parent’slevel+ 1.- Type
int
- search_depth¶
The search depth when traversing the
Environmentlinked list in the__contains__(),__delitem__(),__getitem__(), and__setitem__()methods.- Type
int
- name¶
The name of the execution environment.
- Type
str
- engine¶
A reference to the engine managing the
parser.Parserinstance and the globalEnvironmentinstance.- Type
Engine
- __contains__(item: str) → bool[source]¶
Recursively checks whether an item exists.
Searches with respect to the current
search_depthof the callingEnvironmentinstance. If the target item is encountered at a certain depth less than the target depth, immediately returns True, otherwise, returns False.
- __delitem__(item: str) → None[source]¶
Attempts to recursively delete the provided item.
Searches with respect to the current
search_depthof the callingEnvironmentinstance. If an existing item is encountered at a certain depth less than the target depth, deletes that item instead.
- __getitem__(item: str) → Amalgam[source]¶
Attempts to recursively obtain the provided item.
Searches with respect to the current
search_depthof the callingEnvironmentinstance. If an existing item is encountered at a certain depth less than the target depth, returns that item, otherwise, raisesSymbolNotFound.
- __setitem__(item: str, value: Amalgam) → None[source]¶
Attempts to recursively set the provided value to an item.
Searches with respect to the current
search_depthof the callingEnvironmentinstance. If an existing item is encountered at a certain depth less than the target depth, overrides that item instead.
- env_pop() → amalgam.environment.Environment[source]¶
Discards the current
Environmentand returns the parentEnvironment.
- env_push(bindings: Bindings = None, name: str = None) → Environment[source]¶
Creates a new
Environmentand binds the calling instance as its parent environment.
- search_at(*, depth=0)[source]¶
Context manager for temporarily setting the lookup depth.
The provided depth argument must not exceed the
levelof the callingEnvironmentinstance, and will raise aValueErrorif done so.>>> env = Environment(FUNCTIONS) >>> >>> with env.search_at(depth=42): ... env["+"] # Raises ValueError
Any negative integer can be passed as a depth to signify an infinite lookup until the top-most environment.
>>> env = Environment(FUNCTIONS) >>> cl_env = env.env_push({...}) >>> >>> with cl_env.search_at(depth=-1): ... cl_env["+"] # Searches `env`
- property search_chain: Iterable[Dict[str, Amalgam]]¶
Yields
bindingsof nestedEnvironmentinstances.
- class amalgam.environment.TopLevelPop[source]¶
Raised at
Environment.env_pop().