Skip to content

Structs #28

@fjl

Description

@fjl

In my usage of Geas, it comes up quite often that I have to decode an input containing fixed-size fields. To do this, I pretty much always have to hard-code the offsets of fields, and also need to place their sizes. Accessing a field that is less than 32 bytes sometimes also requires masking off unrelated bytes from the result of MLOAD.

It would be nice to have some support for this from the language. A facility for defining memory layouts could be added like this, with sizes given in bytes:

#defstruct point {
    x: 20
    y: 20
}

This would define macros like this:

point.size = 40
point.x.size = 20
point.x.offset = 0
point.y.size = 20
point.y.offset = 20

There should also be a way to nest definitions, i.e. given the definition above, one could define a rectangle which is defined in terms of two points:

#defstruct rectangle {
    min: point
    max: point
}

which could define macros like below. Note the field offsets are relative to the top-level structure.

rectangle.size = 80
rectangle.min.size = 20
rectangle.min.offset = 0
rectangle.min.x.size = 20
rectangle.min.x.offset = 0
rectangle.min.y.size = 20
rectangle.min.y.offset = 20
rectangle.max.size = 20
rectangle.max.offset = 40
rectangle.max.x.size = 20
rectangle.max.x.offset = 40
rectangle.max.y.size = 20
rectangle.max.y.offset = 60

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions