lua-users home
lua-l archive

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

On 28-Sep-06, at 3:34 PM, Mike Pall wrote:

Another idea -- a meta mechanism for user extensible operators:

Pass a table with user defined operators (*) to loadstring() etc.
Compile this to OP_[LIGHT]{CONST|UN|BIN}OP, indexing a table of
(light) functions held in the Lua function prototype. I guess
this would be very fast.

Yeah, I have something very similar to that kicking around, but it's a slightly different protocol, to avoid the issue with dump.

The operands I defined were:

  OP_BINOP  ra, rkb, rkc
  OP_UNOP   ra, rkb, 0
  OP_GENOP2 0, K

Both OP_BINOP and OP_UNOP must be followed by an OP_GENOP2; the K in the OP_GENOP2 is the index of a string which is used to look the definition up in the metatable of (b or c), as with Arithmetic operators (I considered adding alternative ones for the < and == metarestrictions but it seemed too complicated.)

Then the parser is extended with:

  local operator [binary|unary] <token> as <identifier>

for <token>s which would otherwise be valid identifiers, this follows normal scope visibility rules.

It seems to provide quite a reasonable extension mechanism. I'll dig out the implementation and share it if anyone is interested.