Skip to content

Commit

Permalink
[sc-441] Add char patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
developedby committed Feb 27, 2024
1 parent 39ffe05 commit 5f2d835
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 15 deletions.
8 changes: 5 additions & 3 deletions src/term/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
8 changes: 4 additions & 4 deletions tests/golden_tests/run_file/match_num_adt_tup_parser.hvm
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/compile_file__missing_pat.hvm.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 | : *
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ("+", *)))

0 comments on commit 5f2d835

Please sign in to comment.