1
//! Constructors and conversions between a datum (a value) and code.
2

            
3
use crate::ast::{Expr, Fraction};
4
use crate::runtime::with_sufficient_stack;
5

            
6
impl Expr {
7
    /// The one constructor for a value that is a datum (a `'x` datum is the
8
    /// list `(QUOTE x)`): `nil`, `t`, the empty
9
    /// list, numbers, strings and keywords are the values themselves, anything else
10
    /// is wrapped in `Datum`. A list datum holds
11
    /// its `'x` members as the lists `(QUOTE x)`, at every depth.
12
    #[must_use]
13
27661025
    pub fn quoted(datum: Expr) -> Self {
14
27661025
        Self::wrapped(datum.into_datum_member(), Expr::Datum)
15
27661025
    }
16

            
17
    /// The code form `'x`, as the reader makes it: the expression is code,
18
    /// whose `'x` members stay quotes.
19
    #[must_use]
20
387525
    pub fn quoted_code(code: Expr) -> Self {
21
387525
        Self::wrapped(code, Expr::Quote)
22
387525
    }
23

            
24
28048550
    fn wrapped(expr: Expr, wrap: fn(Box<Expr>) -> Expr) -> Self {
25
28048550
        match expr {
26
31519
            Expr::Nil | Expr::T | Expr::Number(_) | Expr::String(_) | Expr::Keyword(_) => expr,
27
11887461
            Expr::List(items) if items.is_empty() => Expr::Nil,
28
27936864
            other => wrap(Box::new(other)),
29
        }
30
28048550
    }
31

            
32
    /// A computed value where code is wanted: a datum is the form it holds,
33
    /// the exact inverse of [`Expr::into_data`], any other value is itself.
34
    #[must_use]
35
1764135
    pub fn into_code(self) -> Self {
36
1764135
        match self {
37
1574696
            Expr::Datum(datum) => datum.into_form(),
38
189439
            other => other,
39
        }
40
1764135
    }
41

            
42
    /// Datum to code: a `(QUOTE x)` list is the quote node `'x`, at every
43
    /// depth; the inside of a quote stays datum.
44
45690192
    fn into_form(self) -> Self {
45
45690192
        with_sufficient_stack(|| match self {
46
17296223
            Expr::List(items) => match <[Expr; 2]>::try_from(items) {
47
2518767
                Ok([Expr::Symbol(head), inner]) if head == "QUOTE" => Expr::quoted_code(inner),
48
2535237
                Ok(pair) => Expr::List(pair.into_iter().map(Expr::into_form).collect()),
49
14676239
                Err(items) => Expr::List(items.into_iter().map(Expr::into_form).collect()),
50
            },
51
7
            Expr::Cons(car, cdr) => Expr::cons(car.into_form(), cdr.into_form()),
52
28393962
            other => other,
53
45690192
        })
54
45690192
    }
55

            
56
    /// The datum of a quoted list: a `'x` member is the list `(QUOTE x)`, at
57
    /// every depth. The outermost expression is not wrapped.
58
    #[must_use]
59
1645260389
    pub fn into_data(self) -> Self {
60
1645260389
        with_sufficient_stack(|| match self {
61
            Expr::List(items) => {
62
511062154
                Expr::List(items.into_iter().map(Expr::into_datum_member).collect())
63
            }
64
7973
            Expr::Cons(car, cdr) => Expr::cons(car.into_datum_member(), cdr.into_datum_member()),
65
1134190262
            other => other,
66
1645260389
        })
67
1645260389
    }
68

            
69
    /// A value as a member of list data: its `Datum` wrapper goes and a quote
70
    /// is the list `(QUOTE x)`, at every depth.
71
    #[must_use]
72
1662965543
    pub fn into_datum_member(self) -> Self {
73
1662965543
        match self {
74
17653277
            Expr::Datum(inner) => inner.into_datum_member(),
75
51879
            Expr::Quote(inner) => Expr::List(vec![
76
51879
                Expr::Symbol("QUOTE".to_string()),
77
51879
                inner.into_datum_member(),
78
51879
            ]),
79
1645260387
            other => other.into_data(),
80
        }
81
1662965543
    }
82

            
83
    /// The exact value of a numeric constant, whatever its dimension.
84
    #[must_use]
85
39936
    pub fn exact_number(&self) -> Option<Fraction> {
86
39936
        match self {
87
39728
            Expr::Number(n) => Some(n.value()),
88
208
            _ => None,
89
        }
90
39936
    }
91

            
92
    #[must_use]
93
10087536
    pub fn quoted_list(items: Vec<Expr>) -> Self {
94
10087536
        Expr::quoted(Expr::List(items))
95
10087536
    }
96
}