stak_vm/
value.rs

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use crate::{
    cons::{Cons, Tag},
    number::Number,
};
use cfg_exif::feature;
use core::fmt::{self, Display, Formatter};

/// A value.
#[derive(Copy, Clone, Debug)]
pub struct Value(u64);

/// A typed value.
#[derive(Copy, Clone, Debug, PartialEq)]
#[cfg_attr(not(feature = "float"), derive(Eq))]
pub enum TypedValue {
    Cons(Cons),
    Number(Number),
}

impl Value {
    /// Converts a value to a cons.
    pub fn to_cons(self) -> Option<Cons> {
        if let TypedValue::Cons(cons) = self.to_typed() {
            Some(cons)
        } else {
            None
        }
    }

    /// Converts a value to a number.
    pub fn to_number(self) -> Option<Number> {
        if let TypedValue::Number(number) = self.to_typed() {
            Some(number)
        } else {
            None
        }
    }

    /// Converts a value to a typed value.
    pub fn to_typed(self) -> TypedValue {
        if self.is_cons() {
            TypedValue::Cons(self.assume_cons())
        } else {
            TypedValue::Number(self.assume_number())
        }
    }

    /// Converts a value to a cons assuming its type.
    pub fn assume_cons(self) -> Cons {
        debug_assert!(self.is_cons());

        Cons::from_raw(self.0)
    }

    /// Converts a value to a number assuming its type.
    pub fn assume_number(self) -> Number {
        debug_assert!(self.is_number());

        Number::from_raw(self.0)
    }

    /// Checks if it is a cons.
    pub const fn is_cons(&self) -> bool {
        feature!(if ("float") {
            nonbox::f64::u64::unbox_unsigned(self.0).is_some()
        } else {
            self.0 & 1 == 0
        })
    }

    /// Checks if it is a number.
    pub const fn is_number(&self) -> bool {
        !self.is_cons()
    }

    /// Returns a tag.
    pub fn tag(self) -> Tag {
        self.to_cons().map_or(0, Cons::tag)
    }

    /// Sets a tag.
    pub fn set_tag(self, tag: Tag) -> Self {
        self.to_cons().map_or(self, |cons| cons.set_tag(tag).into())
    }
}

impl Default for Value {
    fn default() -> Self {
        Number::default().into()
    }
}

impl PartialEq for Value {
    fn eq(&self, other: &Self) -> bool {
        self.is_cons() && self.to_cons() == other.to_cons()
            || self.is_number() && self.to_number() == other.to_number()
    }
}

impl Eq for Value {}

impl From<Cons> for Value {
    fn from(cons: Cons) -> Self {
        Self(cons.to_raw())
    }
}

impl From<Number> for Value {
    fn from(number: Number) -> Self {
        Self(number.to_raw())
    }
}

impl Display for Value {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        match self.to_typed() {
            TypedValue::Cons(cons) => write!(formatter, "{cons}"),
            TypedValue::Number(number) => write!(formatter, "{number}"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{cons::NEVER, Type};

    #[test]
    fn convert_cons() {
        let cons = Cons::new(42);

        assert_eq!(Value::from(cons).to_cons().unwrap(), cons);
    }

    #[test]
    fn convert_tagged_cons() {
        const TAG: Tag = 0b111;

        let cons = Cons::new(42).set_tag(TAG);
        let converted = Value::from(cons).to_cons().unwrap();

        assert_eq!(converted, cons);
        assert_eq!(converted.tag(), TAG);
    }

    #[test]
    fn convert_number() {
        let number = Number::from_i64(42);

        assert_eq!(Value::from(number).to_number().unwrap(), number);
    }

    #[test]
    fn convert_moved() {
        assert_eq!(Value::from(NEVER).to_cons().unwrap(), NEVER);
    }

    #[test]
    fn get_tag_from_number() {
        let tag = Value::from(Number::from_i64(42)).tag();

        assert_eq!(tag, Default::default());
        assert_eq!(tag, Type::default() as Tag);
    }

    #[test]
    fn set_tag_to_number() {
        let value = Value::from(Number::from_i64(42)).set_tag(0b111);

        assert_eq!(value.tag(), Default::default());
        assert_eq!(value.to_number(), Some(Number::from_i64(42)));
    }
}