Fix control flow bug

This commit is contained in:
Laurenz 2022-05-13 16:39:41 +02:00
parent 05c27a581e
commit 1df621868f
3 changed files with 37 additions and 5 deletions

View File

@ -111,6 +111,7 @@ fn eval_markup(
scp: &mut Scopes, scp: &mut Scopes,
nodes: &mut impl Iterator<Item = MarkupNode>, nodes: &mut impl Iterator<Item = MarkupNode>,
) -> TypResult<Content> { ) -> TypResult<Content> {
let flow = ctx.flow.take();
let mut seq = Vec::with_capacity(nodes.size_hint().1.unwrap_or_default()); let mut seq = Vec::with_capacity(nodes.size_hint().1.unwrap_or_default());
while let Some(node) = nodes.next() { while let Some(node) = nodes.next() {
@ -146,6 +147,10 @@ fn eval_markup(
} }
} }
if flow.is_some() {
ctx.flow = flow;
}
Ok(Content::sequence(seq)) Ok(Content::sequence(seq))
} }
@ -343,6 +348,7 @@ fn eval_code(
scp: &mut Scopes, scp: &mut Scopes,
exprs: &mut impl Iterator<Item = Expr>, exprs: &mut impl Iterator<Item = Expr>,
) -> TypResult<Value> { ) -> TypResult<Value> {
let flow = ctx.flow.take();
let mut output = Value::None; let mut output = Value::None;
while let Some(expr) = exprs.next() { while let Some(expr) = exprs.next() {
@ -383,6 +389,10 @@ fn eval_code(
} }
} }
if flow.is_some() {
ctx.flow = flow;
}
Ok(output) Ok(output)
} }
@ -785,6 +795,7 @@ impl Eval for WhileExpr {
type Output = Value; type Output = Value;
fn eval(&self, ctx: &mut Context, scp: &mut Scopes) -> TypResult<Self::Output> { fn eval(&self, ctx: &mut Context, scp: &mut Scopes) -> TypResult<Self::Output> {
let flow = ctx.flow.take();
let mut output = Value::None; let mut output = Value::None;
let condition = self.condition(); let condition = self.condition();
@ -804,6 +815,10 @@ impl Eval for WhileExpr {
} }
} }
if flow.is_some() {
ctx.flow = flow;
}
Ok(output) Ok(output)
} }
} }
@ -812,11 +827,12 @@ impl Eval for ForExpr {
type Output = Value; type Output = Value;
fn eval(&self, ctx: &mut Context, scp: &mut Scopes) -> TypResult<Self::Output> { fn eval(&self, ctx: &mut Context, scp: &mut Scopes) -> TypResult<Self::Output> {
macro_rules! iter { let flow = ctx.flow.take();
(for ($($binding:ident => $value:ident),*) in $iter:expr) => {{
let mut output = Value::None; let mut output = Value::None;
scp.enter(); scp.enter();
macro_rules! iter {
(for ($($binding:ident => $value:ident),*) in $iter:expr) => {{
#[allow(unused_parens)] #[allow(unused_parens)]
for ($($value),*) in $iter { for ($($value),*) in $iter {
$(scp.top.def_mut(&$binding, $value);)* $(scp.top.def_mut(&$binding, $value);)*
@ -836,8 +852,6 @@ impl Eval for ForExpr {
} }
} }
scp.exit();
return Ok(output);
}}; }};
} }
@ -878,6 +892,13 @@ impl Eval for ForExpr {
bail!(self.iter().span(), "cannot loop over {}", iter.type_name()); bail!(self.iter().span(), "cannot loop over {}", iter.type_name());
} }
} }
if flow.is_some() {
ctx.flow = flow;
}
scp.exit();
Ok(output)
} }
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.7 KiB

After

Width:  |  Height:  |  Size: 7.1 KiB

View File

@ -134,3 +134,14 @@
set text(blue, ..break) set text(blue, ..break)
[Not happening] [Not happening]
} }
---
// Test second block during break flow.
// Ref: true
#for i in range(10) {
table(
{ [A]; break },
for _ in range(3) [B]
)
}