diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-03-03 17:53:40 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-03-03 17:53:40 +0100 |
| commit | c94a18833f23d2b57de1b87971458fd54b56d088 (patch) | |
| tree | 9e1ed55cfca15aef6d39ced50a3a5b14d2800aae /src/eval/scope.rs | |
| parent | 4d90a066f197264341eff6bf67e8c06cae434eb4 (diff) | |
Closures and function definitions 🚀
Supports:
- Closure syntax: `(x, y) => z`
- Shorthand for a single argument: `x => y`
- Function syntax: `let f(x) = y`
- Capturing of variables from the environment
- Error messages for too few / many passed arguments
Does not support:
- Named arguments
- Variadic arguments with `..`
Diffstat (limited to 'src/eval/scope.rs')
| -rw-r--r-- | src/eval/scope.rs | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/src/eval/scope.rs b/src/eval/scope.rs index 0991564f..c0926c0c 100644 --- a/src/eval/scope.rs +++ b/src/eval/scope.rs @@ -13,11 +13,11 @@ pub type Slot = Rc<RefCell<Value>>; #[derive(Debug, Default, Clone, PartialEq)] pub struct Scopes<'a> { /// The active scope. - top: Scope, + pub top: Scope, /// The stack of lower scopes. - scopes: Vec<Scope>, + pub scopes: Vec<Scope>, /// The base scope. - base: Option<&'a Scope>, + pub base: Option<&'a Scope>, } impl<'a> Scopes<'a> { @@ -39,16 +39,16 @@ impl<'a> Scopes<'a> { } } - /// Push a new scope. - pub fn push(&mut self) { + /// Enter a new scope. + pub fn enter(&mut self) { self.scopes.push(std::mem::take(&mut self.top)); } - /// Pop the topmost scope. + /// Exit the topmost scope. /// /// # Panics - /// Panics if no scope was pushed. - pub fn pop(&mut self) { + /// Panics if no scope was entered. + pub fn exit(&mut self) { self.top = self.scopes.pop().expect("no pushed scope"); } @@ -74,6 +74,7 @@ impl<'a> Scopes<'a> { /// A map from variable names to variable slots. #[derive(Default, Clone, PartialEq)] pub struct Scope { + /// The mapping from names to slots. values: HashMap<String, Slot>, } |
