Lines
94.94 %
Functions
100 %
Branches
//! The user functions and macros a walked body reaches.
use super::{CaptureSet, walk, walk_lambda};
use crate::ast::Expr;
use crate::compiler::expr::fold::template::expand_macro_use;
use crate::runtime::{Head, MAX_SHARED_RECURSION_DEPTH, SymbolTable};
/// A macro's expansion is walked in place of its use, unless a LABELS function
/// of its name hides it; a use that cannot expand may store to anything the
/// closure captures.
pub(super) fn follow_macro(
symbols: &SymbolTable,
bound: &[String],
head: &str,
args: &[Expr],
captures: &mut CaptureSet,
) {
if !matches!(symbols.resolve_head(head), Head::Macro(..))
|| bound.contains(&local_function_key(head))
{
return;
}
if captures.depth >= MAX_SHARED_RECURSION_DEPTH {
captures.refuse(SymbolTable::recursion_ceiling_error());
let mut scratch = symbols.clone();
let expanded = scratch
.enter_macro_expansion()
.and_then(|()| expand_macro_use(&mut scratch, head, args));
match expanded {
Ok(Some(expansion)) => {
captures.depth += 1;
walk(&scratch, bound, &expansion, captures);
captures.depth -= 1;
Ok(None) => captures.opaque_store = true,
Err(error) => captures.refuse(error),
/// A call to a user function is inlined into the closure body, so the free
/// names of the function's own body are the closure's too.
pub(super) fn follow_function(
if !bound.contains(&local_function_key(head)) {
follow_global(symbols, bound, head, captures);
fn follow_global(symbols: &SymbolTable, bound: &[String], head: &str, captures: &mut CaptureSet) {
let Head::Function(params, body) = symbols.resolve_head(head) else {
};
if captures.followed.iter().any(|name| name == head) {
captures.followed.push(head.to_string());
walk_lambda(symbols, bound, params, body, captures);
/// The scope entry that marks `name` as a LABELS function: a lexical function
/// shadows a global one only for a call or `#'name`, never for `'name`.
pub(super) fn local_function_key(name: &str) -> String {
format!("#'{name}")
/// `#'g` names a call made later; it sees a LABELS function before a global.
pub(super) fn follow_designator(
operand: Option<&Expr>,
if let Some(Expr::Symbol(name)) = operand {
follow_function(symbols, bound, &name.to_uppercase(), captures);
/// A variable whose compile-time value names a global function calls it when
/// the closure funcalls the variable.
pub(super) fn follow_value(
value: &Expr,
let mut pending = vec![value];
while let Some(form) = pending.pop() {
match form {
Expr::Symbol(name) => follow_global(symbols, bound, &name.to_uppercase(), captures),
Expr::List(items) => pending.extend(items),
Expr::Cons(car, cdr) => pending.extend([car.as_ref(), cdr.as_ref()]),
Expr::Quote(inner) | Expr::Datum(inner) | Expr::Lambda(_, inner) => {
pending.push(inner);
_ => {}