Skip to content

Introduce a match-case construct to Ion's grammar

Michael Aaron Murphy requested to merge huntergoldstein:here_be_matching into master

Created by: huntergoldstein

Problem: In order for a user to examine an expression and act based on its value, one would have to write a set of nested if statements such as:

let v = ...
if test v -eq "foo"
...
else if test v -eq "bar"
...
else
...
end

Solution: Introduce a match construct that works like a case-switch statement in Bash or some C-like language:

match ...
  case foo; ... ; end
  case bar; ... ; end
  case _ ; ... ; end
end

Changes introduced by this pull request:

  • Adds two new statement variants: Match and Case as well as a Case struct
  • Update FlowLogic to require an execute_match function and add associated logic (collecting cases, ensuring that a Match block is complete / incomplete).

TODOs:

  • Need to add unit and integration tests; @mmstick is there a good place to add these?
  • Syntax extensions (if needed)
    • Allow variable binding to default case: case _ as foo; echo $foo ; end
    • Alternation cases, so something like: case "bar" | "baz" | ...
    • Non constant cases? case $(echo file | ... | ...)

Fixes: Closes #273 (closed)

State: WIP: see TODOs, though the syntax extensions can be moved into another issue if need be, this is a pretty hefty PR.

Merge request reports