lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 11/29/06, John Hind <john.hind@zen.co.uk> wrote:
I'd like to be able to offer extensible infix operators so
you'd be able to write something like:

c = a func b
This would be evaluated as:
c = func(a, b)

As explained in the answers, that would be ambiguous; I proposed a more-or-less working metalua macro, allowing Haskell syntax (when you put a function between backquote `like_this`, it becomes infix, e.g. "2 `plus` 2"), which had many quirks, mainly due to then-unimplemented metalua features. Here is a properly working extension (works with metalua 0.2):

-{ block:
   local if_prec = 35 -- precedence of infix functions
   local if_expr = |lx| mlp.expr (lx, if_prec)

   mlp.expr.add_postfix_sequence{
      "`", if_expr, "`", if_expr,
      prec = if_prec, builder = |x| `Call{ x[2], x[1], x[3] } } }

This one properly handles precedence, and is left-associative. For insights of how it works, look at the "sugar.lua" sample on metalua website.