diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-11-08 13:08:15 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-08 13:08:15 +0100 |
| commit | c6f8ad35f45248f1fd36ee00195966f1629c6ca7 (patch) | |
| tree | 51faa3f6bbc56f75636823adeea135ed76e1b33b /src/eval | |
| parent | ea6ee3f667e922ed2f21b08719a45d2395787932 (diff) | |
| parent | 38c5c362419c5eee7a4fdc0b43d3a9dfb339a6d2 (diff) | |
Merge pull request #46 from typst/parser-ng
Next Generation Parser
Diffstat (limited to 'src/eval')
| -rw-r--r-- | src/eval/capture.rs | 161 | ||||
| -rw-r--r-- | src/eval/mod.rs | 222 | ||||
| -rw-r--r-- | src/eval/scope.rs | 4 | ||||
| -rw-r--r-- | src/eval/walk.rs | 41 |
4 files changed, 284 insertions, 144 deletions
diff --git a/src/eval/capture.rs b/src/eval/capture.rs index f0a2b729..e47831df 100644 --- a/src/eval/capture.rs +++ b/src/eval/capture.rs @@ -1,8 +1,8 @@ use std::rc::Rc; use super::{Scope, Scopes, Value}; -use crate::syntax::visit::{immutable::visit_expr, Visit}; -use crate::syntax::{Expr, Ident}; +use crate::syntax::ast::{ClosureParam, Expr, Ident, Imports, TypedNode}; +use crate::syntax::RedRef; /// A visitor that captures variable slots. pub struct CapturesVisitor<'a> { @@ -25,32 +25,153 @@ impl<'a> CapturesVisitor<'a> { pub fn finish(self) -> Scope { self.captures } -} -impl<'ast> Visit<'ast> for CapturesVisitor<'_> { - fn visit_expr(&mut self, node: &'ast Expr) { - if let Expr::Ident(ident) = node { - // Find out whether the name is not locally defined and if so if it - // can be captured. - if self.internal.get(ident).is_none() { - if let Some(slot) = self.external.get(ident) { - self.captures.def_slot(ident.as_str(), Rc::clone(slot)); - } + /// Bind a new internal variable. + pub fn bind(&mut self, ident: Ident) { + self.internal.def_mut(ident.take(), Value::None); + } + + /// Capture a variable if it isn't internal. + pub fn capture(&mut self, ident: Ident) { + if self.internal.get(&ident).is_none() { + if let Some(slot) = self.external.get(&ident) { + self.captures.def_slot(ident.take(), Rc::clone(slot)); } - } else { - visit_expr(self, node); } } - fn visit_binding(&mut self, ident: &'ast Ident) { - self.internal.def_mut(ident.as_str(), Value::None); + /// Visit any node and collect all captured variables. + pub fn visit(&mut self, node: RedRef) { + match node.cast() { + // Every identifier is a potential variable that we need to capture. + // Identifiers that shouldn't count as captures because they + // actually bind a new name are handled further below (individually + // through the expressions that contain them). + Some(Expr::Ident(ident)) => self.capture(ident), + + // A closure contains parameter bindings, which are bound before the + // body is evaluated. Take must be taken so that the default values + // of named parameters cannot access previous parameter bindings. + Some(Expr::Closure(expr)) => { + for param in expr.params() { + if let ClosureParam::Named(named) = param { + self.visit(named.expr().as_red()); + } + } + + for param in expr.params() { + match param { + ClosureParam::Pos(ident) => self.bind(ident), + ClosureParam::Named(named) => self.bind(named.name()), + ClosureParam::Sink(ident) => self.bind(ident), + } + } + + self.visit(expr.body().as_red()); + } + + // A let expression contains a binding, but that binding is only + // active after the body is evaluated. + Some(Expr::Let(expr)) => { + if let Some(init) = expr.init() { + self.visit(init.as_red()); + } + self.bind(expr.binding()); + } + + // A for loop contains one or two bindings in its pattern. These are + // active after the iterable is evaluated but before the body is + // evaluated. + Some(Expr::For(expr)) => { + self.visit(expr.iter().as_red()); + let pattern = expr.pattern(); + if let Some(key) = pattern.key() { + self.bind(key); + } + self.bind(pattern.value()); + self.visit(expr.body().as_red()); + } + + // An import contains items, but these are active only after the + // path is evaluated. + Some(Expr::Import(expr)) => { + self.visit(expr.path().as_red()); + if let Imports::Items(items) = expr.imports() { + for item in items { + self.bind(item); + } + } + } + + // 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() { + self.visit(child); + } + } + } } +} - fn visit_enter(&mut self) { - self.internal.enter(); +#[cfg(test)] +mod tests { + use super::*; + use crate::parse::parse; + use crate::source::SourceId; + use crate::syntax::RedNode; + + #[track_caller] + fn test(src: &str, result: &[&str]) { + let green = parse(src); + let red = RedNode::from_root(green, SourceId::from_raw(0)); + + let mut scopes = Scopes::new(None); + scopes.def_const("x", 0); + scopes.def_const("y", 0); + scopes.def_const("z", 0); + + let mut visitor = CapturesVisitor::new(&scopes); + visitor.visit(red.as_ref()); + + let captures = visitor.finish(); + let mut names: Vec<_> = captures.iter().map(|(k, _)| k).collect(); + names.sort(); + + assert_eq!(names, result); } - fn visit_exit(&mut self) { - self.internal.exit(); + #[test] + fn test_captures() { + // Let binding and function definition. + test("#let x = x", &["x"]); + test("#let x; {x + y}", &["y"]); + test("#let f(x, y) = x + y", &[]); + + // Closure with different kinds of params. + test("{(x, y) => x + z}", &["z"]); + test("{(x: y, z) => x + z}", &["y"]); + test("{(..x) => x + y}", &["y"]); + test("{(x, y: x + z) => x + y}", &["x", "z"]); + + // For loop. + test("#for x in y { x + z }", &["y", "z"]); + test("#for x, y in y { x + y }", &["y"]); + + // Import. + test("#import x, y from z", &["z"]); + test("#import x, y, z from x + y", &["x", "y"]); + + // Scoping. + test("{ let x = 1; { let y = 2; y }; x + y }", &["y"]); + test("[#let x = 1]#x", &["x"]); } } diff --git a/src/eval/mod.rs b/src/eval/mod.rs index 691e3c49..fda2184e 100644 --- a/src/eval/mod.rs +++ b/src/eval/mod.rs @@ -30,16 +30,14 @@ use std::collections::HashMap; use std::io; use std::mem; use std::path::PathBuf; -use std::rc::Rc; use crate::diag::{At, Error, StrResult, Trace, Tracepoint, TypResult}; use crate::geom::{Angle, Fractional, Length, Relative}; use crate::image::ImageStore; use crate::loading::Loader; -use crate::parse::parse; use crate::source::{SourceId, SourceStore}; -use crate::syntax::visit::Visit; -use crate::syntax::*; +use crate::syntax::ast::*; +use crate::syntax::{Span, Spanned}; use crate::util::RefMutExt; use crate::Context; @@ -114,7 +112,7 @@ impl<'a> EvalContext<'a> { // Parse the file. let source = self.sources.get(id); - let ast = parse(&source)?; + let ast = source.ast()?; // Prepare the new context. let new_scopes = Scopes::new(self.scopes.base); @@ -122,7 +120,7 @@ impl<'a> EvalContext<'a> { self.route.push(id); // Evaluate the module. - let template = Rc::new(ast).eval(self).trace(|| Tracepoint::Import, span)?; + let template = ast.eval(self).trace(|| Tracepoint::Import, span)?; // Restore the old context. let new_scopes = mem::replace(&mut self.scopes, old_scopes); @@ -176,8 +174,8 @@ impl Eval for Expr { fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> { match self { - Self::Ident(v) => v.eval(ctx), Self::Lit(v) => v.eval(ctx), + Self::Ident(v) => v.eval(ctx), Self::Array(v) => v.eval(ctx).map(Value::Array), Self::Dict(v) => v.eval(ctx).map(Value::Dict), Self::Template(v) => v.eval(ctx).map(Value::Template), @@ -202,17 +200,17 @@ impl Eval for Lit { type Output = Value; fn eval(&self, _: &mut EvalContext) -> TypResult<Self::Output> { - Ok(match *self { - Self::None(_) => Value::None, - Self::Auto(_) => Value::Auto, - Self::Bool(_, v) => Value::Bool(v), - Self::Int(_, v) => Value::Int(v), - Self::Float(_, v) => Value::Float(v), - Self::Length(_, v, unit) => Value::Length(Length::with_unit(v, unit)), - Self::Angle(_, v, unit) => Value::Angle(Angle::with_unit(v, unit)), - Self::Percent(_, v) => Value::Relative(Relative::new(v / 100.0)), - Self::Fractional(_, v) => Value::Fractional(Fractional::new(v)), - Self::Str(_, ref v) => Value::Str(v.into()), + Ok(match self.kind() { + LitKind::None => Value::None, + LitKind::Auto => Value::Auto, + LitKind::Bool(v) => Value::Bool(v), + LitKind::Int(v) => Value::Int(v), + LitKind::Float(v) => Value::Float(v), + LitKind::Length(v, unit) => Value::Length(Length::with_unit(v, unit)), + LitKind::Angle(v, unit) => Value::Angle(Angle::with_unit(v, unit)), + LitKind::Percent(v) => Value::Relative(Relative::new(v / 100.0)), + LitKind::Fractional(v) => Value::Fractional(Fractional::new(v)), + LitKind::Str(ref v) => Value::Str(v.into()), }) } } @@ -223,7 +221,7 @@ impl Eval for Ident { fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> { match ctx.scopes.get(self) { Some(slot) => Ok(slot.borrow().clone()), - None => bail!(self.span, "unknown variable"), + None => bail!(self.span(), "unknown variable"), } } } @@ -232,7 +230,7 @@ impl Eval for ArrayExpr { type Output = Array; fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> { - self.items.iter().map(|expr| expr.eval(ctx)).collect() + self.items().map(|expr| expr.eval(ctx)).collect() } } @@ -240,9 +238,8 @@ impl Eval for DictExpr { type Output = Dict; fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> { - self.items - .iter() - .map(|Named { name, expr }| Ok(((&name.string).into(), expr.eval(ctx)?))) + self.items() + .map(|x| Ok((x.name().take().into(), x.expr().eval(ctx)?))) .collect() } } @@ -251,7 +248,7 @@ impl Eval for TemplateExpr { type Output = Template; fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> { - self.body.eval(ctx) + self.body().eval(ctx) } } @@ -259,7 +256,7 @@ impl Eval for GroupExpr { type Output = Value; fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> { - self.expr.eval(ctx) + self.expr().eval(ctx) } } @@ -270,7 +267,7 @@ impl Eval for BlockExpr { ctx.scopes.enter(); let mut output = Value::None; - for expr in &self.exprs { + for expr in self.exprs() { let value = expr.eval(ctx)?; output = ops::join(output, value).at(expr.span())?; } @@ -285,13 +282,13 @@ impl Eval for UnaryExpr { type Output = Value; fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> { - let value = self.expr.eval(ctx)?; - let result = match self.op { + let value = self.expr().eval(ctx)?; + let result = match self.op() { UnOp::Pos => ops::pos(value), UnOp::Neg => ops::neg(value), UnOp::Not => ops::not(value), }; - result.at(self.span) + result.at(self.span()) } } @@ -299,7 +296,7 @@ impl Eval for BinaryExpr { type Output = Value; fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> { - match self.op { + match self.op() { BinOp::Add => self.apply(ctx, ops::add), BinOp::Sub => self.apply(ctx, ops::sub), BinOp::Mul => self.apply(ctx, ops::mul), @@ -327,17 +324,17 @@ impl BinaryExpr { where F: FnOnce(Value, Value) -> StrResult<Value>, { - let lhs = self.lhs.eval(ctx)?; + let lhs = self.lhs().eval(ctx)?; // Short-circuit boolean operations. - if (self.op == BinOp::And && lhs == Value::Bool(false)) - || (self.op == BinOp::Or && lhs == Value::Bool(true)) + if (self.op() == BinOp::And && lhs == Value::Bool(false)) + || (self.op() == BinOp::Or && lhs == Value::Bool(true)) { return Ok(lhs); } - let rhs = self.rhs.eval(ctx)?; - op(lhs, rhs).at(self.span) + let rhs = self.rhs().eval(ctx)?; + op(lhs, rhs).at(self.span()) } /// Apply an assignment operation. @@ -345,10 +342,10 @@ impl BinaryExpr { where F: FnOnce(Value, Value) -> StrResult<Value>, { - let rhs = self.rhs.eval(ctx)?; - let mut target = self.lhs.access(ctx)?; + let rhs = self.rhs().eval(ctx)?; + let mut target = self.lhs().access(ctx)?; let lhs = mem::take(&mut *target); - *target = op(lhs, rhs).at(self.span)?; + *target = op(lhs, rhs).at(self.span())?; Ok(Value::None) } } @@ -357,27 +354,27 @@ impl Eval for CallExpr { type Output = Value; fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> { - let callee = self.callee.eval(ctx)?; - let mut args = self.args.eval(ctx)?; + let callee = self.callee().eval(ctx)?; + let mut args = self.args().eval(ctx)?; match callee { Value::Array(array) => { - array.get(args.into_index()?).map(Value::clone).at(self.span) + array.get(args.into_index()?).map(Value::clone).at(self.span()) } Value::Dict(dict) => { - dict.get(args.into_key()?).map(Value::clone).at(self.span) + dict.get(args.into_key()?).map(Value::clone).at(self.span()) } Value::Func(func) => { let point = || Tracepoint::Call(func.name().map(ToString::to_string)); - let value = func.call(ctx, &mut args).trace(point, self.span)?; + let value = func.call(ctx, &mut args).trace(point, self.span())?; args.finish()?; Ok(value) } v => bail!( - self.callee.span(), + self.callee().span(), "expected function or collection, found {}", v.type_name(), ), @@ -389,9 +386,9 @@ impl Eval for CallArgs { type Output = Args; fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> { - let mut items = Vec::with_capacity(self.items.len()); + let mut items = Vec::new(); - for arg in &self.items { + for arg in self.items() { let span = arg.span(); match arg { CallArg::Pos(expr) => { @@ -401,11 +398,11 @@ impl Eval for CallArgs { value: Spanned::new(expr.eval(ctx)?, expr.span()), }); } - CallArg::Named(Named { name, expr }) => { + CallArg::Named(named) => { items.push(Arg { span, - name: Some((&name.string).into()), - value: Spanned::new(expr.eval(ctx)?, expr.span()), + name: Some(named.name().take().into()), + value: Spanned::new(named.expr().eval(ctx)?, named.expr().span()), }); } CallArg::Spread(expr) => match expr.eval(ctx)? { @@ -438,7 +435,7 @@ impl Eval for CallArgs { } } - Ok(Args { span: self.span, items }) + Ok(Args { span: self.span(), items }) } } @@ -446,39 +443,38 @@ impl Eval for ClosureExpr { type Output = Value; fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> { - let name = self.name.as_ref().map(|name| name.string.clone()); - // Collect captured variables. let captured = { let mut visitor = CapturesVisitor::new(&ctx.scopes); - visitor.visit_closure(self); + visitor.visit(self.as_red()); visitor.finish() }; let mut sink = None; - let mut params = Vec::with_capacity(self.params.len()); + let mut params = Vec::new(); // Collect parameters and an optional sink parameter. - for param in &self.params { + for param in self.params() { match param { ClosureParam::Pos(name) => { - params.push((name.string.clone(), None)); + params.push((name.take(), None)); } - ClosureParam::Named(Named { name, expr }) => { - params.push((name.string.clone(), Some(expr.eval(ctx)?))); + ClosureParam::Named(named) => { + params.push((named.name().take(), Some(named.expr().eval(ctx)?))); } ClosureParam::Sink(name) => { if sink.is_some() { - bail!(name.span, "only one argument sink is allowed"); + bail!(name.span(), "only one argument sink is allowed"); } - sink = Some(name.string.clone()); + sink = Some(name.take()); } } } // Clone the body expression so that we don't have a lifetime // dependence on the AST. - let body = Rc::clone(&self.body); + let name = self.name().map(Ident::take); + let body = self.body(); // Define the actual function. let func = Function::new(name, move |ctx, args| { @@ -515,8 +511,9 @@ impl Eval for WithExpr { type Output = Value; fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> { - let wrapped = self.callee.eval(ctx)?.cast::<Function>().at(self.callee.span())?; - let applied = self.args.eval(ctx)?; + let callee = self.callee(); + let wrapped = callee.eval(ctx)?.cast::<Function>().at(callee.span())?; + let applied = self.args().eval(ctx)?; let name = wrapped.name().cloned(); let func = Function::new(name, move |ctx, args| { @@ -532,11 +529,11 @@ impl Eval for LetExpr { type Output = Value; fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> { - let value = match &self.init { + let value = match self.init() { Some(expr) => expr.eval(ctx)?, None => Value::None, }; - ctx.scopes.def_mut(self.binding.as_str(), value); + ctx.scopes.def_mut(self.binding().take(), value); Ok(Value::None) } } @@ -545,12 +542,10 @@ impl Eval for IfExpr { type Output = Value; fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> { - let condition = - self.condition.eval(ctx)?.cast::<bool>().at(self.condition.span())?; - - if condition { - self.if_body.eval(ctx) - } else if let Some(else_body) = &self.else_body { + let condition = self.condition(); + if condition.eval(ctx)?.cast::<bool>().at(condition.span())? { + self.if_body().eval(ctx) + } else if let Some(else_body) = self.else_body() { else_body.eval(ctx) } else { Ok(Value::None) @@ -564,9 +559,11 @@ impl Eval for WhileExpr { fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> { let mut output = Value::None; - while self.condition.eval(ctx)?.cast::<bool>().at(self.condition.span())? { - let value = self.body.eval(ctx)?; - output = ops::join(output, value).at(self.body.span())?; + let condition = self.condition(); + while condition.eval(ctx)?.cast::<bool>().at(condition.span())? { + let body = self.body(); + let value = body.eval(ctx)?; + output = ops::join(output, value).at(body.span())?; } Ok(output) @@ -584,40 +581,44 @@ impl Eval for ForExpr { #[allow(unused_parens)] for ($($value),*) in $iter { - $(ctx.scopes.def_mut($binding.as_str(), $value);)* + $(ctx.scopes.def_mut(&$binding, $value);)* - let value = self.body.eval(ctx)?; + let value = self.body().eval(ctx)?; output = ops::join(output, value) - .at(self.body.span())?; + .at(self.body().span())?; } ctx.scopes.exit(); - Ok(output) + return Ok(output); }}; } - let iter = self.iter.eval(ctx)?; - match (&self.pattern, iter) { - (ForPattern::Value(v), Value::Str(string)) => { - iter!(for (v => value) in string.iter()) + let iter = self.iter().eval(ctx)?; + let pattern = self.pattern(); + let key = pattern.key().map(Ident::take); + let value = pattern.value().take(); + + match (key, value, iter) { + (None, v, Value::Str(string)) => { + iter!(for (v => value) in string.iter()); } - (ForPattern::Value(v), Value::Array(array)) => { - iter!(for (v => value) in array.into_iter()) + (None, v, Value::Array(array)) => { + iter!(for (v => value) in array.into_iter()); } - (ForPattern::KeyValue(i, v), Value::Array(array)) => { - iter!(for (i => idx, v => value) in array.into_iter().enumerate()) + (Some(i), v, Value::Array(array)) => { + iter!(for (i => idx, v => value) in array.into_iter().enumerate()); } - (ForPattern::Value(v), Value::Dict(dict)) => { - iter!(for (v => value) in dict.into_iter().map(|p| p.1)) + (None, v, Value::Dict(dict)) => { + iter!(for (v => value) in dict.into_iter().map(|p| p.1)); } - (ForPattern::KeyValue(k, v), Value::Dict(dict)) => { - iter!(for (k => key, v => value) in dict.into_iter()) + (Some(k), v, Value::Dict(dict)) => { + iter!(for (k => key, v => value) in dict.into_iter()); } - (ForPattern::KeyValue(_, _), Value::Str(_)) => { - bail!(self.pattern.span(), "mismatched pattern"); + (_, _, Value::Str(_)) => { + bail!(pattern.span(), "mismatched pattern"); } - (_, iter) => { - bail!(self.iter.span(), "cannot loop over {}", iter.type_name()); + (_, _, iter) => { + bail!(self.iter().span(), "cannot loop over {}", iter.type_name()); } } } @@ -627,23 +628,23 @@ impl Eval for ImportExpr { type Output = Value; fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> { - let path = self.path.eval(ctx)?.cast::<Str>().at(self.path.span())?; - - let file = ctx.import(&path, self.path.span())?; + let path = self.path(); + let resolved = path.eval(ctx)?.cast::<Str>().at(path.span())?; + let file = ctx.import(&resolved, path.span())?; let module = &ctx.modules[&file]; - match &self.imports { + match self.imports() { Imports::Wildcard => { for (var, slot) in module.scope.iter() { ctx.scopes.def_mut(var, slot.borrow().clone()); } } - Imports::Idents(idents) => { + Imports::Items(idents) => { for ident in idents { if let Some(slot) = module.scope.get(&ident) { - ctx.scopes.def_mut(ident.as_str(), slot.borrow().clone()); + ctx.scopes.def_mut(ident.take(), slot.borrow().clone()); } else { - bail!(ident.span, "unresolved import"); + bail!(ident.span(), "unresolved import"); } } } @@ -657,11 +658,10 @@ impl Eval for IncludeExpr { type Output = Value; fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> { - let path = self.path.eval(ctx)?.cast::<Str>().at(self.path.span())?; - - let file = ctx.import(&path, self.path.span())?; + let path = self.path(); + let resolved = path.eval(ctx)?.cast::<Str>().at(path.span())?; + let file = ctx.import(&resolved, path.span())?; let module = &ctx.modules[&file]; - Ok(Value::Template(module.template.clone())) } } @@ -689,23 +689,23 @@ impl Access for Ident { match ctx.scopes.get(self) { Some(slot) => match slot.try_borrow_mut() { Ok(guard) => Ok(guard), - Err(_) => bail!(self.span, "cannot mutate a constant"), + Err(_) => bail!(self.span(), "cannot mutate a constant"), }, - None => bail!(self.span, "unknown variable"), + None => bail!(self.span(), "unknown variable"), } } } impl Access for CallExpr { fn access<'a>(&self, ctx: &'a mut EvalContext) -> TypResult<RefMut<'a, Value>> { - let args = self.args.eval(ctx)?; - let guard = self.callee.access(ctx)?; + let args = self.args().eval(ctx)?; + let guard = self.callee().access(ctx)?; RefMut::try_map(guard, |value| match value { - Value::Array(array) => array.get_mut(args.into_index()?).at(self.span), + Value::Array(array) => array.get_mut(args.into_index()?).at(self.span()), Value::Dict(dict) => Ok(dict.get_mut(args.into_key()?)), v => bail!( - self.callee.span(), + self.callee().span(), "expected collection, found {}", v.type_name(), ), diff --git a/src/eval/scope.rs b/src/eval/scope.rs index eb057ae3..2290affd 100644 --- a/src/eval/scope.rs +++ b/src/eval/scope.rs @@ -120,6 +120,8 @@ impl Scope { impl Debug for Scope { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - self.values.fmt(f) + f.debug_map() + .entries(self.values.iter().map(|(k, v)| (k, v.borrow()))) + .finish() } } diff --git a/src/eval/walk.rs b/src/eval/walk.rs index 96138338..aab32f40 100644 --- a/src/eval/walk.rs +++ b/src/eval/walk.rs @@ -5,7 +5,7 @@ use crate::diag::TypResult; use crate::geom::Spec; use crate::layout::BlockLevel; use crate::library::{GridNode, ParChild, ParNode, TrackSizing}; -use crate::syntax::*; +use crate::syntax::ast::*; use crate::util::BoolExt; /// Walk markup, filling the currently built template. @@ -16,7 +16,7 @@ pub trait Walk { impl Walk for Markup { fn walk(&self, ctx: &mut EvalContext) -> TypResult<()> { - for node in self.iter() { + for node in self.nodes() { node.walk(ctx)?; } Ok(()) @@ -27,12 +27,13 @@ impl Walk for MarkupNode { fn walk(&self, ctx: &mut EvalContext) -> TypResult<()> { match self { Self::Space => ctx.template.space(), - Self::Linebreak(_) => ctx.template.linebreak(), - Self::Parbreak(_) => ctx.template.parbreak(), - Self::Strong(_) => ctx.template.modify(|s| s.text_mut().strong.flip()), - Self::Emph(_) => ctx.template.modify(|s| s.text_mut().emph.flip()), + Self::Linebreak => ctx.template.linebreak(), + Self::Parbreak => ctx.template.parbreak(), + Self::Strong => ctx.template.modify(|s| s.text_mut().strong.flip()), + Self::Emph => ctx.template.modify(|s| s.text_mut().emph.flip()), Self::Text(text) => ctx.template.text(text), Self::Raw(raw) => raw.walk(ctx)?, + Self::Math(math) => math.walk(ctx)?, Self::Heading(heading) => heading.walk(ctx)?, Self::List(list) => list.walk(ctx)?, Self::Enum(enum_) => enum_.walk(ctx)?, @@ -67,16 +68,32 @@ impl Walk for RawNode { } } +impl Walk for MathNode { + fn walk(&self, ctx: &mut EvalContext) -> TypResult<()> { + if self.display { + ctx.template.parbreak(); + } + + ctx.template.monospace(self.formula.trim()); + + if self.display { + ctx.template.parbreak(); + } + + Ok(()) + } +} + impl Walk for HeadingNode { fn walk(&self, ctx: &mut EvalContext) -> TypResult<()> { - let level = self.level; - let body = self.body.eval(ctx)?; + let level = self.level(); + let body = self.body().eval(ctx)?; ctx.template.parbreak(); ctx.template.save(); ctx.template.modify(move |style| { let text = style.text_mut(); - let upscale = 1.6 - 0.1 * level as f64; + let upscale = (1.6 - 0.1 * level as f64).max(0.75); text.size *= upscale; text.strong = true; }); @@ -90,7 +107,7 @@ impl Walk for HeadingNode { impl Walk for ListNode { fn walk(&self, ctx: &mut EvalContext) -> TypResult<()> { - let body = self.body.eval(ctx)?; + let body = self.body().eval(ctx)?; walk_item(ctx, Str::from('•'), body); Ok(()) } @@ -98,8 +115,8 @@ impl Walk for ListNode { impl Walk for EnumNode { fn walk(&self, ctx: &mut EvalContext) -> TypResult<()> { - let body = self.body.eval(ctx)?; - let label = format_str!("{}.", self.number.unwrap_or(1)); + let body = self.body().eval(ctx)?; + let label = format_str!("{}.", self.number().unwrap_or(1)); walk_item(ctx, label, body); Ok(()) } |
