2 issues uncovered that need solving

This commit is contained in:
Shautvast 2025-11-01 09:47:41 +01:00
parent 55a30afd06
commit 8f1320eae7
3 changed files with 26 additions and 30 deletions

View file

@ -1,9 +1,7 @@
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{compile, run};
use crate::scanner::scan;
use crate::value::Value; use crate::value::Value;
use crate::vm::interpret; use crate::{compile, run};
#[test] #[test]
fn literal_int() { fn literal_int() {
@ -12,37 +10,43 @@ mod tests {
#[test] #[test]
fn literal_float() { fn literal_float() {
assert!(compile("2.1").is_ok()); assert_eq!(run("2.1"), Ok(Value::F64(2.1)));
} }
#[test] #[test]
fn literal_float_scientific() { fn literal_float_scientific() {
assert!(compile("2.1e5").is_ok()); assert_eq!(run("2.1e5"), Ok(Value::F64(2.1e5)));
} }
#[test] #[test]
fn literal_string() { fn literal_string() {
assert!(compile(r#""a""#).is_ok()); assert_eq!(run(r#""a""#), Ok(Value::String("a".into())));
} }
#[test] #[test]
fn literal_list() { 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] #[test]
fn let_infer_type() { 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] #[test]
fn let_u32() { 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] #[test]
fn let_char() { 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] #[test]
@ -83,15 +87,13 @@ mod tests {
#[test] #[test]
fn call_fn_with_args_returns_value() { fn call_fn_with_args_returns_value() {
let r = compile( assert_eq!(
r#" run(r#"
fn add_hello(name: string) -> string: fn add_hello(name: string) -> string:
"Hello " + name "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] #[test]
@ -101,7 +103,7 @@ add_hello("world")"#,
object Person: object Person:
name: string"#, name: string"#,
); );
assert!(r.is_ok()); assert!(r.is_ok()); // does nothing runtime
} }
// #[test] // #[test]
@ -119,9 +121,9 @@ object Person:
#[test] #[test]
fn let_map() { fn let_map() {
let r = compile(r#"{"name": "Dent", "age": 40 }"#); let result = run(r#"{"name": "Dent", "age": 40 }"#);
assert!(r.is_ok()); assert!(result.is_ok());
let result = interpret(&r.unwrap(), "main").unwrap(); let result = result.unwrap();
if let Value::Map(map) = result { if let Value::Map(map) = result {
assert_eq!( assert_eq!(
map.get(&Value::String("name".to_string())).unwrap(), map.get(&Value::String("name".to_string())).unwrap(),

View file

@ -45,7 +45,7 @@ pub enum CompilerError {
#[derive(Error, Debug, PartialEq)] #[derive(Error, Debug, PartialEq)]
pub enum RuntimeError { pub enum RuntimeError {
#[error("Error while executing")] #[error("Error while executing")]
Value(#[from] ValueError), ValueError(#[from] ValueError),
#[error("Error occurred")] #[error("Error occurred")]
Something, Something,
#[error("Expected {0}, got {1}")] #[error("Expected {0}, got {1}")]

View file

@ -74,15 +74,9 @@ impl Into<Value> for &str {
} }
} }
impl From<String> for Value { impl Into<Value> for String {
fn from(s: String) -> Self { fn into(self) -> Value {
Value::String(s.clone()) Value::String(self)
}
}
impl From<&String> for Value {
fn from(s: &String) -> Self {
Value::String(s.clone())
} }
} }