From df9ecb4de17ef9b9f99cbf23cd77a8003a87bca4 Mon Sep 17 00:00:00 2001 From: 0264408 Date: Wed, 17 Jun 2026 16:08:40 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E7=B1=BB=E5=9E=8B=E5=9C=A8=E4=B8=8D=E5=90=8C=E5=9C=BA?= =?UTF-8?q?=E6=99=AF=E7=9A=84=E6=89=93=E5=8D=B0=E5=BD=A2=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/interpreter/mod.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/interpreter/mod.rs b/src/interpreter/mod.rs index 26d842c..3d0b133 100644 --- a/src/interpreter/mod.rs +++ b/src/interpreter/mod.rs @@ -71,18 +71,30 @@ impl std::fmt::Debug for Value { } } +impl Value { + /// 容器内值的格式化:字符串加引号以区分类型,其余类型用 Display + fn fmt_element(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Value::String(s) => write!(f, "\"{}\"", s), + other => write!(f, "{}", other), + } + } +} + impl fmt::Display for Value { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Value::Number(n) => write!(f, "{}", n), - Value::String(s) => write!(f, "\"{}\"", s), + Value::String(s) => write!(f, "{}", s), Value::Bool(b) => write!(f, "{}", b), Value::Nil => write!(f, "nil"), Value::Object(obj) => { let obj = obj.borrow(); write!(f, "{{ ")?; for (key, value) in obj.iter() { - write!(f, "{}: {}, ", key, value)?; + write!(f, "{}: ", key)?; + value.fmt_element(f)?; + write!(f, ", ")?; } write!(f, "}}") }, @@ -93,7 +105,7 @@ impl fmt::Display for Value { if i > 0 { write!(f, ", ")?; } - write!(f, "{}", val)?; + val.fmt_element(f)?; } write!(f, "]") },