  lua-l archive

• Subject: Re: Function calls [was: Lua and Neko comparison]
• From: David Jones <drj@...>
• Date: Fri, 29 Sep 2006 12:48:39 +0100

```
On 28 Sep 2006, at 21:34, 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.

Slight complication: for (un)dumping to/from bytecode you need to
pass the same table of user defined operators.

(*) Name, arity, precedence, (light) function, optional type
checks, optional constant folding.

Simplified, hypothetical example:

local userops = {
PI =     { arity = 0, op = math.pi }, -- arity 0 are constants
sin =    { arity = 1, op = math.sin },
cos =    { arity = 1, op = math.cos },
["|"] =  { arity = 2, op = bit.bor },
["<<"] = { arity = 2, op = bit.lshift },
```
["//"] = { arity = 2, op = function(a, b) return math.floor(a/ b) end },
```  } -- These should of course better be light functions.

local a, b = ...

print( PI * sin a + cos b )
print( (a << 11) | (b << 3) | 5 )
print( "Rici buys", 1.38 // 0.16, "apples." )

]], nil, userops)(...)

[Yes, of course this works with strings or any other type, too.]
```
```
```
Sorry Mike, in another message a credited all this stuff to Rici Lake for some reason.
```
```
I think this is very interesting and I've been meaning to implement something almost exactly like this and release it so that the "we need more operators" crowd could play around with it.
```
```
It wouldn't be too hard imagine having simple operators, like "|", be bound to one of the unused opcodes, and having a user-patchable table in the VM of "light functions" that implemented each "user-defined" opcode.
```
drj

```