lua-users home
lua-l archive

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


2011/11/4 HyperHacker <hyperhacker@gmail.com>:
> On Thu, Nov 3, 2011 at 18:31, Luiz Henrique de Figueiredo
> <lhf@tecgraf.puc-rio.br> wrote:
>>> Trying to port it to using the bit library is very error-prone
>>
>> Perhaps there is a niche for a tool that converts infix bitwise
>> expressions in whatever syntax you prefer to Lua code using the bit
>> library. It's probably quite easy to write this tool with LPEG.
>>
>>
>
> I've thought about doing similar: a function that just parses a string
> like "x << (y | z)" and interprets it at runtime. Your idea though has
> the advantage that you don't have to do any variable interpolation and
> the parsing only needs to be done once.
>
> --
> Sent from my toaster.
>
>

It's very easy with metalua[1], see below:

-{block:
    mlp.expr.infix:add { '&', prec = 35, builder = |a,_,b| +{
bit.band(-{a}, -{b}) } }
    mlp.expr.infix:add { '|', prec = 36, builder = |a,_,b| +{
bit.bor(-{a}, -{b}) } }
    mlp.expr.prefix:add { '!', prec = 80, builder = |_,a| +{ bit.bnot(-{a}) } }
}


require 'bit'

print( 0xF0 & 0xAA )


you can product bytecode file with metalua, without parse it runtime.

[1]: http://metalua.luaforge.net/