use super::*;
pub(super) fn completes(body: &[Stmt]) -> bool {
body.iter().all(stmt_completes)
}
pub(super) fn stmt_completes(st: &Stmt) -> bool {
match st {
Stmt::Return { .. }
| Stmt::Jump { .. }
| Stmt::End(_)
| Stmt::Break(_)
| Stmt::Continue(_) => false,
Stmt::If {
arms, else_body, ..
} => {
else_body.is_empty()
|| arms.iter().any(|(_, body)| completes(body))
|| completes(else_body)
}
Stmt::While { cond, body, .. } => {
!(matches!(cond, Expr::Bool(true, _)) && !contains_break(body))
}
Stmt::Choice { options, .. } => options.iter().any(|o| completes(&o.body)),
Stmt::Match { arms, .. } => arms.is_empty() || arms.iter().any(|a| completes(&a.body)),
_ => true,
}
}
pub(super) fn contains_break(body: &[Stmt]) -> bool {
body.iter().any(|st| match st {
Stmt::Break(_) => true,
Stmt::If {
arms, else_body, ..
} => arms.iter().any(|(_, b)| contains_break(b)) || contains_break(else_body),
Stmt::Choice { options, .. } => options.iter().any(|o| contains_break(&o.body)),
Stmt::Match { arms, .. } => arms.iter().any(|a| contains_break(&a.body)),
_ => false,
})
}
pub(super) fn assign_op_name(op: AssignOp) -> &'static str {
match op {
AssignOp::Set => "=",
AssignOp::Add => "+=",
AssignOp::Sub => "-=",
AssignOp::Mul => "*=",
AssignOp::Div => "/=",
AssignOp::Rem => "%=",
}
}
pub(super) fn bin_op_name(op: BinOp) -> &'static str {
match op {
BinOp::Add => "+",
BinOp::Sub => "-",
BinOp::Mul => "*",
BinOp::Div => "/",
BinOp::Rem => "%",
BinOp::Lt => "<",
BinOp::Le => "<=",
BinOp::Gt => ">",
BinOp::Ge => ">=",
BinOp::Eq => "==",
BinOp::Ne => "!=",
BinOp::And => "&&",
BinOp::Or => "||",
}
}