[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: An operator syntax for bitfields using metatables
- From: Robert Virding <robert.virding@...>
- Date: Wed, 17 Jul 2013 12:32:48 +0100 (BST)
> From: "Jerome Vuarand" <jerome.vuarand@gmail.com>
>
> 2013/7/16 Robert Virding <robert.virding@erlang-solutions.com>:
> > While operators for bit-fields are very useful I think a better way
> > is a syntax which allows you describe/build the whole bitstring in
> > one go. We have this in Erlang and it makes building/pulling apart
> > protocol packets very much easier and clearer. Some simple
> > examples of the top of my head:
> >
> > 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>>
> >
> > The second one is a bit longer than 32 bits. :-) I think you get
> > the idea. We can also use the same syntax for pulling apart the
> > binary/bitstring. This really is powerful and simplifies a lot of
> > things and I think that something similar would be useful for Lua
> > if you seriously intend to work with bitfields.
>
> There are already plenty of libraries that simplify parsing such
> binary structures, some using the Lua flexible syntax as a kind of
> DSL
> to describe them. I don't see how another set of syntactic constructs
> could make it any easier or clearer. Can you elaborate on that claim?
- I make no claims to be a Lua expert, so I am not surprised to hear that there are libraries that support this type of thing.
- My comment was on the suggestion to add operator syntax for bitfields. I have done enough bitfiddling in both C and Erlang to appreciate Erlang's bitsyntax for building/pulling apart bitstrings.
- If it is something that is to be done often and speed is critical then you need some for of "compiler" support. You can either add it to the language like in Erlang or maybe have it so that your library can compile a bit pattern into some descriptive data structure or code for processing the bitstring. Erlang can do something similar to this for regular expressions.
- To be clear I wasn't suggesting using the Erlang syntax, it is to Erlangy and wouldn't fit into Lua. Although it does have a similar feel as Lua syntax for constructing tables and you could probably add some special syntax which builds a suitable table.
Robert