[IronPython] Adding new infix operators to IronPython

Dino Viehland dinov at microsoft.com
Mon Feb 15 20:43:28 CET 2010

Hopefully updating the tokenizer and parser should be fairly obvious.  Once you've done that you'll need to actually implement the operators themselves.  If the implementation of these operators is effectively exactly the same as the normal binary operators it should be pretty easy - you just need to add a new value to PythonOperationKind and PythonOperator, update Symbols.Generated.cs so it maps to the __*__ methods, and update BinaryExpression so PythonOperatorToAction maps between PythonOperator and PythonOperationKind.  By "exactly the same" I mean try __*__ on the LHS if RHS is not a subclass of RHS, __r*__ on the RHS, check for not-implemented, support coercion, etc...

If you wanted to define your unique behavior for the operations you could just change BinaryExpression.cs to generate whatever code you want.  That could even be as simple as calling into a method in PythonOps that figures out what to do.  Or you could create a new DynamicMetaObjectBinder subclass which does some custom binding which gets cached and runs fast.  If you want to go that for you should take a look at some of the existing binders in IronPython.Runtime.Binding.  The PythonProtocol class has the implementation of binary operators, unary operators, and indexing - I'd suggest looking at unary as they're the simplest (binary's quite complex, but hopefully you don't need to go this far).

From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Tristan Zajonc
Sent: Monday, February 15, 2010 11:24 AM
To: users at lists.ironpython.com
Subject: [IronPython] Adding new infix operators to IronPython


I'm interested in experimenting with adding new infix operators to IronPython to support objectwise and elementwise operators. PEP 225
(http://www.python.org/dev/peps/pep-0225/) describes the potential use cases for such operators. Matlab style matrix operations are the chief example.  Clearly new operators won't be added to Python any time soon, but I am interested in exploring the issue nonetheless.

Before diving in, I am wondering if there are any helpful pointers more experienced IronPython developers can offer about adding new syntax to IronPython.  Is there an overall strategy I should obviously be following?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20100215/39b9d0b3/attachment.html>

More information about the Ironpython-users mailing list