case when to case is

This commit is contained in:
Chad Stearns 2019-12-23 17:08:53 -05:00
parent aaae923e59
commit 9a5b6a03b4
10 changed files with 31 additions and 28 deletions

View file

@ -69,7 +69,7 @@ interface Float
#round : Float -> Int #round : Float -> Int
round = \num -> round = \num ->
case num when case num is
0.0 -> 0 0.0 -> 0
_ -> 1 _ -> 1
@ -110,7 +110,7 @@ round = \num ->
## >>> |> Float.div 2.0 ## >>> |> Float.div 2.0
#div : Float, Float -> Result Float DivByZero #div : Float, Float -> Result Float DivByZero
div = \numerator, denominator -> div = \numerator, denominator ->
case numerator when case numerator is
0.0 -> 0.0 # TODO return Result! 0.0 -> 0.0 # TODO return Result!
_ -> denominator _ -> denominator

View file

@ -133,9 +133,10 @@ pub fn fmt_expr<'a>(
fmt_if(buf, loc_condition, loc_then, loc_else, indent); fmt_if(buf, loc_condition, loc_then, loc_else, indent);
} }
Case(loc_condition, branches) => { Case(loc_condition, branches) => {
buf.push_str("case "); buf.push_str("\
case ");
fmt_expr(buf, &loc_condition.value, indent, false, true); fmt_expr(buf, &loc_condition.value, indent, false, true);
buf.push_str(" when\n"); buf.push_str(" is\n");
let mut it = branches.iter().peekable(); let mut it = branches.iter().peekable();
while let Some((pattern, expr)) = it.next() { while let Some((pattern, expr)) = it.next() {

View file

@ -4,3 +4,4 @@ pub static ELSE: &str = "else";
pub static CASE: &str = "case"; pub static CASE: &str = "case";
pub static WHEN: &str = "when"; pub static WHEN: &str = "when";
pub static AS: &str = "as"; pub static AS: &str = "as";
pub static IS: &str = "is";

View file

@ -636,6 +636,7 @@ fn reserved_keyword<'a>() -> impl Parser<'a, ()> {
string(keyword::ELSE), string(keyword::ELSE),
string(keyword::CASE), string(keyword::CASE),
string(keyword::WHEN), string(keyword::WHEN),
string(keyword::IS),
string(keyword::AS) string(keyword::AS)
) )
} }
@ -778,13 +779,13 @@ pub fn case_expr<'a>(min_indent: u16) -> impl Parser<'a, Expr<'a>> {
loc!(move |arena, state| parse_expr(min_indent, arena, state)), loc!(move |arena, state| parse_expr(min_indent, arena, state)),
min_indent, min_indent,
), ),
string(keyword::WHEN) string(keyword::IS)
) )
) )
), ),
move |arena, state, (case_indent, loc_condition)| { move |arena, state, (case_indent, loc_condition)| {
if case_indent < min_indent { if case_indent < min_indent {
panic!("TODO case wasns't indented enough"); panic!("TODO case wasn't indented enough");
} }
// Everything in the branches must be indented at least as much as the case itself. // Everything in the branches must be indented at least as much as the case itself.

View file

@ -249,17 +249,17 @@ mod test_canonicalize {
let src = indoc!( let src = indoc!(
r#" r#"
g = \x -> g = \x ->
case x when case x is
0 -> 0 0 -> 0
_ -> g (x - 1) _ -> g (x - 1)
h = \x -> h = \x ->
case x when case x is
0 -> 0 0 -> 0
_ -> g (x - 1) _ -> g (x - 1)
p = \x -> p = \x ->
case x when case x is
0 -> 0 0 -> 0
1 -> g (x - 1) 1 -> g (x - 1)
_ -> p (x - 1) _ -> p (x - 1)
@ -288,7 +288,7 @@ mod test_canonicalize {
let src = indoc!( let src = indoc!(
r#" r#"
g = \x -> g = \x ->
case x when case x is
0 -> 0 0 -> 0
_ -> g (x + 1) _ -> g (x + 1)
@ -327,7 +327,7 @@ mod test_canonicalize {
let src = indoc!( let src = indoc!(
r#" r#"
q = \x -> q = \x ->
case q x when case q x is
_ -> 0 _ -> 0
0 0
@ -348,12 +348,12 @@ mod test_canonicalize {
let src = indoc!( let src = indoc!(
r#" r#"
q = \x -> q = \x ->
case x when case x is
0 -> 0 0 -> 0
_ -> p (x - 1) _ -> p (x - 1)
p = \x -> p = \x ->
case x when case x is
0 -> 0 0 -> 0
_ -> q (x - 1) _ -> q (x - 1)

View file

@ -87,7 +87,7 @@ mod test_gen {
assert_evals_to!( assert_evals_to!(
indoc!( indoc!(
r#" r#"
case 1 when case 1 is
1 -> 12 1 -> 12
_ -> 34 _ -> 34
"# "#
@ -102,7 +102,7 @@ mod test_gen {
assert_evals_to!( assert_evals_to!(
indoc!( indoc!(
r#" r#"
case 2 when case 2 is
1 -> 63 1 -> 63
_ -> 48 _ -> 48
"# "#

View file

@ -695,7 +695,7 @@ mod test_format {
fn integer_case() { fn integer_case() {
expr_formats_same(indoc!( expr_formats_same(indoc!(
r#" r#"
case b when case b is
1 -> 1 ->
1 1
@ -710,7 +710,7 @@ mod test_format {
expr_formats_to( expr_formats_to(
indoc!( indoc!(
r#" r#"
case year when case year is
1999 -> 1999 ->
@ -725,7 +725,7 @@ mod test_format {
), ),
indoc!( indoc!(
r#" r#"
case year when case year is
1999 -> 1999 ->
1 1
@ -740,7 +740,7 @@ mod test_format {
fn case_with_comments() { fn case_with_comments() {
expr_formats_same(indoc!( expr_formats_same(indoc!(
r#" r#"
case b when case b is
# look at cases # look at cases
1 -> 1 ->
# case 1 # case 1
@ -761,9 +761,9 @@ mod test_format {
fn nested_case() { fn nested_case() {
expr_formats_same(indoc!( expr_formats_same(indoc!(
r#" r#"
case b when case b is
_ -> _ ->
case c when case c is
_ -> _ ->
1 1
"# "#
@ -775,7 +775,7 @@ mod test_format {
expr_formats_to( expr_formats_to(
indoc!( indoc!(
r#" r#"
case b when case b is
1 -> 1 ->
1 # case 1 1 # case 1
@ -786,7 +786,7 @@ mod test_format {
), ),
indoc!( indoc!(
r#" r#"
case b when case b is
1 -> 1 ->
1 1

View file

@ -833,7 +833,7 @@ mod test_infer {
infer_eq( infer_eq(
indoc!( indoc!(
r#" r#"
case 1 when case 1 is
1 -> 2 1 -> 2
3 -> 4 3 -> 4
"# "#

View file

@ -1322,7 +1322,7 @@ mod test_parse {
&arena, &arena,
indoc!( indoc!(
r#" r#"
case x when case x is
"blah" -> 1 "blah" -> 1
"mise" -> 2 "mise" -> 2
"# "#
@ -1356,7 +1356,7 @@ mod test_parse {
&arena, &arena,
indoc!( indoc!(
r#" r#"
case x when case x is
1 -> 2 1 -> 2
3 -> 4 3 -> 4
"# "#
@ -1396,7 +1396,7 @@ mod test_parse {
&arena, &arena,
indoc!( indoc!(
r#" r#"
case x when case x is
{ y } -> 2 { y } -> 2
{ z, w } -> 4 { z, w } -> 4
"# "#

View file

@ -863,7 +863,7 @@ mod test_infer_uniq {
infer_eq( infer_eq(
indoc!( indoc!(
r#" r#"
case 1 when case 1 is
1 -> 2 1 -> 2
3 -> 4 3 -> 4
"# "#