about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/bytecode/value.rs
blob: 4170efadf8fed906dab74fe607b675abb53dc4c0 (plain) (blame)
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
use super::interner::InternedStr;

#[derive(Clone, Debug, PartialEq)]
pub enum Value {
    Nil,
    Bool(bool),
    Number(f64),
    String(LoxString),
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum LoxString {
    Heap(String),
    Interned(InternedStr),
}

impl From<String> for LoxString {
    fn from(s: String) -> Self {
        LoxString::Heap(s)
    }
}

impl From<InternedStr> for LoxString {
    fn from(s: InternedStr) -> Self {
        LoxString::Interned(s)
    }
}

impl Value {
    pub fn is_falsey(&self) -> bool {
        match self {
            Value::Nil => true,
            Value::Bool(false) => true,
            _ => false,
        }
    }
}