From 4d09f14a469a9c1454808958384852a7c187c301 Mon Sep 17 00:00:00 2001 From: caelansar <819711623@qq.com> Date: Wed, 26 Jun 2024 20:24:09 +0800 Subject: [PATCH] feat: quote string object --- src/eval/mod.rs | 2 +- src/eval/object.rs | 7 ++++++- src/vm/mod.rs | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/eval/mod.rs b/src/eval/mod.rs index ceae188..b34b111 100644 --- a/src/eval/mod.rs +++ b/src/eval/mod.rs @@ -842,7 +842,7 @@ mod test { ), ( r#"-"str""#, - Some(Object::Error("unknown operator: -str".into())), + Some(Object::Error("unknown operator: -\"str\"".into())), ), ( "1-true; 10", diff --git a/src/eval/object.rs b/src/eval/object.rs index 9743bbe..6a7928c 100644 --- a/src/eval/object.rs +++ b/src/eval/object.rs @@ -132,7 +132,7 @@ impl Display for Object { Object::Int(i) => write!(f, "{}", i), Object::Float(float) => write!(f, "{}", float), Object::Bool(b) => write!(f, "{}", b), - Object::String(s) => write!(f, "{}", s), + Object::String(s) => write!(f, "\"{}\"", s), Object::Return(r) => write!(f, "{}", r), Object::Array(e) => write!( f, @@ -203,6 +203,7 @@ fn object_display_should_work() { assert_eq!("true", Object::Bool(true).to_string()); assert_eq!("false", Object::Bool(false).to_string()); assert_eq!("null", Object::Null.to_string()); + assert_eq!("\"aa\"", Object::String("aa".into()).to_string()); } #[test] @@ -211,6 +212,10 @@ fn object_arithmetic_should_work() { Object::Error("type mismatch: 123 + 1.1".into()), &Object::Int(123) + &Object::Float(1.1) ); + assert_eq!( + Object::Error("type mismatch: 123 + \"4\"".into()), + &Object::Int(123) + &Object::String("4".into()) + ); assert_eq!(Object::Int(124), &Object::Int(123) + &Object::Int(1)); assert_eq!( Object::String("12".into()), diff --git a/src/vm/mod.rs b/src/vm/mod.rs index 4a72f83..f4cc6c4 100644 --- a/src/vm/mod.rs +++ b/src/vm/mod.rs @@ -791,7 +791,7 @@ mod test { let no_function = "aa"; no_function() "#, - Some(object::Object::Error("aa not a function".into())), + Some(object::Object::Error("\"aa\" not a function".into())), ), ( r#"