1
use super::*;
2
use crate::ast::WasmType;
3
use crate::compiler::expr::format_form;
4
use crate::reader::Reader;
5
use crate::runtime::{Symbol, SymbolKind};
6

            
7
34
fn parse(source: &str) -> Expr {
8
34
    Reader::parse(source).expect("parses").exprs.remove(0)
9
34
}
10

            
11
10
fn scope() -> SymbolTable {
12
10
    let mut symbols = SymbolTable::new();
13
10
    let constants = [("R", "2"), ("S", "1/3")];
14
10
    for (name, value) in constants {
15
20
        symbols.define(Symbol::new(name, SymbolKind::Variable).with_value(parse(value)));
16
    }
17
10
    let identity = Expr::Lambda(
18
10
        LambdaParams::simple(vec!["Y".to_string()]),
19
10
        Box::new(Expr::symbol("Y")),
20
10
    );
21
10
    symbols.define(Symbol::new("F", SymbolKind::Variable).with_value(identity));
22
10
    symbols.define(
23
10
        Symbol::new("N", SymbolKind::Variable).with_value(Expr::WasmRuntime(WasmType::I32)),
24
10
    );
25
10
    symbols
26
10
}
27

            
28
7
fn closed(params: &[&str], body: &str) -> String {
29
7
    let params = LambdaParams::simple(params.iter().map(ToString::to_string).collect());
30
7
    format_form(&close_over_constants(&scope(), &params, parse(body)).expect("closes"))
31
7
}
32

            
33
#[test]
34
1
fn the_constants_a_body_reads_are_bound_around_it() {
35
1
    assert_eq!(closed(&["X"], "(* x r)"), "(LET ((R 2)) (* X R))");
36
1
    assert_eq!(
37
1
        closed(&["X"], "(* x r s)"),
38
1
        "(LET ((R 2) (S 1/3)) (* X R S))"
39
    );
40
1
}
41

            
42
#[test]
43
1
fn a_constant_the_body_does_not_read_is_not_bound() {
44
1
    assert_eq!(closed(&["X"], "(* x 5)"), "(* X 5)");
45
1
}
46

            
47
#[test]
48
1
fn a_parameter_shadows_the_constant_of_its_name() {
49
1
    assert_eq!(closed(&["R"], "(* r 5)"), "(* R 5)");
50
1
}
51

            
52
#[test]
53
1
fn a_constant_the_body_stores_to_is_left_free() {
54
1
    assert_eq!(closed(&[], "(setf r 3)"), "(SETF R 3)");
55
1
}
56

            
57
#[test]
58
1
fn a_constant_lambda_is_bound_like_any_other() {
59
1
    assert_eq!(
60
1
        closed(&["X"], "(funcall f x)"),
61
1
        "(LET ((F (LAMBDA (Y) Y))) (FUNCALL F X))"
62
    );
63
1
}
64

            
65
#[test]
66
1
fn a_runtime_variable_is_not_a_constant() {
67
1
    assert_eq!(closed(&[], "n"), "N");
68
1
}
69

            
70
#[test]
71
1
fn a_constant_holding_a_runtime_value_cannot_be_bound() {
72
1
    let mut symbols = scope();
73
1
    let runtime = Expr::List(vec![Expr::WasmLocal(3, WasmType::I32)]);
74
1
    symbols.define(Symbol::new("B", SymbolKind::Variable).with_value(runtime));
75
1
    let params = LambdaParams::simple(vec![]);
76
1
    assert!(reads_unbound_constant(&symbols, &params, &parse("b")));
77
1
    assert!(!reads_unbound_constant(&symbols, &params, &parse("r")));
78
1
}
79

            
80
#[test]
81
1
fn a_symbol_or_list_constant_is_bound_quoted() {
82
1
    let mut symbols = scope();
83
1
    symbols.define(Symbol::new("Q", SymbolKind::Variable).with_value(parse("Y")));
84
1
    symbols.define(Symbol::new("L", SymbolKind::Variable).with_value(parse("(+ y 1)")));
85
1
    let params = LambdaParams::simple(vec![]);
86
1
    let closed = close_over_constants(&symbols, &params, parse("(list q l)")).expect("closes");
87
1
    assert_eq!(
88
1
        format_form(&closed),
89
1
        "(LET ((L '(+ Y 1)) (Q 'Y)) (LIST Q L))"
90
    );
91
1
}
92

            
93
#[test]
94
1
fn a_macro_time_lambda_refuses_a_name_the_caller_binds() {
95
1
    let mut symbols = scope();
96
1
    symbols.mark_macro_expansion();
97
1
    let params = LambdaParams::simple(vec![]);
98
1
    let err = close_over_constants(&symbols, &params, parse("r")).expect_err("caller lexical");
99
1
    assert!(matches!(err, Error::UndefinedSymbol(name) if name == "R"));
100
1
    let err = close_over_constants(&symbols, &params, parse("zz")).expect_err("unbound");
101
1
    assert!(matches!(err, Error::UndefinedSymbol(name) if name == "ZZ"));
102
1
}