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

Add Token::Kind#unary_operator? #14342

Conversation

straight-shoota
Copy link
Member

This is a tiny refactor to merge two duplicate definitions of unary operators into a predicate method of Token::Kind.
Also simplifies the handling of unary operators in prefix notation in the formatter.

There's another similar definition in ToSVisitor::UNARY_OPERATORS but it's based on method name, not token type. This is practically not very relevant, but it's not trivial to integrate into one. So I think it's fine to keep this duplication, but I've added cross-references between the two.

@HertzDevil
Copy link
Contributor

I think the integration would just be:

module Crystal
  class ToSVisitor
    def visit(node : Not)
      visit_call Call.new(node.obj, "!")
    end
  end
end

Not the fastest code ever, but the formatter does a similar thing all over the place (e.g. as and sizeof).

@straight-shoota
Copy link
Member Author

I don't think Not is an issue with ToSVisitor. We could do what you suggested, or not and just leave it as is (which should be totally fine).

The challenge is that there's no token in ToSVisitor.

@straight-shoota straight-shoota added this to the 1.12.0 milestone Mar 5, 2024
@straight-shoota straight-shoota merged commit d93fe82 into crystal-lang:master Mar 6, 2024
57 checks passed
@straight-shoota straight-shoota deleted the refactor/token-unary_operator branch March 6, 2024 10:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants