use std::{
fmt::{
Debug,
Display,
Formatter,
Result,
},
f64,
rc::Rc,
cell::RefCell,
};
use crate::common::{
lambda::Lambda,
closure::Closure,
};
#[derive(Clone, PartialEq)]
pub enum Data {
Frame,
Heaped(Rc<RefCell<Data>>),
Real(f64),
Boolean(bool),
String(String),
Lambda(Lambda),
Closure(Closure),
Label(String, Box<Data>),
Unit, }
impl Eq for Data {}
impl Display for Data {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
match self {
Data::Frame => unreachable!("Can not display stack frame"),
Data::Heaped(_) => unreachable!("Can not display heaped data"),
Data::Real(n) => write!(f, "Real {}", n),
Data::Boolean(b) => write!(f, "Boolean {}", if *b { "true" } else { "false" }),
Data::String(s) => write!(f, "{}", s),
Data::Lambda(_) => unreachable!("Can not display naked functions"),
Data::Closure(_) => write!(f, "Function"),
Data::Label(n, v) => write!(f, "{} {}", n, v),
Data::Unit => write!(f, "()"),
}
}
}
impl Debug for Data {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
match self {
Data::Frame => write!(f, "Frame"),
Data::Heaped(h) => write!(f, "Heaped({:?})", h.borrow()),
Data::Real(n) => write!(f, "Real({:?})", n),
Data::Boolean(b) => write!(f, "Boolean({:?})", b),
Data::String(s) => write!(f, "String({:?})", s),
Data::Lambda(_) => write!(f, "Function(...)"),
Data::Closure(_) => write!(f, "Closure(...)"),
Data::Label(n, v) => write!(f, "Label({}, {:?})", n, v),
Data::Unit => write!(f, "Unit"),
}
}
}