fix array bug with string concat in display

This commit is contained in:
jess 2026-05-28 04:33:49 -07:00
parent 2111fcac62
commit 5d40b4047a
1 changed files with 67 additions and 0 deletions

View File

@ -2366,6 +2366,23 @@ impl Interpreter {
(Op::Add, Value::Number(a), Value::Str(b)) => Ok(Value::Str(format!("{}{}", format_number(*a), b))),
(Op::Add, Value::Str(a), Value::Bool(b)) => Ok(Value::Str(format!("{}{}", a, b))),
(Op::Add, Value::Bool(a), Value::Str(b)) => Ok(Value::Str(format!("{}{}", a, b))),
(Op::Add, Value::Str(s), other) => Ok(Value::Str(format!("{}{}", s, other.display()))),
(Op::Add, other, Value::Str(s)) => Ok(Value::Str(format!("{}{}", other.display(), s))),
(Op::Add, Value::Array(a), Value::Array(b)) => {
let mut out = a.clone();
out.extend(b.iter().cloned());
Ok(Value::Array(out))
}
(Op::Add, Value::Array(a), other) => {
let mut out = a.clone();
out.push(other.clone());
Ok(Value::Array(out))
}
(Op::Add, other, Value::Array(a)) => {
let mut out = vec![other.clone()];
out.extend(a.iter().cloned());
Ok(Value::Array(out))
}
(Op::Lt, Value::Number(a), Value::Number(b)) => Ok(Value::Bool(a < b)),
(Op::Gt, Value::Number(a), Value::Number(b)) => Ok(Value::Bool(a > b)),
@ -6164,6 +6181,56 @@ fn find(arr, target) {
assert!(matches!(v, Value::Number(n) if n == 3.0));
}
#[test]
fn add_str_plus_array_stringifies() {
let mut i = Interpreter::new();
let v = i.eval_expr_str(r#""Diff: " + [1, 2]"#).unwrap();
match v {
Value::Str(s) => assert_eq!(s, "Diff: [1, 2]"),
_ => panic!("expected Str, got {:?}", v),
}
}
#[test]
fn add_array_plus_str_stringifies() {
let mut i = Interpreter::new();
let v = i.eval_expr_str(r#"[1, 2] + " items""#).unwrap();
match v {
Value::Str(s) => assert_eq!(s, "[1, 2] items"),
_ => panic!(),
}
}
#[test]
fn add_array_plus_array_concats() {
let mut i = Interpreter::new();
let v = i.eval_expr_str("[1, 2] + [3, 4]").unwrap();
match v {
Value::Array(a) => assert_eq!(a.len(), 4),
_ => panic!(),
}
}
#[test]
fn add_array_plus_number_appends() {
let mut i = Interpreter::new();
let v = i.eval_expr_str("[1, 2] + 3").unwrap();
match v {
Value::Array(a) => assert_eq!(a.len(), 3),
_ => panic!(),
}
}
#[test]
fn add_chains_str_array_str() {
let mut i = Interpreter::new();
let v = i.eval_expr_str(r#""Diff: " + [10, 20] + " + Sum: " + [30, 40]"#).unwrap();
match v {
Value::Str(s) => assert_eq!(s, "Diff: [10, 20] + Sum: [30, 40]"),
_ => panic!(),
}
}
#[test]
fn rand_unit_range() {
let mut i = Interpreter::new();