1
//! `QUOTE`'s evaluation is the reader's `'x`: one `Expr::Quote` value.
2

            
3
use crate::ast::Expr;
4
use crate::compiler::expr::eval_value;
5
use crate::error::Error;
6
use crate::reader::Reader;
7
use crate::runtime::SymbolTable;
8

            
9
31
fn form(source: &str) -> Expr {
10
31
    Reader::parse(source)
11
31
        .ok()
12
31
        .and_then(|program| program.exprs.into_iter().next())
13
31
        .unwrap_or(Expr::Nil)
14
31
}
15

            
16
31
fn eval(source: &str) -> Result<Expr, Error> {
17
31
    eval_value(&mut SymbolTable::with_builtins_for_wasm(), &form(source))
18
31
}
19

            
20
#[test]
21
1
fn a_quote_form_evaluates_like_the_reader_sugar() {
22
1
    assert_eq!(eval("(quote b)").ok(), eval("'b").ok());
23
1
    assert_eq!(eval("(quote (1 2))").ok(), eval("'(1 2)").ok());
24
1
}
25

            
26
#[test]
27
1
fn a_quote_form_evaluates_to_a_quote_value() {
28
1
    assert!(matches!(eval("(quote b)"), Ok(Expr::Datum(_))));
29
1
}
30

            
31
#[test]
32
1
fn a_quoted_number_or_string_is_the_atom() {
33
1
    for atom in ["1", "1/2", "\"a\""] {
34
3
        let bare = eval(atom).ok();
35
3
        assert_eq!(
36
3
            eval(&format!("(quote {atom})")).ok(),
37
3
            bare,
38
            "(quote {atom})"
39
        );
40
3
        assert_eq!(eval(&format!("'{atom}")).ok(), bare, "'{atom}");
41
3
        assert!(!matches!(bare, Some(Expr::Datum(_))), "{atom}");
42
    }
43
1
}
44

            
45
#[test]
46
1
fn a_quoted_keyword_is_the_keyword() {
47
1
    assert_eq!(eval("(quote :k)").ok(), Some(Expr::Keyword("K".into())));
48
1
    assert_eq!(eval("(quote :k)").ok(), eval("':k").ok());
49
1
}
50

            
51
#[test]
52
1
fn a_quote_form_with_two_operands_is_an_arity_error() {
53
1
    assert!(matches!(eval("(quote a b)"), Err(Error::Arity { .. })));
54
1
}
55

            
56
#[test]
57
1
fn a_quoted_constant_is_the_constant_the_reader_folds() {
58
1
    for datum in ["nil", "t", "()", "b", "(1 2)"] {
59
5
        assert_eq!(
60
5
            eval(&format!("(quote {datum})")).ok(),
61
5
            eval(&format!("'{datum}")).ok(),
62
            "(quote {datum})"
63
        );
64
    }
65
1
    assert_eq!(eval("(quote nil)").ok(), Some(Expr::Nil));
66
1
    assert_eq!(eval("(quote ())").ok(), Some(Expr::Nil));
67
1
    assert_eq!(eval("(quote t)").ok(), Some(Expr::T));
68
1
}