Environment
class Environment : public std::enable_shared_from_this<Environment>
Variable binding context with lexical scope chain
Environments form chains for lexical scoping:
@mermaid
graph LR
A["Global Env
parent: nullptr"] --> B["Outer Scope
parent: Global"]
B --> C["Inner Scope
parent: Outer"]
C --> D["Innermost
parent: Inner"]
@endmermaid
Variable Lookup (lexical scoping): - Check local bindings first - Follow parent chain to global scope - Throws EvalError if not found
Variable Definition:
- Always local to current environment
- Does not shadow parent bindings
- Use write to modify parent-defined variables
Enables: - Closures with captured environments - Function parameter bindings - Local variable scopes - Proper recursion support
- See
- Closure for environment capture
- See
- SEval for scope management
Functions
| Name | Description |
|---|---|
| get | Lookup variable in scope chain, throws EvalError if undefined |
| define | Define variable in local scope (does not shadow parents) |
| write | Modify existing variable in scope chain |
| undefine | Remove variable from local scope |
| is_defined | Check if variable is defined anywhere in chain |
| get_parent | See : for scope chain architecture |
| get_bindings | Access to local bindings for introspection |
Function Details
define
void define(const std::string& name, const Value& value)
Define variable in local scope (does not shadow parents)
get
Value get(const std::string& name) const
Lookup variable in scope chain, throws EvalError if undefined
get_bindings
const std::unordered_map<std::string, Value>& get_bindings() const
Access to local bindings for introspection
get_parent
std::shared_ptr<Environment> get_parent() const
- See
- for scope chain architecture
is_defined
bool is_defined(const std::string& name) const
Check if variable is defined anywhere in chain
undefine
void undefine(const std::string& name)
Remove variable from local scope
write
void write(const std::string& name, const Value& value)
Modify existing variable in scope chain