summaryrefslogtreecommitdiff
path: root/src/eval
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-03-11 12:22:27 +0100
committerLaurenz <laurmaedje@gmail.com>2022-03-11 12:22:27 +0100
commit5ce2a006b6d45d29be15e4562ae3ab4fc1b8e97c (patch)
tree454b25e04530adcdf08a161e1ab09abb33c0644c /src/eval
parente6b532391deb1e30dc356c4d20dd48199f748f29 (diff)
Consistent block naming
Diffstat (limited to 'src/eval')
-rw-r--r--src/eval/capture.rs18
-rw-r--r--src/eval/mod.rs46
-rw-r--r--src/eval/value.rs12
3 files changed, 33 insertions, 43 deletions
diff --git a/src/eval/capture.rs b/src/eval/capture.rs
index fd6b0101..b27aceb7 100644
--- a/src/eval/capture.rs
+++ b/src/eval/capture.rs
@@ -49,6 +49,15 @@ impl<'a> CapturesVisitor<'a> {
// through the expressions that contain them).
Some(Expr::Ident(ident)) => self.capture(ident),
+ // Blocks and templates create a scope.
+ Some(Expr::Code(_) | Expr::Template(_)) => {
+ self.internal.enter();
+ for child in node.children() {
+ self.visit(child);
+ }
+ self.internal.exit();
+ }
+
// A closure contains parameter bindings, which are bound before the
// body is evaluated. Care must be taken so that the default values
// of named parameters cannot access previous parameter bindings.
@@ -103,15 +112,6 @@ impl<'a> CapturesVisitor<'a> {
}
}
- // Blocks and templates create a scope.
- Some(Expr::Block(_) | Expr::Template(_)) => {
- self.internal.enter();
- for child in node.children() {
- self.visit(child);
- }
- self.internal.exit();
- }
-
// Everything else is traversed from left to right.
_ => {
for child in node.children() {
diff --git a/src/eval/mod.rs b/src/eval/mod.rs
index 590b8463..6a918dbd 100644
--- a/src/eval/mod.rs
+++ b/src/eval/mod.rs
@@ -202,11 +202,11 @@ impl Eval for Expr {
match self {
Self::Lit(v) => v.eval(ctx, scp),
Self::Ident(v) => v.eval(ctx, scp),
+ Self::Code(v) => v.eval(ctx, scp),
+ Self::Template(v) => v.eval(ctx, scp).map(Value::Template),
Self::Array(v) => v.eval(ctx, scp).map(Value::Array),
Self::Dict(v) => v.eval(ctx, scp).map(Value::Dict),
- Self::Template(v) => v.eval(ctx, scp).map(Value::Template),
Self::Group(v) => v.eval(ctx, scp),
- Self::Block(v) => v.eval(ctx, scp),
Self::Call(v) => v.eval(ctx, scp),
Self::Closure(v) => v.eval(ctx, scp),
Self::With(v) => v.eval(ctx, scp),
@@ -260,25 +260,23 @@ impl Eval for Ident {
}
}
-impl Eval for ArrayExpr {
- type Output = Array;
+impl Eval for CodeBlock {
+ type Output = Value;
fn eval(&self, ctx: &mut Context, scp: &mut Scopes) -> EvalResult<Self::Output> {
- self.items().map(|expr| expr.eval(ctx, scp)).collect()
- }
-}
+ scp.enter();
-impl Eval for DictExpr {
- type Output = Dict;
+ let mut output = Value::None;
+ for expr in self.exprs() {
+ output = join_result(output, expr.eval(ctx, scp), expr.span())?;
+ }
- fn eval(&self, ctx: &mut Context, scp: &mut Scopes) -> EvalResult<Self::Output> {
- self.items()
- .map(|x| Ok((x.name().take(), x.expr().eval(ctx, scp)?)))
- .collect()
+ scp.exit();
+ Ok(output)
}
}
-impl Eval for TemplateExpr {
+impl Eval for TemplateBlock {
type Output = Template;
fn eval(&self, ctx: &mut Context, scp: &mut Scopes) -> EvalResult<Self::Output> {
@@ -297,19 +295,21 @@ impl Eval for GroupExpr {
}
}
-impl Eval for BlockExpr {
- type Output = Value;
+impl Eval for ArrayExpr {
+ type Output = Array;
fn eval(&self, ctx: &mut Context, scp: &mut Scopes) -> EvalResult<Self::Output> {
- scp.enter();
+ self.items().map(|expr| expr.eval(ctx, scp)).collect()
+ }
+}
- let mut output = Value::None;
- for expr in self.exprs() {
- output = join_result(output, expr.eval(ctx, scp), expr.span())?;
- }
+impl Eval for DictExpr {
+ type Output = Dict;
- scp.exit();
- Ok(output)
+ fn eval(&self, ctx: &mut Context, scp: &mut Scopes) -> EvalResult<Self::Output> {
+ self.items()
+ .map(|x| Ok((x.name().take(), x.expr().eval(ctx, scp)?)))
+ .collect()
}
}
diff --git a/src/eval/value.rs b/src/eval/value.rs
index 009a1463..7d41bff5 100644
--- a/src/eval/value.rs
+++ b/src/eval/value.rs
@@ -5,7 +5,7 @@ use std::hash::{Hash, Hasher};
use std::sync::Arc;
use super::{ops, Args, Array, Class, Dict, Func, Layout, Template};
-use crate::diag::StrResult;
+use crate::diag::{with_alternative, StrResult};
use crate::geom::{Angle, Color, Fractional, Length, Linear, Relative, RgbaColor};
use crate::syntax::Spanned;
use crate::util::EcoString;
@@ -533,16 +533,6 @@ impl<T: Cast> Cast for Smart<T> {
}
}
-/// Transform `expected X, found Y` into `expected X or A, found Y`.
-pub fn with_alternative(msg: String, alt: &str) -> String {
- let mut parts = msg.split(", found ");
- if let (Some(a), Some(b)) = (parts.next(), parts.next()) {
- format!("{} or {}, found {}", a, alt, b)
- } else {
- msg
- }
-}
-
#[cfg(test)]
mod tests {
use super::*;