1
use super::*;
2
use crate::ast::Fraction;
3
use crate::ast::Number;
4
use crate::compiler::{CompileMode, Compiler};
5
use crate::error::Error;
6
use crate::reader::Reader;
7

            
8
21
fn unit(symbols: &mut SymbolTable, source: &str) -> crate::error::Result<Vec<u8>> {
9
21
    let program = Reader::parse(source).expect("parses");
10
21
    Compiler::new().compile_with_mode(&program, symbols, CompileMode::Eval)
11
21
}
12

            
13
7
fn after_first_unit() -> SymbolTable {
14
7
    let mut symbols = SymbolTable::with_builtins();
15
7
    unit(&mut symbols, "(defvar v 1) (setf v 2)").expect("compiles");
16
7
    symbols.take_unit_slots();
17
7
    symbols
18
7
}
19

            
20
7
fn value_of(symbols: &SymbolTable, name: &str) -> Option<Expr> {
21
7
    symbols.lookup(name).and_then(|s| s.value().cloned())
22
7
}
23

            
24
#[test]
25
1
fn a_carried_constant_is_the_value_of_the_global() {
26
1
    let mut symbols = after_first_unit();
27
1
    let seven = Expr::Number(Number::Index(7));
28
1
    symbols.carry("V", Carried::Constant(seven.clone()));
29
1
    assert_eq!(value_of(&symbols, "V"), Some(seven.clone()));
30
1
    assert_eq!(
31
1
        symbols
32
1
            .lookup("V")
33
1
            .and_then(|s| s.expansion_value().cloned()),
34
1
        Some(seven)
35
    );
36
1
}
37

            
38
#[test]
39
1
fn a_carried_constant_is_read_by_the_next_unit() {
40
1
    let mut symbols = after_first_unit();
41
1
    symbols.carry(
42
1
        "V",
43
1
        Carried::Constant(Expr::Number(Number::Scalar(Fraction::from_integer(2)))),
44
1
    );
45
1
    let (_, ty) = Compiler::new()
46
1
        .compile_eval_with_type(&Reader::parse("(/ v 4/1)").expect("parses"), &mut symbols)
47
1
        .expect("compiles");
48
1
    assert_eq!(ty, Some(crate::ast::WasmType::Ratio));
49
1
}
50

            
51
#[test]
52
1
fn an_opaque_global_names_its_type_when_read() {
53
1
    let mut symbols = after_first_unit();
54
1
    symbols.carry("V", Carried::Opaque("closure".to_string()));
55
1
    assert_eq!(value_of(&symbols, "V"), None);
56
1
    let err = unit(&mut symbols, "v").expect_err("v has no value");
57
1
    assert!(
58
1
        matches!(&err, Error::Compile(msg) if msg == "V: its closure value does not survive the form"),
59
        "{err:?}"
60
    );
61
1
}
62

            
63
#[test]
64
1
fn a_constant_revives_an_opaque_global() {
65
1
    let mut symbols = after_first_unit();
66
1
    symbols.carry("V", Carried::Opaque("closure".to_string()));
67
1
    symbols.carry("V", Carried::Constant(Expr::T));
68
1
    assert_eq!(value_of(&symbols, "V"), Some(Expr::T));
69
1
    unit(&mut symbols, "v").expect("v is read again");
70
1
}
71

            
72
#[test]
73
1
fn carrying_a_name_the_table_does_not_hold_changes_nothing() {
74
1
    let mut symbols = after_first_unit();
75
1
    symbols.carry("MISSING", Carried::Constant(Expr::T));
76
1
    assert_eq!(value_of(&symbols, "MISSING"), None);
77
1
}
78

            
79
4
fn declaration_after(units: &[&str], name: &str, stored: bool) -> (bool, bool) {
80
4
    let mut symbols = SymbolTable::with_builtins();
81
4
    for source in units {
82
2
        unit(&mut symbols, source).expect("compiles");
83
2
        symbols.take_unit_slots();
84
    }
85
4
    unit(&mut symbols, "(defvar v 1)").expect("compiles");
86
4
    symbols.restore_declaration(name, stored);
87
4
    (symbols.is_special(name), symbols.is_created_global(name))
88
4
}
89

            
90
#[test]
91
1
fn an_unrun_defvar_of_a_stored_global_leaves_it_created_and_special() {
92
1
    assert_eq!(declaration_after(&["(setf v 5)"], "V", true), (true, true));
93
1
    assert_eq!(declaration_after(&[], "V", true), (true, true));
94
1
}
95

            
96
#[test]
97
1
fn an_unrun_defvar_of_an_unstored_new_name_leaves_it_undeclared() {
98
1
    assert_eq!(declaration_after(&[], "V", false), (false, false));
99
1
}
100

            
101
#[test]
102
1
fn an_unrun_defvar_of_a_special_leaves_it_special() {
103
1
    assert_eq!(
104
1
        declaration_after(&["(defvar v 5)"], "V", true),
105
1
        (true, false)
106
    );
107
1
}
108

            
109
#[test]
110
1
fn an_unbound_declaration_keeps_the_value_the_unit_started_with() {
111
1
    let mut symbols = SymbolTable::with_builtins();
112
1
    unit(&mut symbols, "(defvar v 5)").expect("compiles");
113
1
    symbols.take_unit_slots();
114
1
    unit(&mut symbols, "(defvar v 6)").expect("compiles");
115
1
    symbols.declare_unbound("V");
116
1
    assert_eq!(
117
1
        value_of(&symbols, "V"),
118
1
        Some(Expr::Number(Number::Index(5)))
119
    );
120
1
}
121

            
122
#[test]
123
1
fn an_unbound_declaration_of_a_new_name_has_no_value() {
124
1
    let mut symbols = SymbolTable::with_builtins();
125
1
    unit(&mut symbols, "(defvar v)").expect("compiles");
126
1
    symbols.declare_unbound("V");
127
1
    assert_eq!(value_of(&symbols, "V"), None);
128
1
}
129

            
130
#[test]
131
1
fn an_unbound_declaration_over_a_held_value_declares_a_created_global_special() {
132
1
    let mut symbols = SymbolTable::with_builtins();
133
1
    unit(&mut symbols, "(setf v 5)").expect("compiles");
134
1
    symbols.take_unit_slots();
135
1
    symbols.carry("V", Carried::Constant(Expr::Number(Number::Index(5))));
136
1
    unit(&mut symbols, "(defvar v)").expect("compiles");
137
1
    symbols.declare_unbound("V");
138
1
    assert!(symbols.is_special("V"));
139
1
    assert!(!symbols.is_created_global("V"));
140
1
    assert_eq!(
141
1
        value_of(&symbols, "V"),
142
1
        Some(Expr::Number(Number::Index(5)))
143
    );
144
1
}
145

            
146
3
fn closure_list_source() -> Expr {
147
3
    let lambda = Expr::Lambda(
148
3
        crate::ast::LambdaParams::simple(vec!["X".to_string()]),
149
3
        Box::new(Expr::List(vec![
150
3
            Expr::symbol("+"),
151
3
            Expr::symbol("X"),
152
3
            Expr::Number(Number::Index(1)),
153
3
        ])),
154
3
    );
155
3
    Expr::List(vec![
156
3
        Expr::symbol("LET"),
157
3
        Expr::List(vec![Expr::List(vec![Expr::symbol("CARRY-0"), lambda])]),
158
3
        Expr::List(vec![Expr::symbol("LIST"), Expr::symbol("CARRY-0")]),
159
3
    ])
160
3
}
161

            
162
#[test]
163
1
fn only_a_let_form_is_a_closure_list_source() {
164
1
    assert!(is_list_source(&closure_list_source()));
165
1
    assert!(!is_list_source(&Expr::List(vec![Expr::symbol("LIST")])));
166
1
    assert!(!is_list_source(&Expr::T));
167
1
}
168

            
169
#[test]
170
1
fn a_carried_value_is_a_constant_unless_opaque() {
171
1
    assert_eq!(Carried::Constant(Expr::T).into_constant(), Some(Expr::T));
172
1
    assert_eq!(Carried::Opaque("list".to_string()).into_constant(), None);
173
1
}
174

            
175
#[test]
176
1
fn a_carried_closure_list_source_is_read_through_a_home() {
177
1
    let mut symbols = after_first_unit();
178
1
    symbols.carry("V", Carried::Constant(closure_list_source()));
179
1
    let (_, ty) = Compiler::new()
180
1
        .compile_eval_with_type(
181
1
            &Reader::parse("(funcall (car v) 4)").expect("parses"),
182
1
            &mut symbols,
183
1
        )
184
1
        .expect("compiles");
185
1
    assert_eq!(ty, Some(crate::ast::WasmType::I32));
186
1
}
187

            
188
#[test]
189
1
fn a_unit_storing_a_carried_closure_list_source_starts_it_lost() {
190
1
    let mut symbols = after_first_unit();
191
1
    symbols.carry("V", Carried::Constant(closure_list_source()));
192
1
    unit(&mut symbols, "(setf v (list 1)) (list 2)").expect("compiles");
193
1
    let slots = symbols.take_unit_slots();
194
1
    assert!(
195
1
        slots
196
1
            .iter()
197
1
            .any(|slot| slot.name == "V" && slot.cell.is_some())
198
    );
199
1
}