Add support for destructuring def
This commit introduces the `DefDestructure` bound kind and modifies the binder, analyzer, type checker, and VM to support destructuring in `def` statements. This allows for pattern matching on the right-hand side of a `def` to bind multiple variables. The parser has been updated to accept patterns in `def` statements. The binder now handles `UntypedKind::Def` with a `target` pattern, rather than a simple `name`. This enables destructuring. The `Gemini.md` documentation has been updated to include a new rule for incremental development.
This commit is contained in:
@@ -312,4 +312,28 @@ mod tests {
|
||||
dump_record
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_def_destructuring() {
|
||||
let env = Environment::new();
|
||||
|
||||
// 1. Global destructuring
|
||||
let source_global = "(do (def [a b] [1 2]) (+ a b))";
|
||||
assert_eq!(format!("{}", env.run_script(source_global).unwrap()), "3");
|
||||
|
||||
// 2. Local nested destructuring inside a function
|
||||
let source_local = "((fn [x] (do (def [a [[b c] d]] x) (+ a (+ b (+ c d))))) [1 [[2 3] 4]])";
|
||||
assert_eq!(format!("{}", env.run_script(source_local).unwrap()), "10");
|
||||
|
||||
// 3. Verify 'def' returns the assigned value
|
||||
let source_return = "(def [x y] [7 8])";
|
||||
let res = env.run_script(source_return).unwrap();
|
||||
if let Value::Tuple(vals) = res {
|
||||
assert_eq!(vals.len(), 2);
|
||||
assert_eq!(format!("{}", vals[0]), "7");
|
||||
assert_eq!(format!("{}", vals[1]), "8");
|
||||
} else {
|
||||
panic!("Expected tuple return from def, got {:?}", res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user