savi-lang / savi

A fast language for programmers who are passionate about their craft.
BSD 3-Clause "New" or "Revised" License
156 stars 12 forks source link

Bit pattern matching #374

Open mneumann opened 2 years ago

mneumann commented 2 years ago

Just looking at msgpack spec, which defines a table like this to decode the type of the following value:

positive fixint | 0xxxxxxx
fixmap          | 1000xxxx
...
nil             | 11000000

The code to test for example for "positive fixint" looks like

first_byte.bit_and(0b10000000).is_nonzero

which IMHO is un-intuitive.

It would be nice to write something along the lines:

first_byte =~ b/0xxxxxxx/

This would require a new BitMask type and a new literal to create it. Once we have custom string literals (#247), we could create e.g. a BitMask using something like '0xxxxxxx'_bitmask. This would actually be simple if we had some kind of comptime evaluations, then we could just add a method bitmask! on String.

mneumann commented 1 year ago

Or something like Ocaml's Bitstring: https://ocaml.org/p/bitstring/latest/doc/Bitstring/index.html