diff --git a/TODO.md b/TODO.md index 38f329433a..9c819da580 100644 --- a/TODO.md +++ b/TODO.md @@ -14,12 +14,15 @@ - [x] parsing tables - [x] Block params - [x] Ranges +- [ ] Iteration (`each`) over tables +- [ ] ctrl-c support - [ ] Column path - [ ] ...rest without calling it rest - [ ] operator overflow - [ ] finish operator type-checking - [ ] Source - [ ] Autoenv +- [ ] Externals - [ ] let [first, rest] = [1, 2, 3] ## Maybe: diff --git a/crates/nu-protocol/src/engine/evaluation_context.rs b/crates/nu-protocol/src/engine/evaluation_context.rs index 5229659343..8eaa68752e 100644 --- a/crates/nu-protocol/src/engine/evaluation_context.rs +++ b/crates/nu-protocol/src/engine/evaluation_context.rs @@ -22,6 +22,29 @@ impl EvaluationContext { } pub fn add_var(&self, var_id: VarId, value: Value) { + // We need to make values concreate before we assign them to variables, as stream values + // will drain and remain drained. + // + // TODO: find a good home for this + // TODO: add ctrl-c support + + let value = match value { + Value::RowStream { + headers, + stream, + span, + } => Value::Table { + headers, + val: stream.collect(), + span, + }, + Value::ValueStream { stream, span } => Value::List { + val: stream.collect(), + span, + }, + x => x, + }; + self.stack.add_var(var_id, value); } diff --git a/src/tests.rs b/src/tests.rs index 4082b0d904..ffb2bcf544 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -229,6 +229,14 @@ fn simple_value_iteration() -> TestResult { run_test("4 | each { $it + 10 }", "14") } +#[test] +fn concrete_variable_assignment() -> TestResult { + run_test( + "let x = (1..100 | each { |y| $y + 100 }); $x | length; $x | length", + "100", + ) +} + #[test] fn build_string1() -> TestResult { run_test("build-string 'nu' 'shell'", "nushell")