deimos-lang/dvm-lib/src/vm/value.rs
2026-03-13 09:16:41 -05:00

69 lines
1.5 KiB
Rust

use crate::vm::object::Object;
use std::cell::RefCell;
use std::fmt::{Display, Formatter};
use std::rc::Rc;
#[derive(Clone, Debug, PartialEq)]
pub enum Value {
Int(i32),
Double(f64),
String(Rc<str>),
Null,
}
impl Default for Value {
fn default() -> Value {
Value::Null
}
}
impl Value {
pub fn unwrap_int(&self) -> i32 {
match self {
Value::Int(i) => *i,
_ => panic!("Attempt to unwrap Int; found {:?}", self),
}
}
pub fn unwrap_double(&self) -> f64 {
match self {
Value::Double(d) => *d,
_ => panic!("Attempt to unwrap Double; found {:?}", self),
}
}
pub fn unwrap_string(&self) -> &Rc<str> {
match self {
Value::String(s) => s,
_ => panic!("Attempt to unwrap String; found {:?}", self),
}
}
pub fn unwrap_object(&self) -> &Rc<RefCell<Object>> {
todo!()
}
pub fn unwrap_object_mut(&mut self) -> &mut Rc<RefCell<Object>> {
todo!()
}
}
impl Display for Value {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Value::Int(i) => {
write!(f, "{}", i)
}
Value::Double(d) => {
write!(f, "{}", d)
}
Value::String(s) => {
write!(f, "{}", s)
}
Value::Null => {
write!(f, "Null")
}
}
}
}