Kipt / BEdit

Issues and documentation for BEdit
6 stars 0 forks source link

Improvements for specifying dynamic type #43

Closed Kipt closed 3 years ago

Kipt commented 3 years ago

A lot of files have structure like:

struct Foo
{
    u(4) typeId;
    type_of_typeid data;
};

The only way to handle these cases in BEdit is by a rather verbose branching scheme.

enum TypeId
{
    TypeA,
    TypeB,
    ...
    TypeN
};

struct A { ... };
struct B { ... };
...
struct N { ... };

struct Foo
{
    TypeId(4) typeId;
    if (typeId == TypeId.TypeA) { A data; }
    else if (typeId == TypeId.TypeB) { B data; }
    ...
    else if (typeId == TypeId.TypeN) { N data; }
};

A rudimentary reflection could be added to help in these cases.

Perhaps something like

struct category(foo, 0) A { ... };
struct category(foo, 1) B { ... };
...
struct category(foo, n) N { ... };

struct Foo
{
    u(4) typeId;
    type(foo, typeId) data;
};
Kipt commented 3 years ago

Basic reflection implemented in version 0.2.0.

Example of syntax (see documentation here at github for more info):

struct (Messages, "Hi") HelloMessage 
{
    u(4) size;
    string(size) greeting;
};

struct(Messages, 12 + 345) i8Message
{
    u(1) value;
};

struct Message
{
    u(4) type;
    Messages[type] content;
};