Fixing captures (#723)

* WIP fixing captures

* small fix

* WIP

* Rewrite to proof-of-concept better parse_def

* Add missing file

* Finish capture refactor

* Fix tests

* Add more tests
This commit is contained in:
JT
2022-01-12 15:06:56 +11:00
committed by GitHub
parent 47495715a6
commit 186da4d725
30 changed files with 424 additions and 164 deletions

View File

@ -40,7 +40,7 @@ pub fn convert_env_values(
let block = engine_state.get_block(block_id);
if let Some(var) = block.signature.get_positional(0) {
let mut stack = stack.collect_captures(&block.captures);
let mut stack = stack.gather_captures(&block.captures);
if let Some(var_id) = &var.var_id {
stack.add_var(*var_id, val.clone());
}
@ -92,7 +92,7 @@ pub fn env_to_string(
if let Some(var) = block.signature.get_positional(0) {
let val_span = value.span()?;
let mut stack = stack.collect_captures(&block.captures);
let mut stack = stack.gather_captures(&block.captures);
if let Some(var_id) = &var.var_id {
stack.add_var(*var_id, value);

View File

@ -1,4 +1,5 @@
use std::cmp::Ordering;
use std::collections::HashMap;
use std::io::Write;
use nu_protocol::ast::{Block, Call, Expr, Expression, Operator, Statement};
@ -40,7 +41,7 @@ fn eval_call(
} else if let Some(block_id) = decl.get_block_id() {
let block = engine_state.get_block(block_id);
let mut callee_stack = caller_stack.collect_captures(&block.captures);
let mut callee_stack = caller_stack.gather_captures(&block.captures);
for (param_idx, param) in decl
.signature()
@ -286,7 +287,7 @@ pub fn eval_expression(
Operator::Pow => lhs.pow(op_span, &rhs),
}
}
Expr::RowCondition(block_id) | Expr::Subexpression(block_id) => {
Expr::Subexpression(block_id) => {
let block = engine_state.get_block(*block_id);
// FIXME: protect this collect with ctrl-c
@ -295,10 +296,22 @@ pub fn eval_expression(
.into_value(expr.span),
)
}
Expr::Block(block_id) => Ok(Value::Block {
val: *block_id,
span: expr.span,
}),
Expr::RowCondition(block_id) | Expr::Block(block_id) => {
let mut captures = HashMap::new();
let block = engine_state.get_block(*block_id);
for var_id in &block.captures {
captures.insert(
*var_id,
stack.get_var(*var_id)?, //.map_err(|_| ShellError::VariableNotFoundAtRuntime(expr.span))?,
);
}
Ok(Value::Block {
val: *block_id,
captures,
span: expr.span,
})
}
Expr::List(x) => {
let mut output = vec![];
for expr in x {