♻️ refactor impl Display for Value

main
_ 2023-09-26 15:57:23 -05:00
parent 72b2d6e656
commit 2bd1ff3d59
2 changed files with 21 additions and 10 deletions

View File

@ -245,16 +245,7 @@ impl State {
assert_eq! (*c, 1);
let value = r.get (a + 1).unwrap ();
match value {
Value::Nil => println! ("nil"),
Value::Boolean (false) => println! ("false"),
Value::Boolean (true) => println! ("true"),
Value::Float (x) => println! ("{:?}", x),
Value::Integer (x) => println! ("{}", x),
Value::String (s) => println! ("{}", s),
Value::Table (t) => println! ("table: {:?}", std::rc::Rc::as_ptr (t)),
_ => unimplemented! (),
};
println! ("{}", value);
r [a] = r [a + 1].take ();
},

View File

@ -4,6 +4,7 @@ use std::{
PartialEq,
},
collections::HashMap,
fmt,
rc::Rc,
};
@ -41,6 +42,25 @@ impl Default for Value {
}
}
impl fmt::Display for Value {
fn fmt (&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Value::Nil => write! (f, "nil"),
Value::Boolean (false) => write! (f, "false"),
Value::Boolean (true) => write! (f, "true"),
Value::Float (x) => write! (f, "{:?}", x),
Value::Integer (x) => write! (f, "{}", x),
Value::String (s) => write! (f, "{}", s),
Value::Table (t) => write! (f, "table: {:?}", std::rc::Rc::as_ptr (t)),
Value::BogusArg (_) => write! (f, "BogusArg"),
Value::BogusClosure (_) => write! (f, "BogusClosure"),
Value::BogusEnv (_) => write! (f, "BogusEnv"),
Value::BogusPrint => write! (f, "BogusPrint"),
}
}
}
impl From <String> for Value {
fn from (x: String) -> Self {
Self::String (x.into ())