Skip to content

Add Token::Kind#unary_operator?#14342

Merged
straight-shoota merged 1 commit intocrystal-lang:masterfrom
straight-shoota:refactor/token-unary_operator
Mar 6, 2024
Merged

Add Token::Kind#unary_operator?#14342
straight-shoota merged 1 commit intocrystal-lang:masterfrom
straight-shoota:refactor/token-unary_operator

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
@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