Skip to content

Commit

Permalink
[sc-441] Fix encoding of string patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
developedby committed Feb 28, 2024
1 parent 7b859fc commit fb8f3fe
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/term/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ impl Pattern {
}

fn encode_str(str: &str) -> Pattern {
let lnil = Pattern::Ctr(Name::from(LNIL), vec![]);
let lnil = Pattern::Ctr(Name::from(SNIL), vec![]);

str.chars().rfold(lnil, |tail, head| {
let head = Pattern::Num(NumCtr::Num(head as u64));
Pattern::Ctr(Name::from(LCONS), vec![head, tail])
Pattern::Ctr(Name::from(SCONS), vec![head, tail])
})
}
}
9 changes: 9 additions & 0 deletions tests/golden_tests/run_file/match_str.hvm
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(is_as "As") = 2
(is_as "as") = 2
(is_as "") = 1
(is_as *) = 0

map f (List.cons x xs) = (List.cons (f x) (map f xs))
map f [] = []

main = (map is_as ["As" "as" "" "Asd" "qwerty" "AAs"])
5 changes: 5 additions & 0 deletions tests/snapshots/run_file__match_str.hvm.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/match_str.hvm
---
[2, 2, 1, 0, 0, 0]
2 changes: 1 addition & 1 deletion tests/snapshots/simplify_matches__match_str.hvm.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
source: tests/golden_tests.rs
input_file: tests/golden_tests/simplify_matches/match_str.hvm
---
(is_as) = λa match a { (List.cons b c): (match b { 65: λd (match d { (List.cons f g): λh (match f { 115: λ* λj (match j { (List.cons * *): λ* 0; (List.nil): λ* 2 } *); *: λ* λ* 0 } h g); (List.nil): λ* 0 } *); 97: λhb (match hb { (List.cons jb kb): λlb (match jb { 115: λ* λnb (match nb { (List.cons * *): λ* 0; (List.nil): λ* 2 } *); *: λ* λ* 0 } lb kb); (List.nil): λ* 0 } *); *: λ* 0 } c); (List.nil): 1 }
(is_as) = λa match a { (String.cons b c): (match b { 65: λd (match d { (String.cons f g): λh (match f { 115: λ* λj (match j { (String.cons * *): λ* 0; (String.nil): λ* 2 } *); *: λ* λ* 0 } h g); (String.nil): λ* 0 } *); 97: λhb (match hb { (String.cons jb kb): λlb (match jb { 115: λ* λnb (match nb { (String.cons * *): λ* 0; (String.nil): λ* 2 } *); *: λ* λ* 0 } lb kb); (String.nil): λ* 0 } *); *: λ* 0 } c); (String.nil): 1 }

(main) = *

0 comments on commit fb8f3fe

Please sign in to comment.