stak_native/
type_check.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
use stak_vm::{Error, Memory, PrimitiveSet, Type};

/// A type check primitive.
pub enum TypeCheckPrimitive {
    /// A null type check.
    Null,
    /// A pair type check.
    Pair,
}

impl TypeCheckPrimitive {
    const NULL: usize = Self::Null as _;
    const PAIR: usize = Self::Pair as _;
}

/// A type check primitive set.
#[derive(Debug, Default)]
pub struct TypeCheckPrimitiveSet {}

impl TypeCheckPrimitiveSet {
    /// Creates a primitive set.
    pub fn new() -> Self {
        Self::default()
    }
}

impl PrimitiveSet for TypeCheckPrimitiveSet {
    type Error = Error;

    fn operate(&mut self, memory: &mut Memory, primitive: usize) -> Result<(), Self::Error> {
        match primitive {
            TypeCheckPrimitive::NULL => memory.operate_top(|memory, value| {
                memory.boolean(value == memory.null().into()).into()
            })?,
            TypeCheckPrimitive::PAIR => memory.operate_top(|memory, value| {
                memory
                    .boolean(
                        value
                            .to_cons()
                            .map(|cons| memory.cdr(cons).tag() == Type::Pair as _)
                            .unwrap_or_default(),
                    )
                    .into()
            })?,
            _ => return Err(Error::IllegalPrimitive),
        }

        Ok(())
    }
}