mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-03 08:34:33 +00:00
case when to case is
This commit is contained in:
parent
aaae923e59
commit
9a5b6a03b4
10 changed files with 31 additions and 28 deletions
|
@ -69,7 +69,7 @@ interface Float
|
|||
|
||||
#round : Float -> Int
|
||||
round = \num ->
|
||||
case num when
|
||||
case num is
|
||||
0.0 -> 0
|
||||
_ -> 1
|
||||
|
||||
|
@ -110,7 +110,7 @@ round = \num ->
|
|||
## >>> |> Float.div 2.0
|
||||
#div : Float, Float -> Result Float DivByZero
|
||||
div = \numerator, denominator ->
|
||||
case numerator when
|
||||
case numerator is
|
||||
0.0 -> 0.0 # TODO return Result!
|
||||
_ -> denominator
|
||||
|
||||
|
|
|
@ -133,9 +133,10 @@ pub fn fmt_expr<'a>(
|
|||
fmt_if(buf, loc_condition, loc_then, loc_else, indent);
|
||||
}
|
||||
Case(loc_condition, branches) => {
|
||||
buf.push_str("case ");
|
||||
buf.push_str("\
|
||||
case ");
|
||||
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();
|
||||
while let Some((pattern, expr)) = it.next() {
|
||||
|
|
|
@ -4,3 +4,4 @@ pub static ELSE: &str = "else";
|
|||
pub static CASE: &str = "case";
|
||||
pub static WHEN: &str = "when";
|
||||
pub static AS: &str = "as";
|
||||
pub static IS: &str = "is";
|
|
@ -636,6 +636,7 @@ fn reserved_keyword<'a>() -> impl Parser<'a, ()> {
|
|||
string(keyword::ELSE),
|
||||
string(keyword::CASE),
|
||||
string(keyword::WHEN),
|
||||
string(keyword::IS),
|
||||
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)),
|
||||
min_indent,
|
||||
),
|
||||
string(keyword::WHEN)
|
||||
string(keyword::IS)
|
||||
)
|
||||
)
|
||||
),
|
||||
move |arena, state, (case_indent, loc_condition)| {
|
||||
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.
|
||||
|
|
|
@ -249,17 +249,17 @@ mod test_canonicalize {
|
|||
let src = indoc!(
|
||||
r#"
|
||||
g = \x ->
|
||||
case x when
|
||||
case x is
|
||||
0 -> 0
|
||||
_ -> g (x - 1)
|
||||
|
||||
h = \x ->
|
||||
case x when
|
||||
case x is
|
||||
0 -> 0
|
||||
_ -> g (x - 1)
|
||||
|
||||
p = \x ->
|
||||
case x when
|
||||
case x is
|
||||
0 -> 0
|
||||
1 -> g (x - 1)
|
||||
_ -> p (x - 1)
|
||||
|
@ -288,7 +288,7 @@ mod test_canonicalize {
|
|||
let src = indoc!(
|
||||
r#"
|
||||
g = \x ->
|
||||
case x when
|
||||
case x is
|
||||
0 -> 0
|
||||
_ -> g (x + 1)
|
||||
|
||||
|
@ -327,7 +327,7 @@ mod test_canonicalize {
|
|||
let src = indoc!(
|
||||
r#"
|
||||
q = \x ->
|
||||
case q x when
|
||||
case q x is
|
||||
_ -> 0
|
||||
|
||||
0
|
||||
|
@ -348,12 +348,12 @@ mod test_canonicalize {
|
|||
let src = indoc!(
|
||||
r#"
|
||||
q = \x ->
|
||||
case x when
|
||||
case x is
|
||||
0 -> 0
|
||||
_ -> p (x - 1)
|
||||
|
||||
p = \x ->
|
||||
case x when
|
||||
case x is
|
||||
0 -> 0
|
||||
_ -> q (x - 1)
|
||||
|
||||
|
|
|
@ -87,7 +87,7 @@ mod test_gen {
|
|||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
case 1 when
|
||||
case 1 is
|
||||
1 -> 12
|
||||
_ -> 34
|
||||
"#
|
||||
|
@ -102,7 +102,7 @@ mod test_gen {
|
|||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
case 2 when
|
||||
case 2 is
|
||||
1 -> 63
|
||||
_ -> 48
|
||||
"#
|
||||
|
|
|
@ -695,7 +695,7 @@ mod test_format {
|
|||
fn integer_case() {
|
||||
expr_formats_same(indoc!(
|
||||
r#"
|
||||
case b when
|
||||
case b is
|
||||
1 ->
|
||||
1
|
||||
|
||||
|
@ -710,7 +710,7 @@ mod test_format {
|
|||
expr_formats_to(
|
||||
indoc!(
|
||||
r#"
|
||||
case year when
|
||||
case year is
|
||||
1999 ->
|
||||
|
||||
|
||||
|
@ -725,7 +725,7 @@ mod test_format {
|
|||
),
|
||||
indoc!(
|
||||
r#"
|
||||
case year when
|
||||
case year is
|
||||
1999 ->
|
||||
1
|
||||
|
||||
|
@ -740,7 +740,7 @@ mod test_format {
|
|||
fn case_with_comments() {
|
||||
expr_formats_same(indoc!(
|
||||
r#"
|
||||
case b when
|
||||
case b is
|
||||
# look at cases
|
||||
1 ->
|
||||
# case 1
|
||||
|
@ -761,9 +761,9 @@ mod test_format {
|
|||
fn nested_case() {
|
||||
expr_formats_same(indoc!(
|
||||
r#"
|
||||
case b when
|
||||
case b is
|
||||
_ ->
|
||||
case c when
|
||||
case c is
|
||||
_ ->
|
||||
1
|
||||
"#
|
||||
|
@ -775,7 +775,7 @@ mod test_format {
|
|||
expr_formats_to(
|
||||
indoc!(
|
||||
r#"
|
||||
case b when
|
||||
case b is
|
||||
1 ->
|
||||
1 # case 1
|
||||
|
||||
|
@ -786,7 +786,7 @@ mod test_format {
|
|||
),
|
||||
indoc!(
|
||||
r#"
|
||||
case b when
|
||||
case b is
|
||||
1 ->
|
||||
1
|
||||
|
||||
|
|
|
@ -833,7 +833,7 @@ mod test_infer {
|
|||
infer_eq(
|
||||
indoc!(
|
||||
r#"
|
||||
case 1 when
|
||||
case 1 is
|
||||
1 -> 2
|
||||
3 -> 4
|
||||
"#
|
||||
|
|
|
@ -1322,7 +1322,7 @@ mod test_parse {
|
|||
&arena,
|
||||
indoc!(
|
||||
r#"
|
||||
case x when
|
||||
case x is
|
||||
"blah" -> 1
|
||||
"mise" -> 2
|
||||
"#
|
||||
|
@ -1356,7 +1356,7 @@ mod test_parse {
|
|||
&arena,
|
||||
indoc!(
|
||||
r#"
|
||||
case x when
|
||||
case x is
|
||||
1 -> 2
|
||||
3 -> 4
|
||||
"#
|
||||
|
@ -1396,7 +1396,7 @@ mod test_parse {
|
|||
&arena,
|
||||
indoc!(
|
||||
r#"
|
||||
case x when
|
||||
case x is
|
||||
{ y } -> 2
|
||||
{ z, w } -> 4
|
||||
"#
|
||||
|
|
|
@ -863,7 +863,7 @@ mod test_infer_uniq {
|
|||
infer_eq(
|
||||
indoc!(
|
||||
r#"
|
||||
case 1 when
|
||||
case 1 is
|
||||
1 -> 2
|
||||
3 -> 4
|
||||
"#
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue