Split blocks and closures (#7075)

* Split closures and blocks

* Tests mostly working

* finish last fixes, passes all tests

* fmt
This commit is contained in:
JT
2022-11-10 21:21:49 +13:00
committed by GitHub
parent 921a66554e
commit 63433f1bc8
57 changed files with 576 additions and 220 deletions

View File

@ -63,6 +63,7 @@ impl Expression {
pub fn as_block(&self) -> Option<BlockId> {
match self.expr {
Expr::Block(block_id) => Some(block_id),
Expr::Closure(block_id) => Some(block_id),
_ => None,
}
}
@ -139,6 +140,22 @@ impl Expression {
false
}
}
Expr::Closure(block_id) => {
let block = working_set.get_block(*block_id);
if block.captures.contains(&IN_VARIABLE_ID) {
return true;
}
if let Some(pipeline) = block.pipelines.get(0) {
match pipeline.expressions.get(0) {
Some(expr) => expr.has_in_variable(working_set),
None => false,
}
} else {
false
}
}
Expr::Binary(_) => false,
Expr::Bool(_) => false,
Expr::Call(call) => {
@ -310,6 +327,37 @@ impl Expression {
.map(|x| if *x != IN_VARIABLE_ID { *x } else { new_var_id })
.collect();
}
Expr::Closure(block_id) => {
let block = working_set.get_block(*block_id);
let new_expr = if let Some(pipeline) = block.pipelines.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);
if let Some(new_expr) = new_expr {
if let Some(pipeline) = block.pipelines.get_mut(0) {
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::Binary(_) => {}
Expr::Bool(_) => {}
Expr::Call(call) => {
@ -456,6 +504,17 @@ impl Expression {
*block_id = working_set.add_block(block);
}
Expr::Closure(block_id) => {
let mut block = working_set.get_block(*block_id).clone();
for pipeline in block.pipelines.iter_mut() {
for expr in pipeline.expressions.iter_mut() {
expr.replace_span(working_set, replaced, new_span)
}
}
*block_id = working_set.add_block(block);
}
Expr::Binary(_) => {}
Expr::Bool(_) => {}
Expr::Call(call) => {