Lines
90.91 %
Functions
100 %
Branches
//! The lexical bindings a `DEFUN` keeps past the binder that made them.
use crate::ast::Expr;
use crate::error::Result;
use crate::runtime::{Symbol, SymbolTable};
use super::shared::{assigns_through_macros, reads_free};
/// The lexical bindings in scope that `function` reads and never assigns: a
/// store would have to reach the binding's one shared place.
pub(super) fn kept_lexicals(symbols: &SymbolTable, function: &Expr) -> Result<Vec<Symbol>> {
let Expr::Lambda(_, body) = function else {
return Ok(Vec::new());
};
let mut kept = Vec::new();
for binding in symbols.lexical_bindings() {
if reads_free(symbols, function, binding.name())?
&& !assigns_through_macros(symbols, body, binding.name())?
{
kept.push(binding);
}
Ok(kept)