Skip to content

pattern-match and, and, or #584

Description

@igl

Simple match test with expression in case:

match 1, 3, 3
| 1, 1, 2 or 3 => ok 0
| 1, 2 or 3, 3 => ok 1
| _            => ok 0

Compiles to:

// Generated by LiveScript 1.3.1
var ref$;
switch (ref$ = [1, 3, 3], false) {
case !(1 === ref$[0] && 1 === ref$[1] && 2 === ref$[2] || 3 === ref$[2]):
  ok(0);
  break;
case !(1 === ref$[0] && 2 === ref$[1] || 3 === ref$[1] && 3 === ref$[2]):
  ok(1);
  break;
default:
  ok(0);
}

The problem in words: Every case-expression is bundled in one long expression.
It should wrap braces around each column so the precedence for each case is preserved.

1 === ref$[1] && 2 === ref$[2] || 3 === ref$[2]
^---- 1 ----^ && ^------------ 2 -------------^ # yay
^------------ 1 -------------^ || ^---- 2 ----^ # nay

Correct output:

(1 === ref$[1]) && (2 === ref$[2] || 3 === ref$[2])

Activity

  1. igl commented on Apr 9, 2015

    @igl
    ContributorAuthor

    Updated OP as it was a little.. plain.
    I fiddled with ast.Case.compileCase but without much luck. I don't fully understand the code to approach this problem :/

  2. phanimahesh commented on Jun 17, 2015

    @phanimahesh

    I don't think LS should add parenthesis by default, but if it is the most commonly expected use case, it makes sense to do it.

  3. igl commented on Jun 17, 2015

    @igl
    ContributorAuthor

    Well if we want to keep the meaning of , to end a expression they should be there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions