[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: An operator syntax for bitfields using metatables
- From: Todd Coram <todd@...>
- Date: Tue, 16 Jul 2013 07:42:09 -0400
On Mon, Jul 15, 2013, at 09:58 PM, Luiz Henrique de Figueiredo wrote:
> > 64-bit IEEE floating point:
> > <<Sign:1,Exponent:11,Mantissa:52>>
> > IP datagram:
> > <<?IP_VERSION:4, HLen:4, SrvcType:8, TotLen:16,
> > ID:16, Flgs:3, FragOff:13,
> > TTL:8, Proto:8, HdrChkSum:16,
> > SrcIP:32, DestIP:32, RestDgram/binary>>
> Given these strings it easy to parse them into a Lua table containing the
> names and starting and end points of the bitfields described there. That
> table can then be used to set index and newindex metamethods for get and
> put the bitfields using Lua syntax, or perhaps pack and unpack functions:
> template = "<<Sign:1,Exponent:11,Mantissa:52>>"
> name = "64-bit IEEE floating point"
> b = bitfelds.parse(name,template)
> s = io.read(file,b.size)
> t = b:unpack(s)
> s = b:pack(t)
Erlang's bit syntax is a joy to work with and I miss it every time I use
another programming language to decode protocols.
In particular, I like the inline binding to variables as opposed to the
regex/scanf like approach taken by other languages.
An efficient analogue (to Erlang's bit syntax) in Lua would be
Lua tables would work great for something like this. :)