1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use std::{
fmt::{
Debug,
Display,
Formatter,
Result,
},
f64,
rc::Rc,
cell::RefCell,
};
use crate::common::{
lambda::Lambda,
closure::Closure,
};
#[derive(Clone, PartialEq)]
pub enum Data {
Heaped(Rc<RefCell<Data>>),
Real(f64),
Boolean(bool),
String(String),
Lambda(Box<Lambda>),
Closure(Box<Closure>),
Kind(String),
Label(Box<String>, Box<Data>),
Unit,
Tuple(Vec<Data>),
}
impl Eq for Data {}
impl Display for Data {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
match self {
Data::Heaped(_) => unreachable!("Can not display heaped data"),
Data::Real(n) => write!(f, "{}", n),
Data::Boolean(b) => write!(f, "{}", if *b { "true" } else { "false" }),
Data::String(s) => write!(f, "{}", s),
Data::Lambda(_) => unreachable!("Can not display naked functions"),
Data::Closure(c) => write!(f, "Function ~ {}", c.id),
Data::Kind(_) => unreachable!("Can not display naked labels"),
Data::Label(n, v) => write!(f, "{} {}", n, v),
Data::Unit => write!(f, "()"),
Data::Tuple(t) => write!(f, "({})", t.iter()
.map(|i| format!("{}", i))
.collect::<Vec<String>>()
.join(", ")
),
}
}
}
impl Debug for Data {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
match self {
Data::Heaped(h) => write!(f, "Heaped({:?})", h.borrow()),
Data::Real(n) => write!(f, "Real({:?})", n),
Data::Boolean(b) => write!(f, "Boolean({:?})", b),
Data::String(s) => write!(f, "String({:?})", s),
Data::Lambda(_) => write!(f, "Function(...)"),
Data::Closure(c) => write!(f, "Closure({})", c.id),
Data::Kind(n) => write!(f, "Kind({})", n),
Data::Label(n, v) => write!(f, "Label({}, {:?})", n, v),
Data::Unit => write!(f, "Unit"),
Data::Tuple(t) => write!(f, "Tuple({:?})", t),
}
}
}