mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-02 08:11:12 +00:00
simplify pattern match
This commit is contained in:
parent
4668cf506b
commit
44c08779c3
1 changed files with 59 additions and 91 deletions
|
@ -1358,21 +1358,23 @@ fn canonicalize_pending_value_def_new<'a>(
|
||||||
// which also implies it's not a self tail call!
|
// which also implies it's not a self tail call!
|
||||||
//
|
//
|
||||||
// Only defs of the form (foo = ...) can be closure declarations or self tail calls.
|
// Only defs of the form (foo = ...) can be closure declarations or self tail calls.
|
||||||
if let Pattern::Identifier(symbol)
|
|
||||||
| Pattern::AbilityMemberSpecialization { ident: symbol, .. } = loc_can_pattern.value
|
match (&loc_can_pattern.value, &loc_can_expr.value) {
|
||||||
{
|
(
|
||||||
if let Closure(ClosureData {
|
Pattern::Identifier(symbol)
|
||||||
|
| Pattern::AbilityMemberSpecialization { ident: symbol, .. },
|
||||||
|
Closure(ClosureData {
|
||||||
function_type,
|
function_type,
|
||||||
closure_type,
|
closure_type,
|
||||||
closure_ext_var,
|
closure_ext_var,
|
||||||
return_type,
|
return_type,
|
||||||
name: ref closure_name,
|
name: closure_name,
|
||||||
ref arguments,
|
arguments,
|
||||||
loc_body: ref body,
|
loc_body: body,
|
||||||
ref captured_symbols,
|
captured_symbols,
|
||||||
..
|
..
|
||||||
}) = loc_can_expr.value
|
}),
|
||||||
{
|
) => {
|
||||||
// Since everywhere in the code it'll be referred to by its defined name,
|
// Since everywhere in the code it'll be referred to by its defined name,
|
||||||
// remove its generated name from the closure map. (We'll re-insert it later.)
|
// remove its generated name from the closure map. (We'll re-insert it later.)
|
||||||
let closure_references = env.closures.remove(closure_name).unwrap_or_else(|| {
|
let closure_references = env.closures.remove(closure_name).unwrap_or_else(|| {
|
||||||
|
@ -1384,17 +1386,16 @@ fn canonicalize_pending_value_def_new<'a>(
|
||||||
|
|
||||||
// The closure is self tail recursive iff it tail calls itself (by defined name).
|
// The closure is self tail recursive iff it tail calls itself (by defined name).
|
||||||
let is_recursive = match can_output.tail_call {
|
let is_recursive = match can_output.tail_call {
|
||||||
Some(tail_symbol) if tail_symbol == symbol => Recursive::TailRecursive,
|
Some(tail_symbol) if tail_symbol == *symbol => Recursive::TailRecursive,
|
||||||
_ => Recursive::NotRecursive,
|
_ => Recursive::NotRecursive,
|
||||||
};
|
};
|
||||||
|
|
||||||
// renamed_closure_def = Some(&symbol);
|
|
||||||
loc_can_expr.value = Closure(ClosureData {
|
loc_can_expr.value = Closure(ClosureData {
|
||||||
function_type,
|
function_type: *function_type,
|
||||||
closure_type,
|
closure_type: *closure_type,
|
||||||
closure_ext_var,
|
closure_ext_var: *closure_ext_var,
|
||||||
return_type,
|
return_type: *return_type,
|
||||||
name: symbol,
|
name: *symbol,
|
||||||
captured_symbols: captured_symbols.clone(),
|
captured_symbols: captured_symbols.clone(),
|
||||||
recursive: is_recursive,
|
recursive: is_recursive,
|
||||||
arguments: arguments.clone(),
|
arguments: arguments.clone(),
|
||||||
|
@ -1416,7 +1417,8 @@ fn canonicalize_pending_value_def_new<'a>(
|
||||||
references: DefReferences::Function(closure_references),
|
references: DefReferences::Function(closure_references),
|
||||||
def,
|
def,
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
_ => {
|
||||||
let refs = can_output.references.clone();
|
let refs = can_output.references.clone();
|
||||||
|
|
||||||
let def = single_can_def(
|
let def = single_can_def(
|
||||||
|
@ -1435,24 +1437,6 @@ fn canonicalize_pending_value_def_new<'a>(
|
||||||
def,
|
def,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
let refs = can_output.references.clone();
|
|
||||||
|
|
||||||
let def = single_can_def(
|
|
||||||
loc_can_pattern,
|
|
||||||
loc_can_expr,
|
|
||||||
expr_var,
|
|
||||||
Some(Loc::at(loc_ann.region, type_annotation)),
|
|
||||||
vars_by_symbol.clone(),
|
|
||||||
);
|
|
||||||
|
|
||||||
output.union(can_output);
|
|
||||||
|
|
||||||
TempOutput {
|
|
||||||
output,
|
|
||||||
references: DefReferences::Value(refs),
|
|
||||||
def,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// If we have a pattern, then the def has a body (that is, it's not a
|
// If we have a pattern, then the def has a body (that is, it's not a
|
||||||
|
@ -1490,19 +1474,21 @@ fn canonicalize_pending_value_def_new<'a>(
|
||||||
// which also implies it's not a self tail call!
|
// which also implies it's not a self tail call!
|
||||||
//
|
//
|
||||||
// Only defs of the form (foo = ...) can be closure declarations or self tail calls.
|
// Only defs of the form (foo = ...) can be closure declarations or self tail calls.
|
||||||
if let Pattern::Identifier(symbol) = loc_can_pattern.value {
|
match (&loc_can_pattern.value, &loc_can_expr.value) {
|
||||||
if let Closure(ClosureData {
|
(
|
||||||
|
Pattern::Identifier(symbol),
|
||||||
|
Closure(ClosureData {
|
||||||
function_type,
|
function_type,
|
||||||
closure_type,
|
closure_type,
|
||||||
closure_ext_var,
|
closure_ext_var,
|
||||||
return_type,
|
return_type,
|
||||||
name: ref closure_name,
|
name: closure_name,
|
||||||
ref arguments,
|
arguments,
|
||||||
loc_body: ref body,
|
loc_body: body,
|
||||||
ref captured_symbols,
|
captured_symbols,
|
||||||
..
|
..
|
||||||
}) = loc_can_expr.value
|
}),
|
||||||
{
|
) => {
|
||||||
// Since everywhere in the code it'll be referred to by its defined name,
|
// Since everywhere in the code it'll be referred to by its defined name,
|
||||||
// remove its generated name from the closure map. (We'll re-insert it later.)
|
// remove its generated name from the closure map. (We'll re-insert it later.)
|
||||||
let closure_references = env.closures.remove(closure_name).unwrap_or_else(|| {
|
let closure_references = env.closures.remove(closure_name).unwrap_or_else(|| {
|
||||||
|
@ -1514,16 +1500,16 @@ fn canonicalize_pending_value_def_new<'a>(
|
||||||
|
|
||||||
// The closure is self tail recursive iff it tail calls itself (by defined name).
|
// The closure is self tail recursive iff it tail calls itself (by defined name).
|
||||||
let is_recursive = match can_output.tail_call {
|
let is_recursive = match can_output.tail_call {
|
||||||
Some(tail_symbol) if tail_symbol == symbol => Recursive::TailRecursive,
|
Some(tail_symbol) if tail_symbol == *symbol => Recursive::TailRecursive,
|
||||||
_ => Recursive::NotRecursive,
|
_ => Recursive::NotRecursive,
|
||||||
};
|
};
|
||||||
|
|
||||||
loc_can_expr.value = Closure(ClosureData {
|
loc_can_expr.value = Closure(ClosureData {
|
||||||
function_type,
|
function_type: *function_type,
|
||||||
closure_type,
|
closure_type: *closure_type,
|
||||||
closure_ext_var,
|
closure_ext_var: *closure_ext_var,
|
||||||
return_type,
|
return_type: *return_type,
|
||||||
name: symbol,
|
name: *symbol,
|
||||||
captured_symbols: captured_symbols.clone(),
|
captured_symbols: captured_symbols.clone(),
|
||||||
recursive: is_recursive,
|
recursive: is_recursive,
|
||||||
arguments: arguments.clone(),
|
arguments: arguments.clone(),
|
||||||
|
@ -1545,7 +1531,8 @@ fn canonicalize_pending_value_def_new<'a>(
|
||||||
references: DefReferences::Function(closure_references),
|
references: DefReferences::Function(closure_references),
|
||||||
def,
|
def,
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
_ => {
|
||||||
let refs = can_output.references.clone();
|
let refs = can_output.references.clone();
|
||||||
|
|
||||||
let def = single_can_def(
|
let def = single_can_def(
|
||||||
|
@ -1564,25 +1551,6 @@ fn canonicalize_pending_value_def_new<'a>(
|
||||||
def,
|
def,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
let refs = can_output.references.clone();
|
|
||||||
|
|
||||||
let def = single_can_def(
|
|
||||||
loc_can_pattern,
|
|
||||||
loc_can_expr,
|
|
||||||
expr_var,
|
|
||||||
None,
|
|
||||||
vars_by_symbol.clone(),
|
|
||||||
);
|
|
||||||
|
|
||||||
output.union(can_output);
|
|
||||||
|
|
||||||
TempOutput {
|
|
||||||
output,
|
|
||||||
references: refs,
|
|
||||||
closure_references: None,
|
|
||||||
def,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue