Refactor Destructure to handle assignments
Renames `DefDestructure` to `Destructure` to better reflect its use in both definitions and assignments. Introduces `bind_assign_pattern` to handle assignment destructuring in the binder. Adds `test_assign_destructuring` to verify assignment destructuring functionality.
This commit is contained in:
@@ -337,4 +337,36 @@ mod tests {
|
||||
panic!("Expected tuple return from def, got {:?}", res);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_assign_destructuring() {
|
||||
// 1. Simple assignment destructuring
|
||||
{
|
||||
let env = Environment::new();
|
||||
let source_simple = "(do (def a 0) (def b 0) (assign [a b] [10 20]) (+ a b))";
|
||||
assert_eq!(format!("{}", env.run_script(source_simple).unwrap()), "30");
|
||||
}
|
||||
|
||||
// 2. Nested assignment destructuring
|
||||
{
|
||||
let env = Environment::new();
|
||||
let source_nested =
|
||||
"(do (def a 0) (def b 0) (def c 0) (assign [a [b c]] [1 [2 3]]) (+ a (+ b c)))";
|
||||
assert_eq!(format!("{}", env.run_script(source_nested).unwrap()), "6");
|
||||
}
|
||||
|
||||
// 3. Assignment returns the assigned value
|
||||
{
|
||||
let env = Environment::new();
|
||||
let source_return = "(do (def a 0) (def b 0) (assign [a b] [5 6]))";
|
||||
let res = env.run_script(source_return).unwrap();
|
||||
if let Value::Tuple(vals) = res {
|
||||
assert_eq!(vals.len(), 2);
|
||||
assert_eq!(format!("{}", vals[0]), "5");
|
||||
assert_eq!(format!("{}", vals[1]), "6");
|
||||
} else {
|
||||
panic!("Expected tuple return from assign, got {:?}", res);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user