-
Notifications
You must be signed in to change notification settings - Fork 503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(syntax): add match operator #2267
base: master
Are you sure you want to change the base?
Conversation
This commit adds a `match` expression, similar to the construct in the Rust programming language. An example of the match statement follows: ``` match true { true => "yes", false => "no", } ```
I'm confused. The original request in #1990 appeared to be asking for something like
Which I agree would be nicer than the equivalent chain of if statements. Looking at the syntax example and the tests in this PR, looks like this implementation of This implementation appears to be introducing a boolean type, which in |
Yah, I thnk @laniakea64 is right, matching on strings is most useful, since for bools there's if / else. To keep the initial feature simple, I would skip bothering to try to bind fallthrough matches to a variable, and require a match arm with
Also for simplicity, all match arms should be required to be unique. A follow-up PR can allow actually matching and binding in the fall-through case. |
Should the |
Ah so maybe my original request wasn't clear! I originally was hoping this would be a generic As a response to @laniakea64 : I'm not sure I share the opinion that everything being a string is a feature of Just (is it? I've never even thought of this, but I guess it is true!) -- but I wonder why limit this to working on strings when it can work on expressions?
What else would match be? I didn't meant to implement it as an alias for if statements (maybe I did something terribly wrong), but that's what I don't intend to add any fancy stuff like destructuring or anything of course
Uhh I don't think that's what this is doing -- Maybe this is a sign that the comment I put in to describe this PR is incredibly bad -- this introduces a new kind of expression, which is the It's not like I actually added a thing that does As a response to @casey :
So the bool was just the simplest expression to write -- but I do think it's more powerful to bind on all expressions, and I don't think it takes that much more work than matching on strings.
Ah so this is actually what the impl looks like -- there is a fallthrough for matches (and it's Am I misunderstanding your suggestion maybe?
This is new, and a good suggestion -- will add this!
I'm a little bit confused on how these would actually be separated -- maybe the approach I'm taking here is just so off-course compared to the simplest approach that people were envisioning. How would you start the simple approach? I just assumed it would be adding an |
IMO it definitely should regardless! We can make sure that |
Also thanks for the feedback ya'll -- I actually have some bugs that I was stuck on so more eyes (on how to actually fix the lexer issues) would be great --- tests don't pass in the PR as is 😞 |
Not an opinion, casey has said this many times before about current
Sorry for the unclear wording. Ideally,
The |
@t3hmrman I investigated this and decided to try the following change, which fixed the lexer test failure, but caused different test failure diff --git a/src/lexer.rs b/src/lexer.rs
index 853e41f..6520d7b 100644
--- a/src/lexer.rs
+++ b/src/lexer.rs
@@ -486,7 +486,7 @@ impl<'src> Lexer<'src> {
',' => self.lex_single(Comma),
'/' => self.lex_single(Slash),
':' => self.lex_colon(),
- '=' => self.lex_choices('=', &[('=', EqualsEquals), ('~', EqualsTilde)], Equals),
+ '=' => self.lex_choices('=', &[('=', EqualsEquals), ('~', EqualsTilde), ('>', EqualsGreaterThan)], Equals),
'?' => self.lex_single(QuestionMark),
'@' => self.lex_single(At),
'[' => self.lex_delimiter(BracketL),
diff --git a/src/parser.rs b/src/parser.rs
index 5b8df2d..b21accf 100644
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -2154,8 +2154,8 @@ mod tests {
test! {
name: _match,
- text: "a := match b == c { true => d, false => e }",
- tree: (justfile (assignment a (match a == b true d false e))),
+ text: "a := match b { c => d, e => f }",
+ tree: (justfile (assignment a (match b c d e f))),
}
test! { The reason for changing the test in |
Thanks @laniakea64 ! I'm going to add that to this PR -- I certainly missed that part of the lexer implementation for lexing things that look like |
This commit adds a
match
expression, similar to the construct in theRust programming language.
An example of the match statement follows: