1
//! The implicit `(block nil …)` of `DO`, `DO*` and `DOLIST`: a loop whose
2
//! body may `return` is lowered as the explicit BLOCK around itself, so the
3
//! one BLOCK / RETURN-FROM lowering serves both.
4

            
5
use crate::ast::Expr;
6
use crate::runtime::SymbolTable;
7

            
8
use super::exit_scan::loop_exits;
9

            
10
const LOOPS: [&str; 3] = ["DO", "DO*", "DOLIST"];
11

            
12
/// Whether `head` names a loop that opens an implicit NIL block.
13
1538318
pub(super) fn is_loop(head: &str) -> bool {
14
3760424
    LOOPS.iter().any(|name| head.eq_ignore_ascii_case(name))
15
1538318
}
16

            
17
/// The loop form `name args…` when it must be wrapped in its NIL block: a
18
/// `return` may leave it and the wrap is not already open.
19
19314267
pub(in crate::compiler::special) fn wrapped_loop(
20
19314267
    symbols: &SymbolTable,
21
19314267
    name: &str,
22
19314267
    args: &[Expr],
23
19314267
) -> Option<Expr> {
24
19314267
    if !LOOPS.contains(&name) || symbols.is_loop_block_open(name, args) {
25
18849259
        return None;
26
465008
    }
27
465008
    let looped = Expr::List(
28
465008
        std::iter::once(Expr::symbol(name))
29
465008
            .chain(args.iter().cloned())
30
465008
            .collect(),
31
465008
    );
32
465008
    loop_exits(symbols, &looped).then_some(looped)
33
19314267
}
34

            
35
#[cfg(test)]
36
#[path = "nil_block_tests.rs"]
37
mod tests;