User-defined operators are similar to the C++ concept of operator overloading, where a default definition of an operator is altered to operate on a wide variety of objects. Typically, operators are unary or binary operators. Implementing binary operator overloading is easier with the existing infrastructure. Unary operators need some additional code to handle. First, binary operator overloading will be defined, and then unary operator overloading will be looked into.
The first part is to define a binary operator for overloading. The logical OR operator (
|) is a good example to start with. The
| operator in our TOY language can be used as follows:
def binary | (LHS RHS) if LHS then 1 ...