stak_vm/
cons.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
use crate::{value::Value, Error};
use cfg_exif::feature;
use core::fmt::{self, Display, Formatter};

/// A tag.
pub type Tag = u16;

/// An unreachable cons. In other words, it is a "null" pointer but not `null`
/// in Scheme.
///
/// This value means:
///
/// - in car, its cons is moved already on garbage collection.
/// - in cdr, nothing.
pub const NEVER: Cons = Cons::new(u64::MAX);

const TAG_SIZE: usize = Tag::BITS as usize;
const TAG_MASK: u64 = Tag::MAX as u64;

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

impl Cons {
    /// Creates a cons from a memory address on heap.
    pub const fn new(index: u64) -> Self {
        Self::r#box(index << TAG_SIZE)
    }

    /// Returns a memory address on heap.
    pub const fn index(self) -> usize {
        (self.unbox() >> TAG_SIZE) as _
    }

    /// Returns a tag.
    pub const fn tag(self) -> Tag {
        (self.unbox() & TAG_MASK) as _
    }

    /// Sets a tag.
    pub const fn set_tag(self, tag: Tag) -> Self {
        Self::r#box(self.unbox() & !TAG_MASK | (tag as u64 & TAG_MASK))
    }

    const fn r#box(value: u64) -> Self {
        Self(feature!(if ("float") {
            nonbox::f64::u64::box_unsigned(value)
        } else {
            value << 1
        }))
    }

    const fn unbox(self) -> u64 {
        feature!(if ("float") {
            nonbox::f64::u64::unbox_unsigned(self.0).unwrap()
        } else {
            self.0 >> 1
        })
    }

    pub(crate) const fn from_raw(raw: u64) -> Self {
        Self(raw)
    }

    pub(crate) const fn to_raw(self) -> u64 {
        self.0
    }
}

impl PartialEq for Cons {
    fn eq(&self, other: &Self) -> bool {
        self.index() == other.index()
    }
}

impl Eq for Cons {}

impl TryFrom<Value> for Cons {
    type Error = Error;

    fn try_from(value: Value) -> Result<Self, Self::Error> {
        value.to_cons().ok_or(Error::ConsExpected)
    }
}

impl Display for Cons {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        if self == &NEVER {
            write!(formatter, "!")?;
        } else {
            write!(formatter, "c{:x}", self.index())?;
        }

        write!(formatter, ":{}", self.tag())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

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

        assert_eq!(cons.index(), 42);
        assert_eq!(cons.tag(), 0);

        let cons = cons.set_tag(1);

        assert_eq!(cons.index(), 42);
        assert_eq!(cons.tag(), 1);

        let cons = cons.set_tag(3);

        assert_eq!(cons.index(), 42);
        assert_eq!(cons.tag(), 3);
    }

    #[test]
    fn reset_tag() {
        assert_eq!(Cons::new(42).set_tag(2).set_tag(1).tag(), 1);
    }

    #[test]
    fn set_too_large_tag() {
        let cons = Cons::new(0).set_tag(Tag::MAX);

        assert_eq!(cons.index(), 0);
        assert_eq!(cons.tag(), TAG_MASK as Tag);
    }
}