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
use std::fmt;
type VecObj = Vec<Object>;
enum_type! {
/// The possible types in an Avid program.
Object {
/// A number.
Num(isize),
/// A string
String(String),
/// A boolean
Bool(bool),
/// A list of objects
List(VecObj)
}
}
impl Clone for Object {
fn clone(&self) -> Self {
match self {
Self::Num(i) => Self::Num(*i),
Self::String(s) => Self::String(s.clone()),
Self::Bool(b) => Self::Bool(*b),
Self::List(l) => Self::List(l.clone()),
}
}
}
impl Object {
/// Whether the object evaluates to "true" in a condition.
///
/// For now, the only instances that evaluate to `false` are `false`, `""` and
/// `0`.
pub fn truthy(&self) -> bool {
let cond = match self {
Object::Num(n) => *n == 0,
Object::String(s) => s.as_str() == "",
Object::Bool(b) => !*b,
Object::List(l) => l.is_empty(),
};
!cond
}
}
impl fmt::Display for Object {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Num(n) => write!(f, "{n}"),
Self::String(s) => write!(f, "{s}"),
Self::Bool(b) => write!(f, "{b}"),
Self::List(l) => {
write!(f, "[")?;
for (idx, obj) in l.iter().enumerate() {
if idx > 0 {
write!(f, ", ")?;
}
write!(f, "{obj}")?;
}
write!(f, "]")
}
}
}
}