Lines
97.44 %
Functions
100 %
Branches
//! The aliases that name a LABELS function for good: a lambda or DEFUN made
//! inside the form calls its alias, which no call site can rebind and which
//! ends with the form.
use std::collections::BTreeSet;
use super::entry::{Function, Symbol};
use super::table::SymbolTable;
const ALIAS_MARK: &str = " (LABELS ";
impl SymbolTable {
/// The unspellable name of the LABELS function `name` of binding `id`.
pub(super) fn local_alias(name: &str, id: u32) -> String {
format!("{name}{ALIAS_MARK}{id})")
}
/// The LABELS function an alias was made for.
#[must_use]
pub fn local_alias_origin(alias: &str) -> Option<&str> {
alias.split_once(ALIAS_MARK).map(|(name, _)| name)
/// The name of the function `name` to a reader: an alias shows the
/// function it names.
pub(super) fn shown(name: &str) -> &str {
Self::local_alias_origin(name).unwrap_or(name)
/// Binds the alias of the LABELS function `name`, its twin.
pub(crate) fn define_local_alias(&mut self, name: &str) {
let Some(function) = self.lookup(name).and_then(|symbol| symbol.function.clone()) else {
return;
};
let alias = Self::local_alias(name, function.definition);
self.define_scoped(Symbol::from_function(
alias,
Function {
local: false,
definition: 0,
..function
},
));
/// The LABELS functions in scope with the alias of each.
pub(crate) fn local_function_aliases(&self) -> Vec<(String, String)> {
let origins: BTreeSet<&str> = self
.symbols
.keys()
.filter_map(|name| Self::local_alias_origin(name))
.collect();
origins
.into_iter()
.filter_map(|name| {
let id = self.visible_local_function(name)?.function_definition();
Some((name.to_string(), Self::local_alias(name, id)))
})
.collect()
/// The LABELS function `name` calls.
fn visible_local_function(&self, name: &str) -> Option<&Symbol> {
self.symbols
.get(name)
.filter(|symbol| symbol.is_local_function())