Introduce a match-case construct to Ion's grammar
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
andCase
as well as aCase
struct - Update
FlowLogic
to require anexecute_match
function and add associated logic (collecting cases, ensuring that aMatch
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.