[antlr-interest] Operator overloading?
Andy Tripp
antlr at jazillian.com
Sat Feb 24 14:29:03 PST 2007
Vladimir Gusev wrote:
> Hello,
>
> Is there a way to implement in my language operator overloading using ANTLR?
> I would appreciate any thoughts about this.
>
> Thanks.
>
> Vladimir
>
Yes, I think this could be done. I replace C++ operator overloading with
Java
function calls as part of my C++ to Java translator (jazillian.com),
which is a little different but related.
Say you're creating a binary "+" operator between two Money objects like
this. You'd
parse the source files into ASTs, search the ASTs for binary "+"
operators, and then
figure out the types of the left and right ASTs. If they're both type
Money, then this
is the thing you're looking for, and you could replace the "+" node in
the AST
by something else, such as some METHOD_CALL type of node.
Print out your modified AST, and you've just used ANTLR as a preprocessor
to convert operator overloading to code without operator overloading. In
that way,
your "money1 + money2" expressions would become "money1.plus(money2)".
Andy
More information about the antlr-interest
mailing list