1
//! The earlier-parameter rule for function defaults (ISS-094) on the macro
2
//! EXPANSION of a default, which only exists where the default is evaluated.
3

            
4
use crate::ast::{Expr, LambdaParams};
5
use crate::compiler::special::binding::reject_defaults_reading_earlier_params;
6
use crate::error::{Error, Result};
7
use crate::runtime::{Head, SymbolTable};
8

            
9
use super::fold::template::expand_macro_use;
10

            
11
3609917
pub(super) fn reject_expanded_defaults(
12
3609917
    symbols: &SymbolTable,
13
3609917
    context: &str,
14
3609917
    params: &LambdaParams,
15
3609917
) -> Result<()> {
16
3609917
    let mut scratch = symbols.clone();
17
3609917
    reject_defaults_reading_earlier_params(context, params, &mut |default, name| {
18
412553
        reads_expanded(&mut scratch, default, name)
19
412553
    })
20
3609917
}
21

            
22
436151
fn reads_expanded(scratch: &mut SymbolTable, expr: &Expr, name: &str) -> Result<bool> {
23
436151
    match expr {
24
13561
        Expr::Symbol(symbol) => Ok(symbol == name),
25
7941
        Expr::List(elems) => match elems.split_first() {
26
7941
            Some((Expr::Symbol(head), _)) if head.eq_ignore_ascii_case("QUOTE") => Ok(false),
27
            Some((Expr::Symbol(head), args))
28
7941
                if matches!(scratch.resolve_head(head), Head::Macro(..)) =>
29
            {
30
                reads_expansion(scratch, head, args, name)
31
            }
32
7941
            _ => reads_any(scratch, elems, name),
33
        },
34
        Expr::Cons(car, cdr) => {
35
            Ok(reads_expanded(scratch, car, name)? || reads_expanded(scratch, cdr, name)?)
36
        }
37
        Expr::Quasiquote(template) => {
38
5
            for form in template.unquoted_forms() {
39
1
                if reads_expanded(scratch, form, name)? {
40
1
                    return Ok(true);
41
                }
42
            }
43
4
            Ok(false)
44
        }
45
        Expr::Unquote(inner) | Expr::UnquoteSplicing(inner) => reads_expanded(scratch, inner, name),
46
        Expr::Lambda(_, body) => reads_expanded(scratch, body, name),
47
414644
        _ => Ok(false),
48
    }
49
436151
}
50

            
51
7941
fn reads_any(scratch: &mut SymbolTable, elems: &[Expr], name: &str) -> Result<bool> {
52
7941
    for elem in elems {
53
23595
        if reads_expanded(scratch, elem, name)? {
54
            return Ok(true);
55
23595
        }
56
    }
57
7941
    Ok(false)
58
7941
}
59

            
60
fn reads_expansion(
61
    scratch: &mut SymbolTable,
62
    head: &str,
63
    args: &[Expr],
64
    name: &str,
65
) -> Result<bool> {
66
    scratch.enter_macro_expansion()?;
67
    let result = match expand_macro_use(scratch, head, args) {
68
        Ok(Some(expansion)) => reads_expanded(scratch, &expansion, name),
69
        Ok(None) => Err(Error::Compile(format!(
70
            "the default expands {head}, whose expansion cannot be checked for an earlier parameter (ISS-094)"
71
        ))),
72
        Err(e) => Err(e),
73
    };
74
    scratch.exit_macro_expansion()?;
75
    result
76
}
77

            
78
#[cfg(test)]
79
#[path = "default_check_tests.rs"]
80
mod tests;