Lines
97.67 %
Functions
94.44 %
Branches
100 %
//! A global function defined under a LABELS function of its name.
use crate::ast::Expr;
use super::entry::{Function, Symbol, SymbolKind};
use super::scope::Shadowed;
use super::table::SymbolTable;
impl SymbolTable {
/// Whether a global function can hide under an alias a quoted designator
/// names.
pub(super) fn hideable(global: &Function) -> bool {
!global.local
&& global.kind != SymbolKind::Macro
&& matches!(global.lambda, Some(Expr::Lambda(..)))
}
/// The function the outermost open frame puts back for `name`.
pub(super) fn restored_function(&self, name: &str) -> Option<&Option<Function>> {
self.scopes
.iter()
.flatten()
.find_map(|(saved, shadowed)| match shadowed {
Shadowed::Function(prior) if saved == name => Some(prior),
_ => None,
})
/// Makes `cell` the function the outermost open frame puts back for
/// `name`: a global function defined under a LABELS function of its name
/// is visible once the form ends, and a quoted designator names it
/// meanwhile. That frame also owns the alias, so it outlives inner
/// frames. Hands the cell back when no frame holds the name.
pub(super) fn defer_function(&mut self, name: &str, cell: Function) -> Option<Function> {
let alias = Self::function_alias(name);
let alias_prior = self
.symbols
.get(&alias)
.and_then(|symbol| symbol.function.clone());
let frame = self.scopes.iter_mut().find(|frame| {
frame
.any(|(saved, shadowed)| saved == name && matches!(shadowed, Shadowed::Function(_)))
});
let Some(frame) = frame else {
return Some(cell);
};
for (saved, shadowed) in frame.iter_mut() {
if let Shadowed::Function(prior) = shadowed
&& saved == name
{
*prior = Some(cell.clone());
break;
if !frame
.any(|(saved, shadowed)| *saved == alias && matches!(shadowed, Shadowed::Function(_)))
frame.push((alias.clone(), Shadowed::Function(alias_prior)));
if Self::hideable(&cell) {
self.define(Symbol::from_function(alias, cell));
} else {
self.clear_cell(&alias, |symbol| symbol.function = None);
None