sphinx/runtime/types/
boolean.rs

1use crate::language::{IntType};
2use crate::runtime::Variant;
3use crate::runtime::strings::{StringValue, static_symbol};
4use crate::runtime::types::{Type, MetaObject};
5use crate::runtime::errors::{ExecResult};
6
7// Booleans
8impl MetaObject for bool {
9    fn type_tag(&self) -> Type { Type::Boolean }
10    
11    fn as_bool(&self) -> ExecResult<bool> { Ok(*self) }
12    
13    fn as_bits(&self) -> Option<ExecResult<IntType>> {
14        if *self { Some(Ok(!0)) } // all 1s
15        else { Some(Ok(0)) } // all 0s
16    }
17    
18    fn op_inv(&self) -> Option<ExecResult<Variant>> { 
19        Some(Ok(Variant::from(!(*self)))) 
20    }
21    
22    fn op_and(&self, rhs: &Variant) -> Option<ExecResult<Variant>> {
23        match rhs {
24            Variant::BoolFalse => Some(Ok(Variant::from(false))),
25            Variant::BoolTrue => Some(Ok(Variant::from(*self))),
26            _ => None,
27        }
28    }
29    fn op_rand(&self, lhs: &Variant) -> Option<ExecResult<Variant>> {
30        self.op_and(lhs)
31    }
32    
33    fn op_xor(&self, rhs: &Variant) -> Option<ExecResult<Variant>> {
34        match rhs {
35            Variant::BoolFalse => Some(Ok(Variant::from(*self))),
36            Variant::BoolTrue => Some(Ok(Variant::from(!(*self)))),
37            _ => None,
38        }
39    }
40    fn op_rxor(&self, lhs: &Variant) -> Option<ExecResult<Variant>> {
41        self.op_xor(lhs)
42    }
43    
44    fn op_or(&self, rhs: &Variant) -> Option<ExecResult<Variant>> {
45        match rhs {
46            Variant::BoolFalse => Some(Ok(Variant::from(*self))),
47            Variant::BoolTrue => Some(Ok(Variant::from(true))),
48            _ => None,
49        }
50    }
51    fn op_ror(&self, lhs: &Variant) -> Option<ExecResult<Variant>> {
52        self.op_or(lhs)
53    }
54    
55    fn cmp_eq(&self, other: &Variant) -> Option<ExecResult<bool>> {
56        match other {
57            Variant::BoolFalse => Some(Ok(!(*self))),
58            Variant::BoolTrue => Some(Ok(*self)),
59            _ => None,
60        }
61    }
62    
63    fn fmt_repr(&self) -> ExecResult<StringValue> {
64        match self {
65            true => Ok(static_symbol!("true").into()),
66            false => Ok(static_symbol!("false").into()),
67        }
68    }
69}