Select output routine branches with ifcase
This commit is contained in:
1 parent
7964765605
commit
61cef7d16c
3 files changed
+69
-8
No files matched your search
@@ -79,6 +79,11 @@ enum Op {
|
||||
then_ops: Vec<Op>,
|
||||
else_ops: Vec<Op>,
|
||||
},
|
||||
IfCase {
|
||||
value: Integer,
|
||||
branches: Vec<Vec<Op>>,
|
||||
else_ops: Vec<Op>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
@@ -377,6 +382,33 @@ fn parse_sequence(tokens: &[Token], index: &mut usize, stop: &[&str]) -> Result<
|
||||
else_ops,
|
||||
});
|
||||
}
|
||||
"ifcase" => {
|
||||
*index += 1;
|
||||
let value = parse_integer(tokens, index)?;
|
||||
let mut branches = vec![parse_sequence(tokens, index, &["or", "else", "fi"])?];
|
||||
while let Some(Token::Command(name)) = tokens.get(*index)
|
||||
&& name == "or"
|
||||
{
|
||||
*index += 1;
|
||||
branches.push(parse_sequence(tokens, index, &["or", "else", "fi"])?);
|
||||
}
|
||||
let mut else_ops = Vec::new();
|
||||
if let Some(Token::Command(name)) = tokens.get(*index)
|
||||
&& name == "else"
|
||||
{
|
||||
*index += 1;
|
||||
else_ops = parse_sequence(tokens, index, &["fi"])?;
|
||||
}
|
||||
match tokens.get(*index) {
|
||||
Some(Token::Command(name)) if name == "fi" => *index += 1,
|
||||
_ => return Err("\\ifcase is missing \\fi".to_owned()),
|
||||
}
|
||||
ops.push(Op::IfCase {
|
||||
value,
|
||||
branches,
|
||||
else_ops,
|
||||
});
|
||||
}
|
||||
other => return Err(format!("unsupported \\output command `\\{other}`")),
|
||||
},
|
||||
token => return Err(format!("unexpected token {token:?}")),
|
||||
@@ -491,6 +523,22 @@ fn execute(ops: &[Op], context: &OutputContext, shipped: &mut usize) -> Result<(
|
||||
};
|
||||
execute(if taken { then_ops } else { else_ops }, context, shipped)?;
|
||||
}
|
||||
Op::IfCase {
|
||||
value,
|
||||
branches,
|
||||
else_ops,
|
||||
} => {
|
||||
let value = value.resolve(context);
|
||||
let branch: &[Op] = if value >= 0 {
|
||||
branches
|
||||
.get(value as usize)
|
||||
.map(|branch| branch.as_slice())
|
||||
.unwrap_or(else_ops.as_slice())
|
||||
} else {
|
||||
else_ops.as_slice()
|
||||
};
|
||||
execute(branch, context, shipped)?;
|
||||
}
|
||||
Op::IfOdd {
|
||||
value,
|
||||
then_ops,
|
||||
@@ -584,6 +632,19 @@ mod tests {
|
||||
assert_eq!(program.shipout_count(&context).unwrap(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ifcase_selects_a_branch_or_the_else_clause() {
|
||||
let program =
|
||||
OutputProgram::parse(r"\ifcase\pageno\relax\or\shipout\box255\else\relax\fi").unwrap();
|
||||
let mut context = context(0);
|
||||
context.pageno = 1;
|
||||
assert_eq!(program.shipout_count(&context).unwrap(), 1);
|
||||
context.pageno = 0;
|
||||
assert_eq!(program.shipout_count(&context).unwrap(), 0);
|
||||
context.pageno = 5;
|
||||
assert_eq!(program.shipout_count(&context).unwrap(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsupported_commands_and_unbalanced_conditionals_are_errors() {
|
||||
assert!(OutputProgram::parse(r"\vbox{}").is_err());
|
||||
|
||||
Reference in new issue
Block a user