diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-05-25 13:50:33 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-05-25 13:59:06 +0200 |
| commit | c010cbc17dcbb2f0d6005d21530143bf57cb5871 (patch) | |
| tree | 937fe79f0c121bcc025480181287fd4a3d0c0f4f /src/library/utility | |
| parent | 6935cf8dfefff3d6cf234f077a7d61661fd5ca57 (diff) | |
Move route from context to VM
Diffstat (limited to 'src/library/utility')
| -rw-r--r-- | src/library/utility/color.rs | 4 | ||||
| -rw-r--r-- | src/library/utility/math.rs | 18 | ||||
| -rw-r--r-- | src/library/utility/mod.rs | 23 | ||||
| -rw-r--r-- | src/library/utility/string.rs | 14 |
4 files changed, 25 insertions, 34 deletions
diff --git a/src/library/utility/color.rs b/src/library/utility/color.rs index 75410380..5857c4c7 100644 --- a/src/library/utility/color.rs +++ b/src/library/utility/color.rs @@ -3,7 +3,7 @@ use std::str::FromStr; use crate::library::prelude::*; /// Create an RGB(A) color. -pub fn rgb(_: &mut Context, args: &mut Args) -> TypResult<Value> { +pub fn rgb(_: &mut Machine, args: &mut Args) -> TypResult<Value> { Ok(Value::from( if let Some(string) = args.find::<Spanned<EcoString>>()? { match RgbaColor::from_str(&string.v) { @@ -37,7 +37,7 @@ pub fn rgb(_: &mut Context, args: &mut Args) -> TypResult<Value> { } /// Create a CMYK color. -pub fn cmyk(_: &mut Context, args: &mut Args) -> TypResult<Value> { +pub fn cmyk(_: &mut Machine, args: &mut Args) -> TypResult<Value> { struct Component(u8); castable! { diff --git a/src/library/utility/math.rs b/src/library/utility/math.rs index 63ec5e55..05c706ca 100644 --- a/src/library/utility/math.rs +++ b/src/library/utility/math.rs @@ -3,7 +3,7 @@ use std::cmp::Ordering; use crate::library::prelude::*; /// Convert a value to a integer. -pub fn int(_: &mut Context, args: &mut Args) -> TypResult<Value> { +pub fn int(_: &mut Machine, args: &mut Args) -> TypResult<Value> { let Spanned { v, span } = args.expect("value")?; Ok(Value::Int(match v { Value::Bool(v) => v as i64, @@ -18,7 +18,7 @@ pub fn int(_: &mut Context, args: &mut Args) -> TypResult<Value> { } /// Convert a value to a float. -pub fn float(_: &mut Context, args: &mut Args) -> TypResult<Value> { +pub fn float(_: &mut Machine, args: &mut Args) -> TypResult<Value> { let Spanned { v, span } = args.expect("value")?; Ok(Value::Float(match v { Value::Int(v) => v as f64, @@ -32,7 +32,7 @@ pub fn float(_: &mut Context, args: &mut Args) -> TypResult<Value> { } /// The absolute value of a numeric value. -pub fn abs(_: &mut Context, args: &mut Args) -> TypResult<Value> { +pub fn abs(_: &mut Machine, args: &mut Args) -> TypResult<Value> { let Spanned { v, span } = args.expect("numeric value")?; Ok(match v { Value::Int(v) => Value::Int(v.abs()), @@ -48,12 +48,12 @@ pub fn abs(_: &mut Context, args: &mut Args) -> TypResult<Value> { } /// The minimum of a sequence of values. -pub fn min(_: &mut Context, args: &mut Args) -> TypResult<Value> { +pub fn min(_: &mut Machine, args: &mut Args) -> TypResult<Value> { minmax(args, Ordering::Less) } /// The maximum of a sequence of values. -pub fn max(_: &mut Context, args: &mut Args) -> TypResult<Value> { +pub fn max(_: &mut Machine, args: &mut Args) -> TypResult<Value> { minmax(args, Ordering::Greater) } @@ -79,17 +79,17 @@ fn minmax(args: &mut Args, goal: Ordering) -> TypResult<Value> { } /// Whether an integer is even. -pub fn even(_: &mut Context, args: &mut Args) -> TypResult<Value> { +pub fn even(_: &mut Machine, args: &mut Args) -> TypResult<Value> { Ok(Value::Bool(args.expect::<i64>("integer")? % 2 == 0)) } /// Whether an integer is odd. -pub fn odd(_: &mut Context, args: &mut Args) -> TypResult<Value> { +pub fn odd(_: &mut Machine, args: &mut Args) -> TypResult<Value> { Ok(Value::Bool(args.expect::<i64>("integer")? % 2 != 0)) } /// The modulo of two numbers. -pub fn mod_(_: &mut Context, args: &mut Args) -> TypResult<Value> { +pub fn mod_(_: &mut Machine, args: &mut Args) -> TypResult<Value> { let Spanned { v: v1, span: span1 } = args.expect("integer or float")?; let Spanned { v: v2, span: span2 } = args.expect("integer or float")?; @@ -119,7 +119,7 @@ pub fn mod_(_: &mut Context, args: &mut Args) -> TypResult<Value> { } /// Create a sequence of numbers. -pub fn range(_: &mut Context, args: &mut Args) -> TypResult<Value> { +pub fn range(_: &mut Machine, args: &mut Args) -> TypResult<Value> { let first = args.expect::<i64>("end")?; let (start, end) = match args.eat::<i64>()? { Some(second) => (first, second), diff --git a/src/library/utility/mod.rs b/src/library/utility/mod.rs index 6f0f9a4c..051f8195 100644 --- a/src/library/utility/mod.rs +++ b/src/library/utility/mod.rs @@ -8,19 +8,17 @@ pub use color::*; pub use math::*; pub use string::*; -use std::mem; - use crate::eval::{Eval, Machine, Scopes}; use crate::library::prelude::*; use crate::source::SourceFile; /// The name of a value's type. -pub fn type_(_: &mut Context, args: &mut Args) -> TypResult<Value> { +pub fn type_(_: &mut Machine, args: &mut Args) -> TypResult<Value> { Ok(args.expect::<Value>("value")?.type_name().into()) } /// Ensure that a condition is fulfilled. -pub fn assert(_: &mut Context, args: &mut Args) -> TypResult<Value> { +pub fn assert(_: &mut Machine, args: &mut Args) -> TypResult<Value> { let Spanned { v, span } = args.expect::<Spanned<bool>>("condition")?; if !v { bail!(span, "assertion failed"); @@ -29,28 +27,21 @@ pub fn assert(_: &mut Context, args: &mut Args) -> TypResult<Value> { } /// Evaluate a string as Typst markup. -pub fn eval(ctx: &mut Context, args: &mut Args) -> TypResult<Value> { +pub fn eval(vm: &mut Machine, args: &mut Args) -> TypResult<Value> { let Spanned { v: src, span } = args.expect::<Spanned<String>>("source")?; // Parse the source and set a synthetic span for all nodes. let source = SourceFile::synthesized(src, span); let ast = source.ast()?; - // Save the old route, then detach it. - let prev_route = mem::take(&mut ctx.route); - // Evaluate the source. - let std = ctx.config.std.clone(); + let std = vm.ctx.config.std.clone(); let scopes = Scopes::new(Some(&std)); - let mut vm = Machine::new(ctx, scopes); - let result = ast.eval(&mut vm); - let flow = vm.flow; - - // Restore the old route. - ctx.route = prev_route; + let mut sub = Machine::new(vm.ctx, vec![], scopes); + let result = ast.eval(&mut sub); // Handle control flow. - if let Some(flow) = flow { + if let Some(flow) = sub.flow { return Err(flow.forbidden()); } diff --git a/src/library/utility/string.rs b/src/library/utility/string.rs index 13a6bbd8..2f80a5cb 100644 --- a/src/library/utility/string.rs +++ b/src/library/utility/string.rs @@ -4,12 +4,12 @@ use crate::eval::Regex; use crate::library::prelude::*; /// The string representation of a value. -pub fn repr(_: &mut Context, args: &mut Args) -> TypResult<Value> { +pub fn repr(_: &mut Machine, args: &mut Args) -> TypResult<Value> { Ok(args.expect::<Value>("value")?.repr().into()) } /// Cconvert a value to a string. -pub fn str(_: &mut Context, args: &mut Args) -> TypResult<Value> { +pub fn str(_: &mut Machine, args: &mut Args) -> TypResult<Value> { let Spanned { v, span } = args.expect("value")?; Ok(Value::Str(match v { Value::Int(v) => format_eco!("{}", v), @@ -20,29 +20,29 @@ pub fn str(_: &mut Context, args: &mut Args) -> TypResult<Value> { } /// Create blind text. -pub fn lorem(_: &mut Context, args: &mut Args) -> TypResult<Value> { +pub fn lorem(_: &mut Machine, args: &mut Args) -> TypResult<Value> { let words: usize = args.expect("number of words")?; Ok(Value::Str(lipsum_from_seed(words, 97).into())) } /// Create a regular expression. -pub fn regex(_: &mut Context, args: &mut Args) -> TypResult<Value> { +pub fn regex(_: &mut Machine, args: &mut Args) -> TypResult<Value> { let Spanned { v, span } = args.expect::<Spanned<EcoString>>("regular expression")?; Ok(Regex::new(&v).at(span)?.into()) } /// Converts an integer into one or multiple letters. -pub fn letter(_: &mut Context, args: &mut Args) -> TypResult<Value> { +pub fn letter(_: &mut Machine, args: &mut Args) -> TypResult<Value> { convert(Numbering::Letter, args) } /// Converts an integer into a roman numeral. -pub fn roman(_: &mut Context, args: &mut Args) -> TypResult<Value> { +pub fn roman(_: &mut Machine, args: &mut Args) -> TypResult<Value> { convert(Numbering::Roman, args) } /// Convert a number into a symbol. -pub fn symbol(_: &mut Context, args: &mut Args) -> TypResult<Value> { +pub fn symbol(_: &mut Machine, args: &mut Args) -> TypResult<Value> { convert(Numbering::Symbol, args) } |
