Use .pretty() instead of defining Display as pretty version

This commit is contained in:
Andrew Morris
2023-05-26 12:21:02 +10:00
parent ea42e94d5d
commit 3cfe1a6527
17 changed files with 49 additions and 39 deletions

View File

@@ -82,7 +82,7 @@ impl ValTrait for ArrayBuiltin {
LoadFunctionResult::NotAFunction
}
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn pretty_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "\x1b[36m[Array]\x1b[39m")
}

View File

@@ -74,7 +74,7 @@ impl ValTrait for BooleanBuiltin {
LoadFunctionResult::NotAFunction
}
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn pretty_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "\x1b[36m[Boolean]\x1b[39m")
}

View File

@@ -77,7 +77,7 @@ impl ValTrait for DebugBuiltin {
LoadFunctionResult::NotAFunction
}
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn pretty_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "\x1b[36m[Debug]\x1b[39m")
}
@@ -89,7 +89,7 @@ impl ValTrait for DebugBuiltin {
static LOG: NativeFunction = NativeFunction {
fn_: |_this: ThisWrapper, params: Vec<Val>| -> Result<Val, Val> {
for p in params {
println!("Debug.log: {}", p);
println!("Debug.log: {}", p.pretty());
}
Ok(Val::Undefined)

View File

@@ -88,7 +88,7 @@ impl ValTrait for ErrorBuiltin {
LoadFunctionResult::NotAFunction
}
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn pretty_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "\x1b[36m[Error]\x1b[39m")
}
@@ -153,6 +153,6 @@ static SET_MESSAGE: NativeFunction = NativeFunction {
static ERROR_TO_STRING: NativeFunction = NativeFunction {
fn_: |this: ThisWrapper, _params: Vec<Val>| -> Result<Val, Val> {
let message = op_sub(this.get().clone(), "message".to_val())?;
Ok(format!("Error({})", message).to_val()) // TODO: Fixes needed here (and other errors)
Ok(format!("Error({})", message.val_to_string()).to_val()) // TODO: Fixes needed here (and other errors)
},
};

View File

@@ -122,7 +122,7 @@ impl ValTrait for MathBuiltin {
LoadFunctionResult::NotAFunction
}
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn pretty_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "\x1b[36m[Math]\x1b[39m")
}

View File

@@ -92,7 +92,7 @@ impl ValTrait for NumberBuiltin {
LoadFunctionResult::NotAFunction
}
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn pretty_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "\x1b[36m[Number]\x1b[39m")
}

View File

@@ -80,7 +80,7 @@ impl ValTrait for RangeErrorBuiltin {
LoadFunctionResult::NotAFunction
}
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn pretty_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "\x1b[36m[RangeError]\x1b[39m")
}
@@ -135,7 +135,7 @@ static SET_MESSAGE: NativeFunction = NativeFunction {
static RANGE_ERROR_TO_STRING: NativeFunction = NativeFunction {
fn_: |this: ThisWrapper, _params: Vec<Val>| -> Result<Val, Val> {
let message = op_sub(this.get().clone(), "message".to_val())?;
Ok(format!("RangeError({})", message).to_val())
Ok(format!("RangeError({})", message.val_to_string()).to_val())
},
};

View File

@@ -85,7 +85,7 @@ impl ValTrait for StringBuiltin {
LoadFunctionResult::NotAFunction
}
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn pretty_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "\x1b[36m[String]\x1b[39m")
}

View File

@@ -77,7 +77,7 @@ impl ValTrait for SymbolBuiltin {
LoadFunctionResult::NotAFunction
}
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn pretty_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "\x1b[36m[Symbol]\x1b[39m")
}

View File

@@ -86,7 +86,7 @@ impl ValTrait for TypeErrorBuiltin {
LoadFunctionResult::NotAFunction
}
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn pretty_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "\x1b[36m[TypeError]\x1b[39m")
}
@@ -124,7 +124,7 @@ static SET_MESSAGE: NativeFunction = NativeFunction {
static TYPE_ERROR_TO_STRING: NativeFunction = NativeFunction {
fn_: |this: ThisWrapper, _params: Vec<Val>| -> Result<Val, Val> {
let message = op_sub(this.get().clone(), "message".to_val())?;
Ok(format!("TypeError({})", message).to_val())
Ok(format!("TypeError({})", message.val_to_string()).to_val())
},
};

View File

@@ -73,7 +73,7 @@ impl ValTrait for NativeFrameFunction {
LoadFunctionResult::NotAFunction
}
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn pretty_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "\x1b[36m[Function]\x1b[39m")
}

