On 03/25/2010 06:21 PM, Walter Bright wrote:
Ellery Newcomer wrote:
What do you think of Erlang's bit syntax if you've looked at it, or
could you if you haven't?

I know nothing about it.

I suppose you could think of it as sort of a regex-for-generic-data, but you can use it as a pattern matcher or a data constructor.

The example Armstrong gives in Programming Erlang is

<<2#11111111111:11,B:2,C:2,_D:1,E:4,F:2,G:1,Bits:9>> = X

where X is assumedly a byte array or something.

the first 11 bits are asserted to be 1, and the remaining bits get associated with the following variables.

Yeah, that example isn't much more impressive than bitfields.

You could do something like

<<N:32, Data:N/binary, _/binary>> = X

which would interpret the first four bytes as a length field, bind the next N bytes to Data, and ignore the rest..

The general syntax for them should look something like

Bit:   << >>
       << E >>

E:     E , E1
       E1

E1:    Value
       Value : Size
       Value / TypeSpecList
       Value : Size / TypeSpecList

//integral, fp, or binary (byte arrays, strings, etc) type
Value: Expression

// integral type
Size:  Expression

TypeSpecList: TypeSpecList - TypeSpec
              TypeSpec

TypeSpec: Endianness
          Sign
          Type
          Unit

// default is big
Endianness:  big
             little
             native

Sign:  signed
       unsigned

Type:  integer
       float
       binary

//Size*Unit is the number of bits that get matched
// Unit defaults to 1 for Type = integer or float,
                    8 for Type = binary
Unit:  IntegerLiteral

Reply via email to