[sc-441] Add char patterns

This commit is contained in:
Nicolas Abril 2024-02-27 18:02:04 +01:00
parent 39ffe05d72
commit 5f2d835a77
6 changed files with 17 additions and 15 deletions

View File

@ -413,16 +413,18 @@ where
})
.labelled("<Num>");
let num_ctr = num.map(|n| Pattern::Num(NumCtr::Num(n)));
let num_pat = num.map(|n| Pattern::Num(NumCtr::Num(n)));
let succ_ctr = num
let succ_pat = num
.then_ignore(just(Token::Add))
.then(name_or_era().or_not())
.map(|(num, nam)| Pattern::Num(NumCtr::Succ(num, nam)))
.labelled("<Num>+")
.boxed();
choice((succ_ctr, num_ctr, var, ctr, list, tup))
let chr_pat = select!(Token::Char(c) => Pattern::Num(NumCtr::Num(c))).labelled("<Char>").boxed();
choice((succ_pat, num_pat, chr_pat, var, ctr, list, tup))
})
}

View File

@ -1,13 +1,13 @@
// Testing various forms of pattern matching
data Result = (Ok val) | (Err err)
Parse state (String.cons 40 xs) = (Ok (40, xs, state))
Parse state (String.cons 41 xs) = (Ok (41, xs, state))
Parse state (String.cons 10 xs) = (Ok (0, xs, state))
Parse state (String.cons '(' xs) = (Ok ('(', xs, state))
Parse state (String.cons ')' xs) = (Ok (')', xs, state))
Parse state (String.cons '\n' xs) = (Ok (0, xs, state))
Parse state xs = (Err (xs, state))
main =
let str = (String.cons 40 (String.cons 50 String.nil));
let str = "(+";
let state = *;
match (Parse state str) {
(Ok (val, xs, state)): (val, (Parse state xs))

View File

@ -1,13 +1,13 @@
// Testing various forms of pattern matching
data Result = (Ok val) | (Err err)
Parse state (String.cons 40 xs) = (Ok (40, xs, state))
Parse state (String.cons 41 xs) = (Ok (41, xs, state))
Parse state (String.cons 10 xs) = (Ok (0, xs, state))
Parse state (String.cons '(' xs) = (Ok ('(', xs, state))
Parse state (String.cons ')' xs) = (Ok (')', xs, state))
Parse state (String.cons '\n' xs) = (Ok (0, xs, state))
Parse state xs = (Err (xs, state))
main =
let str = (String.cons 40 (String.cons 50 String.nil));
let str = "(+";
let state = *;
match (Parse state str) {
(Ok (val, xs, state)): (val, (Parse state xs))

View File

@ -2,5 +2,5 @@
source: tests/golden_tests.rs
input_file: tests/golden_tests/compile_file/missing_pat.hvm
---
At tests/golden_tests/compile_file/missing_pat.hvm:2:3: found ':' expected '(', '#', '$', <Name>, '[', '{', 'λ', 'let', 'match', '*', '|', <Num>+, or <Num>
At tests/golden_tests/compile_file/missing_pat.hvm:2:3: found ':' expected '(', '#', '$', <Name>, '[', '{', 'λ', 'let', 'match', '*', '|', <Num>+, <Num>, or <Char>
 2 | : *

View File

@ -5,7 +5,7 @@ input_file: tests/golden_tests/encode_pattern_match/match_num_adt_tup_parser.hvm
TaggedScott:
(Parse) = λa λb (#String (b #String.String.cons.head λc #String.String.cons.tail λd λe (match (- c 40) { 0: λf λg (Ok (40, (g, f))); 1+: λk match k { 0: λl λm (Ok (41, (m, l))); 1+: λq match (- q 18446744073709551584) { 0: λr λs (Ok (0, (s, r))); 1+: λw λy λz (Err ((String.cons (+ w 11) z), y)) } } } e d) λeb (Err (String.nil, eb))) a)
(main) = #Result ((Parse * (String.cons 40 (String.cons 50 String.nil))) #Result.Ok.val λd let (e, f) = d; (let (g, h) = f; λi (i, (Parse h g)) e) #Result.Err.err λm (Err m))
(main) = #Result ((Parse * (String.cons 40 (String.cons 43 String.nil))) #Result.Ok.val λd let (e, f) = d; (let (g, h) = f; λi (i, (Parse h g)) e) #Result.Err.err λm (Err m))
(String.cons) = λa λb #String λc #String λ* #String.String.cons.tail (#String.String.cons.head (c a) b)
@ -18,7 +18,7 @@ TaggedScott:
Scott:
(Parse) = λa λb (b λc λd λe (match (- c 40) { 0: λf λg (Ok (40, (g, f))); 1+: λk match k { 0: λl λm (Ok (41, (m, l))); 1+: λq match (- q 18446744073709551584) { 0: λr λs (Ok (0, (s, r))); 1+: λw λy λz (Err ((String.cons (+ w 11) z), y)) } } } e d) λeb (Err (String.nil, eb)) a)
(main) = (Parse * (String.cons 40 (String.cons 50 String.nil)) λd let (e, f) = d; (let (g, h) = f; λi (i, (Parse h g)) e) λm (Err m))
(main) = (Parse * (String.cons 40 (String.cons 43 String.nil)) λd let (e, f) = d; (let (g, h) = f; λi (i, (Parse h g)) e) λm (Err m))
(String.cons) = λa λb λc λ* (c a b)

View File

@ -2,4 +2,4 @@
source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/match_num_adt_tup_parser.hvm
---
(40, (Err ("2", *)))
(40, (Err ("+", *)))