Add magic in variable, part 2 (#310)

This commit is contained in:
JT 2021-11-08 20:13:55 +13:00 committed by GitHub
parent e9a7def183
commit 066afb059e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 6 deletions

View File

@ -3197,6 +3197,7 @@ pub fn parse_block(
*expr = wrap_expr_with_collect(working_set, expr); *expr = wrap_expr_with_collect(working_set, expr);
} }
} }
Statement::Pipeline(Pipeline { Statement::Pipeline(Pipeline {
expressions: output, expressions: output,
}) })

View File

@ -217,13 +217,35 @@ impl Expression {
right.replace_in_variable(working_set, new_var_id); right.replace_in_variable(working_set, new_var_id);
} }
Expr::Block(block_id) => { Expr::Block(block_id) => {
let block = working_set.get_block(*block_id);
let new_expr = if let Some(Statement::Pipeline(pipeline)) = block.stmts.get(0) {
if let Some(expr) = pipeline.expressions.get(0) {
let mut new_expr = expr.clone();
new_expr.replace_in_variable(working_set, new_var_id);
Some(new_expr)
} else {
None
}
} else {
None
};
let block = working_set.get_block_mut(*block_id); let block = working_set.get_block_mut(*block_id);
if let Some(Statement::Pipeline(pipeline)) = block.stmts.get_mut(0) { if let Some(new_expr) = new_expr {
if let Some(expr) = pipeline.expressions.get_mut(0) { if let Some(Statement::Pipeline(pipeline)) = block.stmts.get_mut(0) {
expr.clone().replace_in_variable(working_set, new_var_id) if let Some(expr) = pipeline.expressions.get_mut(0) {
*expr = new_expr
}
} }
} }
block.captures = block
.captures
.iter()
.map(|x| if *x != IN_VARIABLE_ID { *x } else { new_var_id })
.collect();
} }
Expr::Bool(_) => {} Expr::Bool(_) => {}
Expr::Call(call) => { Expr::Call(call) => {
@ -274,13 +296,35 @@ impl Expression {
Expr::Signature(_) => {} Expr::Signature(_) => {}
Expr::String(_) => {} Expr::String(_) => {}
Expr::Subexpression(block_id) => { Expr::Subexpression(block_id) => {
let block = working_set.get_block(*block_id);
let new_expr = if let Some(Statement::Pipeline(pipeline)) = block.stmts.get(0) {
if let Some(expr) = pipeline.expressions.get(0) {
let mut new_expr = expr.clone();
new_expr.replace_in_variable(working_set, new_var_id);
Some(new_expr)
} else {
None
}
} else {
None
};
let block = working_set.get_block_mut(*block_id); let block = working_set.get_block_mut(*block_id);
if let Some(Statement::Pipeline(pipeline)) = block.stmts.get_mut(0) { if let Some(new_expr) = new_expr {
if let Some(expr) = pipeline.expressions.get_mut(0) { if let Some(Statement::Pipeline(pipeline)) = block.stmts.get_mut(0) {
expr.clone().replace_in_variable(working_set, new_var_id) if let Some(expr) = pipeline.expressions.get_mut(0) {
*expr = new_expr
}
} }
} }
block.captures = block
.captures
.iter()
.map(|x| if *x != IN_VARIABLE_ID { *x } else { new_var_id })
.collect();
} }
Expr::Table(headers, cells) => { Expr::Table(headers, cells) => {
for header in headers { for header in headers {

View File

@ -869,3 +869,23 @@ fn in_variable_1() -> TestResult {
fn in_variable_2() -> TestResult { fn in_variable_2() -> TestResult {
run_test(r#"3 | if $in > 2 { "yay!" } else { "boo" }"#, "yay!") run_test(r#"3 | if $in > 2 { "yay!" } else { "boo" }"#, "yay!")
} }
#[test]
fn in_variable_3() -> TestResult {
run_test(r#"3 | if $in > 4 { "yay!" } else { $in }"#, "3")
}
#[test]
fn in_variable_4() -> TestResult {
run_test(r#"3 | do { $in }"#, "3")
}
#[test]
fn in_variable_5() -> TestResult {
run_test(r#"3 | if $in > 2 { $in - 10 } else { $in * 10 }"#, "-7")
}
#[test]
fn in_variable_6() -> TestResult {
run_test(r#"3 | if $in > 6 { $in - 10 } else { $in * 10 }"#, "30")
}