sealmove / binarylang

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

Implement a constructor that allows initialization of the object with the assertion expressions defined in the struct #26

Open sgmihai opened 1 year ago

sgmihai commented 1 year ago

For expresiveness and consistency, it would be nice to use the binarylang's data structure definitions, to define outgoing only data in a network protocol, that does not need to be parsed in any way, just serialized. We can do this, but the object variable still has to be initialized with the values we want, and thus the assertion expressions are just comsetic in this example. I'd like to be able to do something like

let myData = DataObject.initAssertions()

  struct(udpTrackerPing, endian = b):
    64: connection_id = 0x41727101980
    32: action = 0
    32: transaction_id = rand(int32)
  let ping = UdpTrackerPing(connection_id: 0x41727101980, action: 0, transaction_id: rand(int32))
sgmihai commented 1 year ago

On second thought, there are cases where one might want to initialize the object with different default values than the ones used for assertions. I believe there should be some special syntax to allow defining default values. Nim also recently got support merged for default value in objects: https://github.com/nim-lang/Nim/pull/20480

sealmove commented 1 year ago

Sounds like a useful feature. My only concern is that, this essentially it requires extra codegen - some kind of initializer proc. I don't want to bloat codegen with something like this, because most people will probably not need it, but binarylang will still generate it for every single parser definition.

The next big planned change for binarylang is to split codegen into loosely-coupled components, and allow the used to configure which components to use. Once this component-based system is implemented, this feature can be a separate component.

sealmove commented 1 year ago

Update: Binarylang now supports plugins as a parser option, so this feature can be implemented as such.