diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-02-17 15:47:54 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-02-17 17:32:56 +0100 |
| commit | c5e67af22bd6242366819879be84c10c4dd135be (patch) | |
| tree | d857b99b26401d1b3b74c4cebacbf086c25bef40 /src/eval/func.rs | |
| parent | 3d965ae6a479636a13b2e2f2344e8d97bedece1f (diff) | |
Merge eval and layout contexts into `Vm`
Diffstat (limited to 'src/eval/func.rs')
| -rw-r--r-- | src/eval/func.rs | 28 |
1 files changed, 14 insertions, 14 deletions
diff --git a/src/eval/func.rs b/src/eval/func.rs index a4a733b3..9a8a9c94 100644 --- a/src/eval/func.rs +++ b/src/eval/func.rs @@ -2,7 +2,7 @@ use std::fmt::{self, Debug, Formatter, Write}; use std::hash::{Hash, Hasher}; use std::sync::Arc; -use super::{Cast, Eval, EvalContext, Scope, Value}; +use super::{Cast, Eval, Scope, Value, Vm}; use crate::diag::{At, TypResult}; use crate::syntax::ast::Expr; use crate::syntax::{Span, Spanned}; @@ -27,7 +27,7 @@ impl Func { /// Create a new function from a native rust function. pub fn native( name: &'static str, - func: fn(&mut EvalContext, &mut Args) -> TypResult<Value>, + func: fn(&mut Vm, &mut Args) -> TypResult<Value>, ) -> Self { Self(Arc::new(Repr::Native(Native { name, func }))) } @@ -47,13 +47,13 @@ impl Func { } /// Call the function in the context with the arguments. - pub fn call(&self, ctx: &mut EvalContext, mut args: Args) -> TypResult<Value> { + pub fn call(&self, vm: &mut Vm, mut args: Args) -> TypResult<Value> { let value = match self.0.as_ref() { - Repr::Native(native) => (native.func)(ctx, &mut args)?, - Repr::Closure(closure) => closure.call(ctx, &mut args)?, + Repr::Native(native) => (native.func)(vm, &mut args)?, + Repr::Closure(closure) => closure.call(vm, &mut args)?, Repr::With(wrapped, applied) => { args.items.splice(.. 0, applied.items.iter().cloned()); - return wrapped.call(ctx, args); + return wrapped.call(vm, args); } }; args.finish()?; @@ -88,7 +88,7 @@ struct Native { /// The name of the function. pub name: &'static str, /// The function pointer. - pub func: fn(&mut EvalContext, &mut Args) -> TypResult<Value>, + pub func: fn(&mut Vm, &mut Args) -> TypResult<Value>, } impl Hash for Native { @@ -115,15 +115,15 @@ pub struct Closure { impl Closure { /// Call the function in the context with the arguments. - pub fn call(&self, ctx: &mut EvalContext, args: &mut Args) -> TypResult<Value> { + pub fn call(&self, vm: &mut Vm, args: &mut Args) -> TypResult<Value> { // Don't leak the scopes from the call site. Instead, we use the // scope of captured variables we collected earlier. - let prev_scopes = std::mem::take(&mut ctx.scopes); - ctx.scopes.top = self.captured.clone(); + let prev_scopes = std::mem::take(&mut vm.scopes); + vm.scopes.top = self.captured.clone(); // Parse the arguments according to the parameter list. for (param, default) in &self.params { - ctx.scopes.def_mut(param, match default { + vm.scopes.top.def_mut(param, match default { None => args.expect::<Value>(param)?, Some(default) => { args.named::<Value>(param)?.unwrap_or_else(|| default.clone()) @@ -133,14 +133,14 @@ impl Closure { // Put the remaining arguments into the sink. if let Some(sink) = &self.sink { - ctx.scopes.def_mut(sink, args.take()); + vm.scopes.top.def_mut(sink, args.take()); } // Evaluate the body. - let value = self.body.eval(ctx)?; + let value = self.body.eval(vm)?; // Restore the call site scopes. - ctx.scopes = prev_scopes; + vm.scopes = prev_scopes; Ok(value) } |
