use std::fmt;
use std::fmt::Formatter;
use std::rc::Rc;
use crate::environment::Env;
use parser::ast::BlockStatement;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
pub type EvalError = String;
pub type BuiltinFunc = fn(Vec<Rc<Object>>) -> Rc<Object>;
#[derive(Debug, Clone, PartialEq)]
pub enum Object {
Integer(i64),
Boolean(bool),
String(String),
Array(Vec<Rc<Object>>),
Hash(HashMap<Rc<Object>, Rc<Object>>),
Null,
ReturnValue(Rc<Object>),
Function(Vec<String>, BlockStatement, Env),
Builtin(BuiltinFunc),
Error(String),
}
impl fmt::Display for Object {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Object::Integer(i) => write!(f, "{}", i),
Object::Boolean(b) => write!(f, "{}", b),
Object::String(s) => write!(f, "{}", s),
Object::Null => write!(f, "null"),
Object::ReturnValue(expr) => write!(f, "{}", expr),
Object::Function(params, body, _env) => {
write!(f, "fn({}) {{ {} }}", params.join(", "), body)
},
Object::Builtin(_) => write!(f, "[builtin function]"),
Object::Error(e) => write!(f, "{}", e),
Object::Array(e) => write!(f, "[{}]", e.iter().map(|o|o.to_string()).collect::<Vec<String>>().join(", ")),
Object::Hash(map) => write!(f, "[{}]",
map
.iter()
.map(|(k, v)| format!("{}: {}", k, v))
.collect::<Vec<String>>()
.join(", ")
)
}
}
}
impl Object {
pub fn is_hashable(&self) -> bool {
match self {
Object::Integer(_) | Object::Boolean(_) | Object::String(_) => return true,
_ => return false,
}
}
}
impl Eq for Object {}
impl Hash for Object {
fn hash<H: Hasher>(&self, state: &mut H) {
match self {
Object::Integer(i) => i.hash(state),
Object::Boolean(b) => b.hash(state),
Object::String(s) => s.hash(state),
t => panic!(format!("can't hashable for {}" ,t))
}
}
}