Lines
100 %
Functions
Branches
//! The implicit `(block nil …)` of `DO`, `DO*` and `DOLIST`: a loop whose
//! body may `return` is lowered as the explicit BLOCK around itself, so the
//! one BLOCK / RETURN-FROM lowering serves both.
use crate::ast::Expr;
use crate::runtime::SymbolTable;
use super::exit_scan::loop_exits;
const LOOPS: [&str; 3] = ["DO", "DO*", "DOLIST"];
/// Whether `head` names a loop that opens an implicit NIL block.
pub(super) fn is_loop(head: &str) -> bool {
LOOPS.iter().any(|name| head.eq_ignore_ascii_case(name))
}
/// The loop form `name args…` when it must be wrapped in its NIL block: a
/// `return` may leave it and the wrap is not already open.
pub(in crate::compiler::special) fn wrapped_loop(
symbols: &SymbolTable,
name: &str,
args: &[Expr],
) -> Option<Expr> {
if !LOOPS.contains(&name) || symbols.is_loop_block_open(name, args) {
return None;
let looped = Expr::List(
std::iter::once(Expr::symbol(name))
.chain(args.iter().cloned())
.collect(),
);
loop_exits(symbols, &looped).then_some(looped)
#[cfg(test)]
#[path = "nil_block_tests.rs"]
mod tests;