turtle/interpreter/values/
mod.rs

1use crate::Expression;
2
3use std::fmt;
4
5pub mod operator;
6pub use operator::Operator;
7
8pub mod symbol;
9pub use symbol::Symbol;
10
11pub mod keyword;
12pub use keyword::Keyword;
13
14pub mod function;
15pub use function::Function;
16
17#[derive(Debug, Clone, PartialEq, PartialOrd)]
18pub enum Value {
19    List(Vec<Expression>),
20    Number(f64),
21    Text(String),
22    Keyword(Keyword),
23    Symbol(Symbol),
24    Byte(u8),
25    True,
26
27    // Primitive (axiomatic) operators
28    Operator(Operator),
29
30    Lambda(Function),
31    Macro(Function),
32}
33
34impl Value {
35    pub fn as_type(&self) -> Self {
36        use Value::*;
37
38        Value::Keyword(crate::Keyword::new(match self {
39            List(_) => "list".to_string(),
40            Number(_) => "number".to_string(),
41            Text(_) => "text".to_string(),
42            Keyword(_) => "keyword".to_string(),
43            Symbol(_) => "symbol".to_string(),
44            Operator(_) => "operator".to_string(),
45            Byte(_) => "byte".to_string(),
46            Lambda { .. } => "lambda".to_string(),
47            Macro { .. } => "macro".to_string(),
48            _ => "unknown".to_string(),
49        }))
50    }
51}
52
53impl<'a> fmt::Display for Value {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        use Value::*;
56
57        match &self {
58            List(vals) => match vals.len() {
59                0 => write!(f, "nil"),
60                _ => write!(
61                    f,
62                    "({})",
63                    vals.iter()
64                        .map(|v| format!("{}", v))
65                        .collect::<Vec<String>>()
66                        .join(" ")
67                ),
68            },
69            Number(val) => write!(f, "{}", val),
70            Text(val) => write!(f, "{}", val),
71            Symbol(val) => write!(f, "{}", val),
72            Keyword(val) => write!(f, "{}", val),
73            Byte(val) => write!(f, "b{}", val),
74            True => write!(f, "true"),
75            Lambda(function) | Macro(function) => write!(
76                f,
77                "<{} {}{}{} -> {}>",
78                match self {
79                    Lambda(_) => "lambda",
80                    Macro(_) => "macro",
81                    _ => unreachable!(),
82                },
83                match function.collapse_input {
84                    true => "",
85                    false => "(",
86                },
87                function
88                    .params
89                    .iter()
90                    .map(|x| format!("{}", x))
91                    .collect::<Vec<String>>()
92                    .join(" "),
93                match function.collapse_input {
94                    true => "",
95                    false => ")",
96                },
97                function
98                    .expressions
99                    .iter()
100                    .map(|x| format!("{}", x))
101                    .collect::<Vec<String>>()
102                    .join(" ")
103            ),
104            _ => write!(f, "<{}>", format!("{:?}", self).to_lowercase()),
105        }
106    }
107}