1
//! P11: a constant-control DO is unrolled with its effects kept; its
2
//! variable is constant data no store may rebind; a loop whose control is
3
//! not constant, or that stores to its variable, runs at runtime.
4

            
5
use crate::compiler::behaviour_oracle::{Outcome, observe};
6
use crate::compiler::{CompileMode, Compiler};
7
use crate::reader::Reader;
8
use crate::runtime::SymbolTable;
9

            
10
8
fn outcome(source: &str) -> (Outcome, Vec<String>) {
11
8
    let (behaviours, _) = observe(source, CompileMode::Script, &[]);
12
8
    let first = behaviours.into_iter().next().expect("a world");
13
8
    let logs = first
14
8
        .output
15
8
        .iter()
16
2
        .map(|(_, line)| format!("{line:?}"))
17
        .collect();
18
8
    (first.outcome, logs)
19
8
}
20

            
21
7
fn value(source: &str) -> String {
22
7
    match outcome(source).0 {
23
7
        Outcome::Value(value) => value,
24
        other => panic!("{source}: {other:?}"),
25
    }
26
7
}
27

            
28
1
fn compile_error(source: &str) -> String {
29
1
    let program = Reader::parse(source).unwrap_or_else(|err| panic!("{err}"));
30
1
    let mut symbols = SymbolTable::with_builtins_for_wasm();
31
1
    match Compiler::new().compile(&program, &mut symbols) {
32
        Ok(_) => panic!("{source} compiled"),
33
1
        Err(err) => err.to_string(),
34
    }
35
1
}
36

            
37
#[test]
38
1
fn a_constant_control_do_keeps_its_global_stores() {
39
1
    assert!(
40
1
        value("(defvar *g* 0) (do ((i 0 (+ i 1))) ((> i 3)) (setf *g* (+ *g* i))) *g*")
41
1
            .contains("Index(6)")
42
    );
43
1
    assert!(
44
1
        value("(defvar *g* 0) (do* ((i 0 (+ i 1)) (j i i)) ((> i 3)) (setf *g* (+ *g* j))) *g*")
45
1
            .contains("Index(6)")
46
    );
47
1
}
48

            
49
#[test]
50
1
fn a_do_with_a_macro_body_unrolls_with_the_variable_as_data() {
51
1
    let (_, logs) = outcome(
52
1
        "(defmacro emit-k () (if (= k 0) '(print \"a\") '(print \"b\"))) (do ((k 0 (+ k 1))) ((> k 1)) (emit-k))",
53
1
    );
54
1
    assert_eq!(logs, vec!["Text(\"a\")", "Text(\"b\")"]);
55
1
}
56

            
57
#[test]
58
1
fn a_store_to_an_unrolled_loop_variable_is_refused() {
59
1
    let err = compile_error(
60
1
        "(defmacro bump (v) (list 'setf v 7)) (do ((k 0 (+ k 1))) ((>= k 2)) (bump k) (print k))",
61
1
    );
62
1
    assert!(err.contains("may not be assigned"), "{err}");
63
1
    let direct = value("(do ((k 0 (+ k 1))) ((>= k 2)) (setf k 5))");
64
1
    assert!(
65
1
        direct.contains("Nil"),
66
        "a body that stores to its variable runs at runtime: {direct}"
67
    );
68
1
}
69

            
70
#[test]
71
1
fn a_loop_whose_control_is_not_constant_runs_at_runtime() {
72
1
    assert!(
73
1
        value("(defvar n 3) (setf n 2) (do ((i 0 (+ i 1))) ((>= i n) i))").contains("Index(2)")
74
    );
75
1
}
76

            
77
#[test]
78
1
fn a_loop_too_long_to_unroll_within_the_budget_compiles_as_a_runtime_loop() {
79
1
    let source = "(defmacro inc () '(setf *g* (+ *g* 1))) (defvar *g* 0) (do ((i 0 (+ i 1))) ((> i 100000000)) (inc)) *g*";
80
1
    let program = Reader::parse(source).expect("parses");
81
1
    let mut symbols = SymbolTable::with_builtins_for_wasm();
82
1
    assert!(Compiler::new().compile(&program, &mut symbols).is_ok());
83
1
}
84

            
85
#[test]
86
1
fn a_do_init_store_is_kept() {
87
1
    assert!(value("(let ((x 0)) (do ((z (setf x 5) z)) (t)) x)").contains("Index(5)"));
88
1
}
89

            
90
#[test]
91
1
fn a_result_form_that_stores_to_a_loop_variable_runs_at_runtime() {
92
1
    assert!(value("(do ((i 0 (+ i 1))) ((= i 3) (setf i 8) i))").contains("Index(8)"));
93
1
    assert!(value("(do* ((i 0 (+ i 1))) ((>= i 2) (setf i (+ i 10)) i))").contains("Index(12)"));
94
1
}