use super::Simplify;
use toasty_core::stmt::{self, visit_mut};
impl Simplify<'_> {
pub(super) fn simplify_expr_let(&self, expr_let: &mut stmt::ExprLet) -> Option<stmt::Expr> {
if !expr_let.bindings.iter().all(|b| b.is_stable()) {
return None;
}
let mut body = *expr_let.body.clone();
substitute_let_bindings(&mut body, &expr_let.bindings);
Some(body)
}
}
fn substitute_let_bindings(expr: &mut stmt::Expr, bindings: &[stmt::Expr]) {
visit_mut::walk_expr_scoped_mut(expr, 0, |expr, scope_depth| match expr {
stmt::Expr::Arg(arg) if arg.nesting == scope_depth && arg.position < bindings.len() => {
*expr = bindings[arg.position].clone();
false
}
stmt::Expr::Arg(arg) if arg.nesting > scope_depth => {
arg.nesting -= 1;
false
}
_ => true,
});
}