reqlang_expr/
value.rs

1//! The core value type used in the virtual machine.
2
3use std::{fmt::Display, rc::Rc};
4
5use crate::{builtins::BuiltinFn, types::Type};
6
7#[derive(Debug, Clone, PartialEq)]
8pub enum Value {
9    String(String),
10    Fn(Rc<BuiltinFn>),
11    Bool(bool),
12    Type(Box<Type>),
13}
14
15impl Value {
16    pub fn get_type(&self) -> Type {
17        self.clone().into()
18    }
19
20    pub fn get_string(&self) -> &str {
21        match self {
22            Value::String(s) => s.as_str(),
23            _ => panic!("Value is not a string"),
24        }
25    }
26
27    pub fn get_func(&self) -> Rc<BuiltinFn> {
28        match self {
29            Value::Fn(f) => f.clone(),
30            _ => panic!("Value is not a function"),
31        }
32    }
33
34    pub fn get_bool(&self) -> bool {
35        match self {
36            Value::Bool(s) => *s,
37            _ => panic!("Value is not a string"),
38        }
39    }
40}
41
42impl Display for Value {
43    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
44        match self {
45            Value::String(string) => write!(f, "`{}`", string),
46            Value::Fn(builtin) => write!(f, "{builtin:?}"),
47            Value::Bool(value) => write!(f, "{}", value),
48            Value::Type(ty) => write!(f, "{}", ty),
49        }
50    }
51}