diff options
Diffstat (limited to 'src/eval')
| -rw-r--r-- | src/eval/args.rs | 22 | ||||
| -rw-r--r-- | src/eval/array.rs | 19 | ||||
| -rw-r--r-- | src/eval/dict.rs | 9 | ||||
| -rw-r--r-- | src/eval/func.rs | 22 | ||||
| -rw-r--r-- | src/eval/methods.rs | 6 | ||||
| -rw-r--r-- | src/eval/mod.rs | 110 | ||||
| -rw-r--r-- | src/eval/scope.rs | 4 | ||||
| -rw-r--r-- | src/eval/vm.rs | 4 |
8 files changed, 95 insertions, 101 deletions
diff --git a/src/eval/args.rs b/src/eval/args.rs index 8d62b675..f95fbf08 100644 --- a/src/eval/args.rs +++ b/src/eval/args.rs @@ -1,7 +1,7 @@ use std::fmt::{self, Debug, Formatter, Write}; use super::{Array, Cast, Dict, Str, Value}; -use crate::diag::{At, TypResult}; +use crate::diag::{At, SourceResult}; use crate::syntax::{Span, Spanned}; /// Evaluated arguments to a function. @@ -48,7 +48,7 @@ impl Args { } /// Consume and cast the first positional argument if there is one. - pub fn eat<T>(&mut self) -> TypResult<Option<T>> + pub fn eat<T>(&mut self) -> SourceResult<Option<T>> where T: Cast<Spanned<Value>>, { @@ -66,7 +66,7 @@ impl Args { /// /// Returns a `missing argument: {what}` error if no positional argument is /// left. - pub fn expect<T>(&mut self, what: &str) -> TypResult<T> + pub fn expect<T>(&mut self, what: &str) -> SourceResult<T> where T: Cast<Spanned<Value>>, { @@ -77,7 +77,7 @@ impl Args { } /// Find and consume the first castable positional argument. - pub fn find<T>(&mut self) -> TypResult<Option<T>> + pub fn find<T>(&mut self) -> SourceResult<Option<T>> where T: Cast<Spanned<Value>>, { @@ -92,7 +92,7 @@ impl Args { } /// Find and consume all castable positional arguments. - pub fn all<T>(&mut self) -> TypResult<Vec<T>> + pub fn all<T>(&mut self) -> SourceResult<Vec<T>> where T: Cast<Spanned<Value>>, { @@ -105,7 +105,7 @@ impl Args { /// Cast and remove the value for the given named argument, returning an /// error if the conversion fails. - pub fn named<T>(&mut self, name: &str) -> TypResult<Option<T>> + pub fn named<T>(&mut self, name: &str) -> SourceResult<Option<T>> where T: Cast<Spanned<Value>>, { @@ -126,7 +126,7 @@ impl Args { } /// Same as named, but with fallback to find. - pub fn named_or_find<T>(&mut self, name: &str) -> TypResult<Option<T>> + pub fn named_or_find<T>(&mut self, name: &str) -> SourceResult<Option<T>> where T: Cast<Spanned<Value>>, { @@ -146,7 +146,7 @@ impl Args { /// Return an "unexpected argument" error if there is any remaining /// argument. - pub fn finish(self) -> TypResult<()> { + pub fn finish(self) -> SourceResult<()> { if let Some(arg) = self.items.first() { bail!(arg.span, "unexpected argument"); } @@ -171,17 +171,17 @@ impl Args { } /// Reinterpret these arguments as actually being an array index. - pub fn into_index(self) -> TypResult<i64> { + pub fn into_index(self) -> SourceResult<i64> { self.into_castable("index") } /// Reinterpret these arguments as actually being a dictionary key. - pub fn into_key(self) -> TypResult<Str> { + pub fn into_key(self) -> SourceResult<Str> { self.into_castable("key") } /// Reinterpret these arguments as actually being a single castable thing. - fn into_castable<T: Cast>(self, what: &str) -> TypResult<T> { + fn into_castable<T: Cast>(self, what: &str) -> SourceResult<T> { let mut iter = self.items.into_iter(); let value = match iter.next() { Some(Arg { name: None, value, .. }) => value.v.cast().at(value.span)?, diff --git a/src/eval/array.rs b/src/eval/array.rs index 6d558393..b77ce93c 100644 --- a/src/eval/array.rs +++ b/src/eval/array.rs @@ -4,7 +4,7 @@ use std::ops::{Add, AddAssign}; use std::sync::Arc; use super::{ops, Args, Func, Value, Vm}; -use crate::diag::{At, StrResult, TypResult}; +use crate::diag::{At, SourceResult, StrResult}; use crate::syntax::Spanned; use crate::util::ArcExt; @@ -124,7 +124,7 @@ impl Array { } /// Return the first matching element. - pub fn find(&self, vm: &mut Vm, f: Spanned<Func>) -> TypResult<Option<Value>> { + pub fn find(&self, vm: &mut Vm, f: Spanned<Func>) -> SourceResult<Option<Value>> { for item in self.iter() { let args = Args::new(f.span, [item.clone()]); if f.v.call(vm, args)?.cast::<bool>().at(f.span)? { @@ -136,7 +136,7 @@ impl Array { } /// Return the index of the first matching element. - pub fn position(&self, vm: &mut Vm, f: Spanned<Func>) -> TypResult<Option<i64>> { + pub fn position(&self, vm: &mut Vm, f: Spanned<Func>) -> SourceResult<Option<i64>> { for (i, item) in self.iter().enumerate() { let args = Args::new(f.span, [item.clone()]); if f.v.call(vm, args)?.cast::<bool>().at(f.span)? { @@ -149,7 +149,7 @@ impl Array { /// Return a new array with only those elements for which the function /// returns true. - pub fn filter(&self, vm: &mut Vm, f: Spanned<Func>) -> TypResult<Self> { + pub fn filter(&self, vm: &mut Vm, f: Spanned<Func>) -> SourceResult<Self> { let mut kept = vec![]; for item in self.iter() { let args = Args::new(f.span, [item.clone()]); @@ -161,10 +161,9 @@ impl Array { } /// Transform each item in the array with a function. - pub fn map(&self, vm: &mut Vm, f: Spanned<Func>) -> TypResult<Self> { + pub fn map(&self, vm: &mut Vm, f: Spanned<Func>) -> SourceResult<Self> { let enumerate = f.v.argc() == Some(2); - Ok(self - .iter() + self.iter() .enumerate() .map(|(i, item)| { let mut args = Args::new(f.span, []); @@ -174,11 +173,11 @@ impl Array { args.push(f.span, item.clone()); f.v.call(vm, args) }) - .collect::<TypResult<_>>()?) + .collect() } /// Whether any element matches. - pub fn any(&self, vm: &mut Vm, f: Spanned<Func>) -> TypResult<bool> { + pub fn any(&self, vm: &mut Vm, f: Spanned<Func>) -> SourceResult<bool> { for item in self.iter() { let args = Args::new(f.span, [item.clone()]); if f.v.call(vm, args)?.cast::<bool>().at(f.span)? { @@ -190,7 +189,7 @@ impl Array { } /// Whether all elements match. - pub fn all(&self, vm: &mut Vm, f: Spanned<Func>) -> TypResult<bool> { + pub fn all(&self, vm: &mut Vm, f: Spanned<Func>) -> SourceResult<bool> { for item in self.iter() { let args = Args::new(f.span, [item.clone()]); if !f.v.call(vm, args)?.cast::<bool>().at(f.span)? { diff --git a/src/eval/dict.rs b/src/eval/dict.rs index 3ba72c8a..b95ead31 100644 --- a/src/eval/dict.rs +++ b/src/eval/dict.rs @@ -4,7 +4,7 @@ use std::ops::{Add, AddAssign}; use std::sync::Arc; use super::{Args, Array, Func, Str, Value, Vm}; -use crate::diag::{StrResult, TypResult}; +use crate::diag::{SourceResult, StrResult}; use crate::parse::is_ident; use crate::syntax::Spanned; use crate::util::ArcExt; @@ -101,14 +101,13 @@ impl Dict { } /// Transform each pair in the array with a function. - pub fn map(&self, vm: &mut Vm, f: Spanned<Func>) -> TypResult<Array> { - Ok(self - .iter() + pub fn map(&self, vm: &mut Vm, f: Spanned<Func>) -> SourceResult<Array> { + self.iter() .map(|(key, value)| { let args = Args::new(f.span, [Value::Str(key.clone()), value.clone()]); f.v.call(vm, args) }) - .collect::<TypResult<_>>()?) + .collect() } /// Iterate over pairs of references to the contained keys and values. diff --git a/src/eval/func.rs b/src/eval/func.rs index d6b5252a..b8730a8a 100644 --- a/src/eval/func.rs +++ b/src/eval/func.rs @@ -3,7 +3,7 @@ use std::hash::{Hash, Hasher}; use std::sync::Arc; use super::{Args, Eval, Flow, Scope, Scopes, Value, Vm}; -use crate::diag::{StrResult, TypResult}; +use crate::diag::{SourceResult, StrResult}; use crate::model::{Content, NodeId, StyleMap}; use crate::source::SourceId; use crate::syntax::ast::Expr; @@ -29,7 +29,7 @@ impl Func { /// Create a new function from a native rust function. pub fn from_fn( name: &'static str, - func: fn(&mut Vm, &mut Args) -> TypResult<Value>, + func: fn(&mut Vm, &mut Args) -> SourceResult<Value>, ) -> Self { Self(Arc::new(Repr::Native(Native { name, @@ -86,7 +86,7 @@ impl Func { } /// Call the function with the given arguments. - pub fn call(&self, vm: &mut Vm, mut args: Args) -> TypResult<Value> { + pub fn call(&self, vm: &mut Vm, mut args: Args) -> SourceResult<Value> { let value = match self.0.as_ref() { Repr::Native(native) => (native.func)(vm, &mut args)?, Repr::Closure(closure) => closure.call(vm, &mut args)?, @@ -100,13 +100,13 @@ impl Func { } /// Call the function without an existing virtual machine. - pub fn call_detached(&self, world: &dyn World, args: Args) -> TypResult<Value> { + pub fn call_detached(&self, world: &dyn World, args: Args) -> SourceResult<Value> { let mut vm = Vm::new(world, vec![], Scopes::new(None)); self.call(&mut vm, args) } /// Execute the function's set rule and return the resulting style map. - pub fn set(&self, mut args: Args) -> TypResult<StyleMap> { + pub fn set(&self, mut args: Args) -> SourceResult<StyleMap> { let styles = match self.0.as_ref() { Repr::Native(Native { set: Some(set), .. }) => set(&mut args)?, _ => StyleMap::new(), @@ -144,9 +144,9 @@ struct Native { /// The name of the function. pub name: &'static str, /// The function pointer. - pub func: fn(&mut Vm, &mut Args) -> TypResult<Value>, + pub func: fn(&mut Vm, &mut Args) -> SourceResult<Value>, /// The set rule. - pub set: Option<fn(&mut Args) -> TypResult<StyleMap>>, + pub set: Option<fn(&mut Args) -> SourceResult<StyleMap>>, /// The id of the node to customize with this function's show rule. pub node: Option<NodeId>, } @@ -169,13 +169,13 @@ pub trait Node: 'static { /// /// This is passed only the arguments that remain after execution of the /// node's set rule. - fn construct(vm: &mut Vm, args: &mut Args) -> TypResult<Content>; + fn construct(vm: &mut Vm, args: &mut Args) -> SourceResult<Content>; /// Parse relevant arguments into style properties for this node. /// /// When `constructor` is true, [`construct`](Self::construct) will run /// after this invocation of `set` with the remaining arguments. - fn set(args: &mut Args, constructor: bool) -> TypResult<StyleMap>; + fn set(args: &mut Args, constructor: bool) -> SourceResult<StyleMap>; } /// A user-defined closure. @@ -198,7 +198,7 @@ pub struct Closure { impl Closure { /// Call the function in the context with the arguments. - pub fn call(&self, vm: &mut Vm, args: &mut Args) -> TypResult<Value> { + pub fn call(&self, vm: &mut Vm, args: &mut Args) -> SourceResult<Value> { // Don't leak the scopes from the call site. Instead, we use the scope // of captured variables we collected earlier. let mut scopes = Scopes::new(None); @@ -235,7 +235,7 @@ impl Closure { match sub.flow { Some(Flow::Return(_, Some(explicit))) => return Ok(explicit), Some(Flow::Return(_, None)) => {} - Some(flow) => return Err(flow.forbidden())?, + Some(flow) => bail!(flow.forbidden()), None => {} } diff --git a/src/eval/methods.rs b/src/eval/methods.rs index 08d7dd3f..19f3d65e 100644 --- a/src/eval/methods.rs +++ b/src/eval/methods.rs @@ -1,7 +1,7 @@ //! Methods on values. use super::{Args, Value, Vm}; -use crate::diag::{At, TypResult}; +use crate::diag::{At, SourceResult}; use crate::syntax::Span; use crate::util::EcoString; @@ -12,7 +12,7 @@ pub fn call( method: &str, mut args: Args, span: Span, -) -> TypResult<Value> { +) -> SourceResult<Value> { let name = value.type_name(); let missing = || Err(missing_method(name, method)).at(span); @@ -121,7 +121,7 @@ pub fn call_mut( method: &str, mut args: Args, span: Span, -) -> TypResult<()> { +) -> SourceResult<()> { let name = value.type_name(); let missing = || Err(missing_method(name, method)).at(span); diff --git a/src/eval/mod.rs b/src/eval/mod.rs index bae9ac94..eeb95534 100644 --- a/src/eval/mod.rs +++ b/src/eval/mod.rs @@ -36,7 +36,7 @@ use std::collections::BTreeMap; use unicode_segmentation::UnicodeSegmentation; -use crate::diag::{failed_to_load, At, StrResult, Trace, Tracepoint, TypResult}; +use crate::diag::{At, SourceResult, StrResult, Trace, Tracepoint}; use crate::geom::{Angle, Em, Fraction, Length, Ratio}; use crate::library; use crate::model::{Content, Pattern, Recipe, StyleEntry, StyleMap}; @@ -55,7 +55,7 @@ pub fn evaluate( world: &dyn World, id: SourceId, mut route: Vec<SourceId>, -) -> TypResult<Module> { +) -> SourceResult<Module> { // Prevent cyclic evaluation. if route.contains(&id) { let path = world.source(id).path().display(); @@ -73,7 +73,7 @@ pub fn evaluate( // Handle control flow. if let Some(flow) = vm.flow { - return Err(flow.forbidden()); + bail!(flow.forbidden()); } // Assemble the module. @@ -95,13 +95,13 @@ pub trait Eval { type Output; /// Evaluate the expression to the output value. - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output>; + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output>; } impl Eval for Markup { type Output = Content; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { eval_markup(vm, &mut self.nodes()) } } @@ -110,7 +110,7 @@ impl Eval for Markup { fn eval_markup( vm: &mut Vm, nodes: &mut impl Iterator<Item = MarkupNode>, -) -> TypResult<Content> { +) -> SourceResult<Content> { let flow = vm.flow.take(); let mut seq = Vec::with_capacity(nodes.size_hint().1.unwrap_or_default()); @@ -157,7 +157,7 @@ fn eval_markup( impl Eval for MarkupNode { type Output = Content; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { Ok(match self { Self::Space => Content::Space, Self::Parbreak => Content::Parbreak, @@ -181,7 +181,7 @@ impl Eval for MarkupNode { impl Eval for StrongNode { type Output = Content; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { Ok(Content::show(library::text::StrongNode( self.body().eval(vm)?, ))) @@ -191,7 +191,7 @@ impl Eval for StrongNode { impl Eval for EmphNode { type Output = Content; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { Ok(Content::show(library::text::EmphNode( self.body().eval(vm)?, ))) @@ -201,7 +201,7 @@ impl Eval for EmphNode { impl Eval for RawNode { type Output = Content; - fn eval(&self, _: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, _: &mut Vm) -> SourceResult<Self::Output> { let content = Content::show(library::text::RawNode { text: self.text.clone(), block: self.block, @@ -216,7 +216,7 @@ impl Eval for RawNode { impl Eval for Spanned<MathNode> { type Output = Content; - fn eval(&self, _: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, _: &mut Vm) -> SourceResult<Self::Output> { Ok(Content::show(library::math::MathNode { formula: self.clone().map(|math| math.formula), display: self.v.display, @@ -227,7 +227,7 @@ impl Eval for Spanned<MathNode> { impl Eval for HeadingNode { type Output = Content; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { Ok(Content::show(library::structure::HeadingNode { body: self.body().eval(vm)?, level: self.level(), @@ -238,7 +238,7 @@ impl Eval for HeadingNode { impl Eval for ListNode { type Output = Content; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { Ok(Content::Item(library::structure::ListItem { kind: library::structure::UNORDERED, number: None, @@ -250,7 +250,7 @@ impl Eval for ListNode { impl Eval for EnumNode { type Output = Content; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { Ok(Content::Item(library::structure::ListItem { kind: library::structure::ORDERED, number: self.number(), @@ -262,7 +262,7 @@ impl Eval for EnumNode { impl Eval for Expr { type Output = Value; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { let forbidden = |name| { error!( self.span(), @@ -285,9 +285,9 @@ impl Eval for Expr { Self::Unary(v) => v.eval(vm), Self::Binary(v) => v.eval(vm), Self::Let(v) => v.eval(vm), - Self::Set(_) => Err(forbidden("set")), - Self::Show(_) => Err(forbidden("show")), - Self::Wrap(_) => Err(forbidden("wrap")), + Self::Set(_) => bail!(forbidden("set")), + Self::Show(_) => bail!(forbidden("show")), + Self::Wrap(_) => bail!(forbidden("wrap")), Self::If(v) => v.eval(vm), Self::While(v) => v.eval(vm), Self::For(v) => v.eval(vm), @@ -303,7 +303,7 @@ impl Eval for Expr { impl Eval for Lit { type Output = Value; - fn eval(&self, _: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, _: &mut Vm) -> SourceResult<Self::Output> { Ok(match self.kind() { LitKind::None => Value::None, LitKind::Auto => Value::Auto, @@ -325,7 +325,7 @@ impl Eval for Lit { impl Eval for Ident { type Output = Value; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { vm.scopes.get(self).cloned().at(self.span()) } } @@ -333,7 +333,7 @@ impl Eval for Ident { impl Eval for CodeBlock { type Output = Value; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { vm.scopes.enter(); let output = eval_code(vm, &mut self.exprs())?; vm.scopes.exit(); @@ -342,7 +342,7 @@ impl Eval for CodeBlock { } /// Evaluate a stream of expressions. -fn eval_code(vm: &mut Vm, exprs: &mut impl Iterator<Item = Expr>) -> TypResult<Value> { +fn eval_code(vm: &mut Vm, exprs: &mut impl Iterator<Item = Expr>) -> SourceResult<Value> { let flow = vm.flow.take(); let mut output = Value::None; @@ -394,7 +394,7 @@ fn eval_code(vm: &mut Vm, exprs: &mut impl Iterator<Item = Expr>) -> TypResult<V impl Eval for ContentBlock { type Output = Content; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { vm.scopes.enter(); let content = self.body().eval(vm)?; vm.scopes.exit(); @@ -405,7 +405,7 @@ impl Eval for ContentBlock { impl Eval for GroupExpr { type Output = Value; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { self.expr().eval(vm) } } @@ -413,7 +413,7 @@ impl Eval for GroupExpr { impl Eval for ArrayExpr { type Output = Array; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { let items = self.items(); let mut vec = Vec::with_capacity(items.size_hint().0); @@ -435,7 +435,7 @@ impl Eval for ArrayExpr { impl Eval for DictExpr { type Output = Dict; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { let mut map = BTreeMap::new(); for item in self.items() { @@ -465,7 +465,7 @@ impl Eval for DictExpr { impl Eval for UnaryExpr { type Output = Value; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { let value = self.expr().eval(vm)?; let result = match self.op() { UnOp::Pos => ops::pos(value), @@ -479,7 +479,7 @@ impl Eval for UnaryExpr { impl Eval for BinaryExpr { type Output = Value; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { match self.op() { BinOp::Add => self.apply(vm, ops::add), BinOp::Sub => self.apply(vm, ops::sub), @@ -510,7 +510,7 @@ impl BinaryExpr { &self, vm: &mut Vm, op: fn(Value, Value) -> StrResult<Value>, - ) -> TypResult<Value> { + ) -> SourceResult<Value> { let lhs = self.lhs().eval(vm)?; // Short-circuit boolean operations. @@ -529,7 +529,7 @@ impl BinaryExpr { &self, vm: &mut Vm, op: fn(Value, Value) -> StrResult<Value>, - ) -> TypResult<Value> { + ) -> SourceResult<Value> { let rhs = self.rhs().eval(vm)?; let location = self.lhs().access(vm)?; let lhs = std::mem::take(&mut *location); @@ -541,7 +541,7 @@ impl BinaryExpr { impl Eval for FieldAccess { type Output = Value; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { let object = self.object().eval(vm)?; let span = self.field().span(); let field = self.field().take(); @@ -567,7 +567,7 @@ impl Eval for FieldAccess { impl Eval for FuncCall { type Output = Value; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { let callee = self.callee().eval(vm)?; let args = self.args().eval(vm)?; @@ -591,7 +591,7 @@ impl Eval for FuncCall { impl Eval for MethodCall { type Output = Value; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { let span = self.span(); let method = self.method(); let point = || Tracepoint::Call(Some(method.to_string())); @@ -613,7 +613,7 @@ impl Eval for MethodCall { impl Eval for CallArgs { type Output = Args; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { let mut items = Vec::new(); for arg in self.items() { @@ -662,7 +662,7 @@ impl Eval for CallArgs { impl Eval for ClosureExpr { type Output = Value; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { // The closure's name is defined by its let binding if there's one. let name = self.name().map(Ident::take); @@ -709,7 +709,7 @@ impl Eval for ClosureExpr { impl Eval for LetExpr { type Output = Value; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { let value = match self.init() { Some(expr) => expr.eval(vm)?, None => Value::None, @@ -722,7 +722,7 @@ impl Eval for LetExpr { impl Eval for SetExpr { type Output = StyleMap; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { let target = self.target(); let target = target.eval(vm)?.cast::<Func>().at(target.span())?; let args = self.args().eval(vm)?; @@ -733,7 +733,7 @@ impl Eval for SetExpr { impl Eval for ShowExpr { type Output = Recipe; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { // Evaluate the target function. let pattern = self.pattern(); let pattern = pattern.eval(vm)?.cast::<Pattern>().at(pattern.span())?; @@ -770,7 +770,7 @@ impl Eval for ShowExpr { impl Eval for IfExpr { type Output = Value; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { let condition = self.condition(); if condition.eval(vm)?.cast::<bool>().at(condition.span())? { self.if_body().eval(vm) @@ -785,7 +785,7 @@ impl Eval for IfExpr { impl Eval for WhileExpr { type Output = Value; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { let flow = vm.flow.take(); let mut output = Value::None; @@ -817,7 +817,7 @@ impl Eval for WhileExpr { impl Eval for ForExpr { type Output = Value; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { let flow = vm.flow.take(); let mut output = Value::None; vm.scopes.enter(); @@ -896,7 +896,7 @@ impl Eval for ForExpr { impl Eval for ImportExpr { type Output = Value; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { let span = self.path().span(); let path = self.path().eval(vm)?.cast::<EcoString>().at(span)?; let module = import(vm, &path, span)?; @@ -925,7 +925,7 @@ impl Eval for ImportExpr { impl Eval for IncludeExpr { type Output = Content; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { let span = self.path().span(); let path = self.path().eval(vm)?.cast::<EcoString>().at(span)?; let module = import(vm, &path, span)?; @@ -934,14 +934,10 @@ impl Eval for IncludeExpr { } /// Process an import of a module relative to the current location. -fn import(vm: &mut Vm, path: &str, span: Span) -> TypResult<Module> { +fn import(vm: &mut Vm, path: &str, span: Span) -> SourceResult<Module> { // Load the source file. let full = vm.locate(&path).at(span)?; - let id = vm - .world - .resolve(&full) - .map_err(|err| failed_to_load("source file", &full, err)) - .at(span)?; + let id = vm.world.resolve(&full).at(span)?; // Prevent cyclic importing. if vm.route.contains(&id) { @@ -959,7 +955,7 @@ fn import(vm: &mut Vm, path: &str, span: Span) -> TypResult<Module> { impl Eval for BreakExpr { type Output = Value; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { if vm.flow.is_none() { vm.flow = Some(Flow::Break(self.span())); } @@ -970,7 +966,7 @@ impl Eval for BreakExpr { impl Eval for ContinueExpr { type Output = Value; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { if vm.flow.is_none() { vm.flow = Some(Flow::Continue(self.span())); } @@ -981,7 +977,7 @@ impl Eval for ContinueExpr { impl Eval for ReturnExpr { type Output = Value; - fn eval(&self, vm: &mut Vm) -> TypResult<Self::Output> { + fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { let value = self.body().map(|body| body.eval(vm)).transpose()?; if vm.flow.is_none() { vm.flow = Some(Flow::Return(self.span(), value)); @@ -993,11 +989,11 @@ impl Eval for ReturnExpr { /// Access an expression mutably. pub trait Access { /// Access the value. - fn access<'a>(&self, vm: &'a mut Vm) -> TypResult<&'a mut Value>; + fn access<'a>(&self, vm: &'a mut Vm) -> SourceResult<&'a mut Value>; } impl Access for Expr { - fn access<'a>(&self, vm: &'a mut Vm) -> TypResult<&'a mut Value> { + fn access<'a>(&self, vm: &'a mut Vm) -> SourceResult<&'a mut Value> { match self { Expr::Ident(v) => v.access(vm), Expr::FieldAccess(v) => v.access(vm), @@ -1008,13 +1004,13 @@ impl Access for Expr { } impl Access for Ident { - fn access<'a>(&self, vm: &'a mut Vm) -> TypResult<&'a mut Value> { + fn access<'a>(&self, vm: &'a mut Vm) -> SourceResult<&'a mut Value> { vm.scopes.get_mut(self).at(self.span()) } } impl Access for FieldAccess { - fn access<'a>(&self, vm: &'a mut Vm) -> TypResult<&'a mut Value> { + fn access<'a>(&self, vm: &'a mut Vm) -> SourceResult<&'a mut Value> { Ok(match self.object().access(vm)? { Value::Dict(dict) => dict.get_mut(self.field().take().into()), v => bail!( @@ -1027,7 +1023,7 @@ impl Access for FieldAccess { } impl Access for FuncCall { - fn access<'a>(&self, vm: &'a mut Vm) -> TypResult<&'a mut Value> { + fn access<'a>(&self, vm: &'a mut Vm) -> SourceResult<&'a mut Value> { let args = self.args().eval(vm)?; Ok(match self.callee().access(vm)? { Value::Array(array) => array.get_mut(args.into_index()?).at(self.span())?, diff --git a/src/eval/scope.rs b/src/eval/scope.rs index 1b11e6ea..1ab7032c 100644 --- a/src/eval/scope.rs +++ b/src/eval/scope.rs @@ -3,7 +3,7 @@ use std::fmt::{self, Debug, Formatter}; use std::hash::Hash; use super::{Args, Func, Node, Value, Vm}; -use crate::diag::{StrResult, TypResult}; +use crate::diag::{SourceResult, StrResult}; use crate::util::EcoString; /// A stack of scopes. @@ -78,7 +78,7 @@ impl Scope { pub fn def_fn( &mut self, name: &'static str, - func: fn(&mut Vm, &mut Args) -> TypResult<Value>, + func: fn(&mut Vm, &mut Args) -> SourceResult<Value>, ) { self.define(name, Func::from_fn(name, func)); } diff --git a/src/eval/vm.rs b/src/eval/vm.rs index 937152cf..7c8e8e31 100644 --- a/src/eval/vm.rs +++ b/src/eval/vm.rs @@ -1,7 +1,7 @@ use std::path::PathBuf; use super::{Scopes, Value}; -use crate::diag::{StrResult, TypError}; +use crate::diag::{SourceError, StrResult}; use crate::source::SourceId; use crate::syntax::Span; use crate::util::PathExt; @@ -56,7 +56,7 @@ pub enum Flow { impl Flow { /// Return an error stating that this control flow is forbidden. - pub fn forbidden(&self) -> TypError { + pub fn forbidden(&self) -> SourceError { match *self { Self::Break(span) => { error!(span, "cannot break outside of loop") |
