pub struct Environment { /* private fields */ }Expand description
An environment frame (variable bindings)
Corresponds to OpenJade’s environment frames in Interpreter.
§Lexical Scoping
Each frame has:
bindings: Variables defined in this frameparent: Enclosing scope (None for global environment)
Variable lookup walks up the parent chain until found or global reached.
Implementations§
Source§impl Environment
impl Environment
Sourcepub fn new_global() -> Gc<Self>
pub fn new_global() -> Gc<Self>
Create a new global (top-level) environment
Sourcepub fn extend(parent: Gc<Environment>) -> Gc<Self>
pub fn extend(parent: Gc<Environment>) -> Gc<Self>
Create a new child environment extending the given parent
Used for:
- Function calls (parameters in new scope)
let,let*,letrecbindings- Internal defines
Sourcepub fn define(&self, name: &str, value: Value)
pub fn define(&self, name: &str, value: Value)
Define a variable in this environment (set binding)
Mutates the current frame.
Used for:
defineexpressions- Function parameter bindings
letbindings
§DSSSL Note
DSSSL is mostly functional, but define at top-level is imperative.
Internal defines in lambda bodies create a new frame.
Sourcepub fn lookup(&self, name: &str) -> Option<Value>
pub fn lookup(&self, name: &str) -> Option<Value>
Look up a variable in this environment or any parent
Returns None if variable is undefined.
Walks up the parent chain until:
- Variable found → return value
- Global reached and not found → return None
Corresponds to OpenJade’s Interpreter::lookup().
Sourcepub fn set(&self, name: &str, value: Value) -> Result<(), String>
pub fn set(&self, name: &str, value: Value) -> Result<(), String>
Set an existing variable (mutation)
Returns Ok(()) if variable found and updated, Err if not found.
Used for:
set!expressions
§R4RS vs DSSSL
R4RS allows set! on any variable.
DSSSL restricts mutation (mostly functional).
For now, we allow it (OpenJade does).
Sourcepub fn is_defined(&self, name: &str) -> bool
pub fn is_defined(&self, name: &str) -> bool
Check if a variable is defined in this environment or any parent
Sourcepub fn parent(&self) -> Option<Gc<Environment>>
pub fn parent(&self) -> Option<Gc<Environment>>
Get the parent environment (if any)
Trait Implementations§
Source§impl Clone for Environment
impl Clone for Environment
Source§fn clone(&self) -> Environment
fn clone(&self) -> Environment
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Environment
impl Debug for Environment
Source§impl Drop for Environment
impl Drop for Environment
Source§impl Trace for Environment
impl Trace for Environment
Source§fn finalize_glue(&self)
fn finalize_glue(&self)
Finalize::finalize() on this object and all
contained subobjects