From 8f1320eae788aeb7608d84b97b64550a5a405ba1 Mon Sep 17 00:00:00 2001 From: Shautvast Date: Sat, 1 Nov 2025 09:47:41 +0100 Subject: [PATCH] 2 issues uncovered that need solving --- src/compiler_tests.rs | 42 ++++++++++++++++++++++-------------------- src/errors.rs | 2 +- src/value.rs | 12 +++--------- 3 files changed, 26 insertions(+), 30 deletions(-) diff --git a/src/compiler_tests.rs b/src/compiler_tests.rs index 26ef21c..966f32f 100644 --- a/src/compiler_tests.rs +++ b/src/compiler_tests.rs @@ -1,9 +1,7 @@ #[cfg(test)] mod tests { - use crate::{compile, run}; - use crate::scanner::scan; use crate::value::Value; - use crate::vm::interpret; + use crate::{compile, run}; #[test] fn literal_int() { @@ -12,37 +10,43 @@ mod tests { #[test] fn literal_float() { - assert!(compile("2.1").is_ok()); + assert_eq!(run("2.1"), Ok(Value::F64(2.1))); } #[test] fn literal_float_scientific() { - assert!(compile("2.1e5").is_ok()); + assert_eq!(run("2.1e5"), Ok(Value::F64(2.1e5))); } #[test] fn literal_string() { - assert!(compile(r#""a""#).is_ok()); + assert_eq!(run(r#""a""#), Ok(Value::String("a".into()))); } #[test] fn literal_list() { - assert!(compile(r#"["abc","def"]"#).is_ok()); + assert_eq!(run(r#"["abc","def"]"#), Ok(Value::List(vec![Value::String("abc".into()), Value::String("def".into())]))); } #[test] fn let_infer_type() { - assert!(compile(r#"let a=1"#).is_ok()); + assert_eq!(run(r#"let a=1 +a"#), Ok(Value::I64(1))); } #[test] fn let_u32() { - assert!(compile(r#"let a:u32=1"#).is_ok()); + assert_eq!(run(r#"let a:u32=1 +a"#), Ok(Value::U32(1))); } #[test] fn let_char() { - assert!(scan(r#"let a:char='a'"#).is_ok()); + assert_eq!( + run(r#"let a:char='a' +a"#), + Ok(Value::Char('a')) + ); } #[test] @@ -83,15 +87,13 @@ mod tests { #[test] fn call_fn_with_args_returns_value() { - let r = compile( - r#" + assert_eq!( + run(r#" fn add_hello(name: string) -> string: "Hello " + name -add_hello("world")"#, +add_hello("world")"#,), + Ok(Value::String("Hello world".to_string())) ); - assert!(r.is_ok()); - let result = interpret(&r.unwrap(), "main").unwrap(); - assert_eq!(result, Value::String("Hello world".to_string())); } #[test] @@ -101,7 +103,7 @@ add_hello("world")"#, object Person: name: string"#, ); - assert!(r.is_ok()); + assert!(r.is_ok()); // does nothing runtime } // #[test] @@ -119,9 +121,9 @@ object Person: #[test] fn let_map() { - let r = compile(r#"{"name": "Dent", "age": 40 }"#); - assert!(r.is_ok()); - let result = interpret(&r.unwrap(), "main").unwrap(); + let result = run(r#"{"name": "Dent", "age": 40 }"#); + assert!(result.is_ok()); + let result = result.unwrap(); if let Value::Map(map) = result { assert_eq!( map.get(&Value::String("name".to_string())).unwrap(), diff --git a/src/errors.rs b/src/errors.rs index c4d6772..0fa85e8 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -45,7 +45,7 @@ pub enum CompilerError { #[derive(Error, Debug, PartialEq)] pub enum RuntimeError { #[error("Error while executing")] - Value(#[from] ValueError), + ValueError(#[from] ValueError), #[error("Error occurred")] Something, #[error("Expected {0}, got {1}")] diff --git a/src/value.rs b/src/value.rs index cd0ca83..30f4dc4 100644 --- a/src/value.rs +++ b/src/value.rs @@ -74,15 +74,9 @@ impl Into for &str { } } -impl From for Value { - fn from(s: String) -> Self { - Value::String(s.clone()) - } -} - -impl From<&String> for Value { - fn from(s: &String) -> Self { - Value::String(s.clone()) +impl Into for String { + fn into(self) -> Value { + Value::String(self) } }