Implementing operator overloading for e.g. +
(plus) isn't terribly difficult if you know it's a binary operator. One can just parse expression
+
expression
But what if the programmer can choose whether +
is binary, unary (prefix/postfix), part of a ternary, or something else? E.g.
.. ? .. : ..
..²
.. | .. | .. | .. | ..
.. 大 .. 小 ..
不 ..
The problem seems to be that the arity should be known at parsing time, but it isn't, it's only known after the whole compiling/transpiling.
Some ideas:
- I think the whole problem kind of goes away by switching to Lisp-like syntax (not familiar enough with Lisp yet), but I'm gonna make it difficult by saying the syntax has to be somewhat C-like as in the examples.
- Some kind of restriction like having overloads at the top / in a separate file so they can be compiled first.
- (Maybe Arity must be fixed for each symbol - doesn't really answer the question though).
Is there a way that isn't terrible?
Does it become more possible by assuming operators can be distinguished from other identifiers at the parsing phase (e.g. other identifiers are alphanumeric)?