Skip to content
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

Support pattern matching on constants #3641

Merged
merged 6 commits into from
Aug 12, 2022

Conversation

hubertp
Copy link
Collaborator

@hubertp hubertp commented Aug 10, 2022

Pull Request Description

This change adds support for matching on constants by:

  1. extending parser to allow literals in patterns
  2. generate branch node for literals

Related to https://www.pivotaltracker.com/story/show/182743559

Checklist

Please include the following checklist in your PR:

  • The documentation has been updated if necessary.
  • All code conforms to the
    Scala,
    Java,
    and
    Rust
    style guides.
  • All code has been tested:
    • Unit tests have been written where possible.
    • If GUI codebase was changed: Enso GUI was tested when built using BOTH
      ./run ide build and ./run ide watch.

@hubertp hubertp marked this pull request as ready for review August 10, 2022 16:20
Copy link
Member

@radeusgd radeusgd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! I can't wait to use it 😃

Copy link
Member

@JaroslavTulach JaroslavTulach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work in a really fast time!

Object state,
Text target,
@Cached("build()") ToJavaStringNode toJavaStringNode) {
if (textProfile.profile(literal.equals(toJavaStringNode.execute(target))))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converting Text to String just to do equals? I guess we could benefit from a switch to TruffleStrings then and use EqualsNode which would compare the values without converting to String.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is the only ugly part in this PR, I admit.
Thanks for the suggestion, I wasn't entirely sure what is the proper way to deal with this.

Copy link
Collaborator Author

@hubertp hubertp Aug 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the other hand, we use similar technique already with ExpectStringNode, ExpectTextNode (and ToJavaStringNode) in various places.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I think that with a comparison of normalized strings this is no longer an option.

test/Tests/src/Semantic/Case_Spec.enso Show resolved Hide resolved
@hubertp hubertp force-pushed the wip/hubert/pattern-matching-constants-182743559 branch from b12eaf3 to 2c8c8bb Compare August 11, 2022 16:05
This change adds support for matching on constants by:
1) extending parser to allow literals in patterns
2) generate branch node for literals
Added tests for matching on literals that are deeply nested. Turned out
the initial setup was wrong but was able to reuse the existing logic.

Addressed the first of the comments in PR
Split LiteralBranchNode into comparing strings and numeric values. That
way we can ensure that LHS is a String and avoid casting.
@hubertp hubertp force-pushed the wip/hubert/pattern-matching-constants-182743559 branch from 2c8c8bb to 275b43f Compare August 12, 2022 08:43
@hubertp hubertp force-pushed the wip/hubert/pattern-matching-constants-182743559 branch from cf00ecc to abeccec Compare August 12, 2022 09:59
@hubertp hubertp added the CI: Ready to merge This PR is eligible for automatic merge label Aug 12, 2022
@mergify mergify bot merged commit 8575b76 into develop Aug 12, 2022
@mergify mergify bot deleted the wip/hubert/pattern-matching-constants-182743559 branch August 12, 2022 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI: Ready to merge This PR is eligible for automatic merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants