sealmove / binarylang

Binary parser/encoder DSL
MIT License
59 stars 0 forks source link

Proposal of shift operator in type declaration #16

Closed arkanoid87 closed 1 year ago

arkanoid87 commented 3 years ago

What about a letter to define the possibility to "shift left" an int field when inflating it to the nearest int size, instead of the current default shift right?

I mean using an hypotetical symbol x

import std/[sugar, strutils]
import binarylang, bitstreams

struct(parser):
    u15: one
    ux17: two

let data = 0xFFFFFFFF.uint32
dump data.sizeof
let parsed = parser.get(newStringBitStream(data.toHex.parseHexStr))
dump parsed.one.sizeof
dump parsed.one.toHex
dump parsed.two.sizeof
dump parsed.two.toHex

# OUTPUT
# data = 4294967295
# data.sizeof = 4
# parsed.one.sizeof = 2
# parsed.one.toHex = 7FFF
# parsed.two.sizeof = 4
# parsed.two.toHex = 1FFFF000 instead of 0001FFFF
sealmove commented 3 years ago

In order to keep BL as lean as possible, this should be implemented as a plugin. The syntax will look like this:

struct(parser):
  u15: one
  u17 {shiftLeft}: two