Skip to main content

nomiscript/
ast_datum.rs

1//! Constructors and conversions between a datum (a value) and code.
2
3use crate::ast::{Expr, Fraction};
4use crate::runtime::with_sufficient_stack;
5
6impl 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    pub fn quoted(datum: Expr) -> Self {
14        Self::wrapped(datum.into_datum_member(), Expr::Datum)
15    }
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    pub fn quoted_code(code: Expr) -> Self {
21        Self::wrapped(code, Expr::Quote)
22    }
23
24    fn wrapped(expr: Expr, wrap: fn(Box<Expr>) -> Expr) -> Self {
25        match expr {
26            Expr::Nil | Expr::T | Expr::Number(_) | Expr::String(_) | Expr::Keyword(_) => expr,
27            Expr::List(items) if items.is_empty() => Expr::Nil,
28            other => wrap(Box::new(other)),
29        }
30    }
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    pub fn into_code(self) -> Self {
36        match self {
37            Expr::Datum(datum) => datum.into_form(),
38            other => other,
39        }
40    }
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    fn into_form(self) -> Self {
45        with_sufficient_stack(|| match self {
46            Expr::List(items) => match <[Expr; 2]>::try_from(items) {
47                Ok([Expr::Symbol(head), inner]) if head == "QUOTE" => Expr::quoted_code(inner),
48                Ok(pair) => Expr::List(pair.into_iter().map(Expr::into_form).collect()),
49                Err(items) => Expr::List(items.into_iter().map(Expr::into_form).collect()),
50            },
51            Expr::Cons(car, cdr) => Expr::cons(car.into_form(), cdr.into_form()),
52            other => other,
53        })
54    }
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    pub fn into_data(self) -> Self {
60        with_sufficient_stack(|| match self {
61            Expr::List(items) => {
62                Expr::List(items.into_iter().map(Expr::into_datum_member).collect())
63            }
64            Expr::Cons(car, cdr) => Expr::cons(car.into_datum_member(), cdr.into_datum_member()),
65            other => other,
66        })
67    }
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    pub fn into_datum_member(self) -> Self {
73        match self {
74            Expr::Datum(inner) => inner.into_datum_member(),
75            Expr::Quote(inner) => Expr::List(vec![
76                Expr::Symbol("QUOTE".to_string()),
77                inner.into_datum_member(),
78            ]),
79            other => other.into_data(),
80        }
81    }
82
83    /// The exact value of a numeric constant, whatever its dimension.
84    #[must_use]
85    pub fn exact_number(&self) -> Option<Fraction> {
86        match self {
87            Expr::Number(n) => Some(n.value()),
88            _ => None,
89        }
90    }
91
92    #[must_use]
93    pub fn quoted_list(items: Vec<Expr>) -> Self {
94        Expr::quoted(Expr::List(items))
95    }
96}