View File

@@ -95,7 +95,7 @@ impl ValTrait for NativeFunction {
LoadFunctionResult::NotAFunction
}
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn pretty_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "\x1b[36m[Function]\x1b[39m")
}

View File

@@ -152,7 +152,7 @@ impl ValTrait for ArrayPrototype {
LoadFunctionResult::NotAFunction
}
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn pretty_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "\x1b[36m[Array Prototype]\x1b[39m")
}

View File

@@ -154,8 +154,8 @@ impl ValTrait for VsPointer {
self.resolve().next()
}
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.resolve().fmt(f)
fn pretty_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.resolve().pretty_fmt(f)
}
fn codify(&self) -> String {

View File

@@ -109,14 +109,14 @@ pub trait ValTrait {
fn next(&mut self) -> LoadFunctionResult;
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result;
fn pretty_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result;
fn codify(&self) -> String;
}
impl fmt::Debug for dyn ValTrait {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "(dyn ValTrait)(")?;
self.fmt(f)?;
self.pretty_fmt(f)?;
write!(f, ")")
}
}
@@ -408,8 +408,8 @@ impl ValTrait for Val {
}
}
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self, f)
fn pretty_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(&self.pretty(), f)
}
fn codify(&self) -> String {
@@ -554,17 +554,27 @@ impl ToVal for Vec<Val> {
}
}
impl std::fmt::Display for Val {
pub struct PrettyVal<'a> {
val: &'a Val,
}
impl<'a> Val {
pub fn pretty(&'a self) -> PrettyVal<'a> {
PrettyVal { val: self }
}
}
impl<'a> std::fmt::Display for PrettyVal<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
match self.val {
Val::Void => write!(f, "void"),
Val::Undefined => write!(f, "\x1b[90mundefined\x1b[39m"),
Val::Null => write!(f, "\x1b[1mnull\x1b[22m"),
Val::Bool(_) => write!(f, "\x1b[33m{}\x1b[39m", self.val_to_string()),
Val::Number(_) => write!(f, "\x1b[33m{}\x1b[39m", self.val_to_string()),
Val::BigInt(_) => write!(f, "\x1b[33m{}n\x1b[39m", self.val_to_string()),
Val::Symbol(_) => write!(f, "\x1b[32m{}\x1b[39m", self.codify()),
Val::String(_) => write!(f, "\x1b[32m{}\x1b[39m", self.codify()),
Val::Bool(_) => write!(f, "\x1b[33m{}\x1b[39m", self.val.val_to_string()),
Val::Number(_) => write!(f, "\x1b[33m{}\x1b[39m", self.val.val_to_string()),
Val::BigInt(_) => write!(f, "\x1b[33m{}n\x1b[39m", self.val.val_to_string()),
Val::Symbol(_) => write!(f, "\x1b[32m{}\x1b[39m", self.val.codify()),
Val::String(_) => write!(f, "\x1b[32m{}\x1b[39m", self.val.codify()),
Val::Array(array) => {
if array.elements.len() == 0 {
return write!(f, "[]");
@@ -581,7 +591,7 @@ impl std::fmt::Display for Val {
write!(f, ", ").expect("Failed to write");
}
write!(f, "{}", elem).expect("Failed to write");
write!(f, "{}", elem.pretty()).expect("Failed to write");
}
write!(f, " ]")
@@ -615,10 +625,10 @@ impl std::fmt::Display for Val {
if first {
first = false;
} else {
write!(f, ", ").expect("Failed to write");
write!(f, ", ")?;
}
write!(f, "{}: {}", k, v).expect("Failed to write");
write!(f, "{}: {}", k, v.pretty())?;
}
f.write_str(" }")
@@ -627,8 +637,8 @@ impl std::fmt::Display for Val {
Val::Class(_) => write!(f, "\x1b[36m[Class]\x1b[39m"),
// TODO: Improve printing these
Val::Static(s) => s.fmt(f),
Val::Custom(c) => c.fmt(f),
Val::Static(s) => s.pretty_fmt(f),
Val::Custom(c) => c.pretty_fmt(f),
}
}
}