mirror of
https://github.com/HigherOrderCO/Bend.git
synced 2024-11-05 04:51:40 +03:00
Fix encode_str & encode_list to look inside match arms patterns
This commit is contained in:
parent
4abe695a5e
commit
bf07d22654
@ -616,6 +616,27 @@ impl Pattern {
|
||||
set.into_iter().flat_map(|a| a.as_mut())
|
||||
}
|
||||
|
||||
pub fn ctrs(&self) -> impl DoubleEndedIterator<Item = &Name> {
|
||||
fn go<'a>(pat: &'a Pattern, set: &mut Vec<&'a Name>) {
|
||||
match pat {
|
||||
Pattern::Ctr(nam, pats) => {
|
||||
set.push(nam);
|
||||
pats.iter().for_each(|pat| go(pat, set));
|
||||
}
|
||||
Pattern::List(pats) => pats.iter().for_each(|pat| go(pat, set)),
|
||||
Pattern::Tup(fst, snd) => {
|
||||
go(fst, set);
|
||||
go(snd, set);
|
||||
}
|
||||
Pattern::Var(_) => {},
|
||||
Pattern::Num(_) => {}
|
||||
}
|
||||
}
|
||||
let mut set = Vec::new();
|
||||
go(self, &mut set);
|
||||
set.into_iter()
|
||||
}
|
||||
|
||||
pub fn is_detached_num_match(&self) -> bool {
|
||||
if let Pattern::Num(num) = self {
|
||||
match num {
|
||||
|
@ -81,12 +81,12 @@ impl Term {
|
||||
fst_uses || snd_uses
|
||||
}
|
||||
Term::Match { scrutinee, arms } => {
|
||||
let mut used = false;
|
||||
let scrutinee_used = scrutinee.encode_lists();
|
||||
for arm in arms {
|
||||
used |= arm.1.encode_lists();
|
||||
let mut used = scrutinee.encode_lists();
|
||||
for (pat, arm) in arms {
|
||||
used |= pat.names().chain(pat.ctrs()).any(|Name(n)| matches!(n.as_str(), LCONS | LNIL));
|
||||
used |= arm.encode_lists();
|
||||
}
|
||||
scrutinee_used || used
|
||||
used
|
||||
}
|
||||
Term::Var { nam: Name(nam) } => nam == LCONS || nam == LNIL,
|
||||
Term::Lnk { .. } | Term::Ref { .. } | Term::Num { .. } | Term::Str { .. } | Term::Era => false,
|
||||
|
@ -76,12 +76,12 @@ impl Term {
|
||||
fst_uses || snd_uses
|
||||
}
|
||||
Term::Match { arms, scrutinee } => {
|
||||
let mut used = false;
|
||||
let scrutinee_used = scrutinee.encode_str();
|
||||
for arm in arms {
|
||||
used |= arm.1.encode_str();
|
||||
let mut used = scrutinee.encode_str();
|
||||
for (pat, arm) in arms {
|
||||
used |= pat.names().chain(pat.ctrs()).any(|Name(n)| matches!(n.as_str(), SCONS | SNIL));
|
||||
used |= arm.encode_str();
|
||||
}
|
||||
scrutinee_used || used
|
||||
used
|
||||
}
|
||||
Term::List { els } => {
|
||||
let mut used = false;
|
||||
|
@ -0,0 +1,10 @@
|
||||
main = @a @b
|
||||
let a = match a {
|
||||
SCons: 1
|
||||
SNil : 2
|
||||
};
|
||||
let b = match b {
|
||||
(LCons h t): 1
|
||||
(LNil) : 2
|
||||
};
|
||||
(a, b)
|
@ -0,0 +1,33 @@
|
||||
---
|
||||
source: tests/golden_tests.rs
|
||||
input_file: tests/golden_tests/encode_pattern_match/list_str_encoding_undeclared.hvm
|
||||
---
|
||||
(main) = λa λb let a = (main$match$1 a); let b = (main$match$2 b); (a, b)
|
||||
|
||||
(LCons) = λhead λtail #List λLCons #List λLNil #List.LCons.tail (#List.LCons.head (LCons head) tail)
|
||||
|
||||
(LNil) = #List λLCons #List λLNil LNil
|
||||
|
||||
(SCons) = λhead λtail #String λSCons #String λSNil #String.SCons.tail (#String.SCons.head (SCons head) tail)
|
||||
|
||||
(SNil) = #String λSCons #String λSNil SNil
|
||||
|
||||
(main$match$1) = λx #String (x main$match$1$PSCons main$match$1$PSNil)
|
||||
|
||||
(main$match$2) = λx #List (x main$match$2$PLCons main$match$2$PLNil)
|
||||
|
||||
(main$match$1$R0) = λa.head λa.tail 1
|
||||
|
||||
(main$match$1$R1) = 2
|
||||
|
||||
(main$match$1$PSCons) = #String.SCons.head λy0 #String.SCons.tail λy1 (main$match$1$R0 * *)
|
||||
|
||||
(main$match$1$PSNil) = main$match$1$R1
|
||||
|
||||
(main$match$2$R0) = λh λt 1
|
||||
|
||||
(main$match$2$R1) = 2
|
||||
|
||||
(main$match$2$PLCons) = #List.LCons.head λy0 #List.LCons.tail λy1 (main$match$2$R0 * *)
|
||||
|
||||
(main$match$2$PLNil) = main$match$2$R1
|
Loading…
Reference in New Issue
Block a user