Interpreter - Implement PartialEq manually for values

This commit is contained in:
Emmanuel BENOîT 2023-01-02 17:39:31 +01:00
parent a4875ff876
commit 696a363ec6

View file

@ -1,21 +1,36 @@
use std::{cell::RefCell, rc::Rc};
use super::Callable;
/// A value being handled by the interpreter. /// A value being handled by the interpreter.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone)]
pub enum Value { pub enum Value {
Nil, Nil,
Boolean(bool), Boolean(bool),
String(String), String(String),
Number(f64), Number(f64),
Callable(Rc<RefCell<dyn Callable>>),
}
impl PartialEq for Value {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Nil, Self::Nil) => true,
(Self::Boolean(a), Self::Boolean(b)) => a == b,
(Self::String(a), Self::String(b)) => a == b,
(Self::Number(a), Self::Number(b)) => a == b,
_ => false,
}
}
} }
impl Value { impl Value {
/// Check whether a value is truthy or not. /// Check whether a value is truthy or not.
pub fn is_truthy(&self) -> bool { pub fn is_truthy(&self) -> bool {
if self == &Value::Nil { match self {
false &Self::Nil => false,
} else if let Value::Boolean(b) = self { &Self::Boolean(b) => b,
*b _ => true,
} else {
true
} }
} }
} }