Skip to content

Commit

Permalink
Allow enums to reference their own members
Browse files Browse the repository at this point in the history
  • Loading branch information
Schamper committed Jul 28, 2023
1 parent 80b64cd commit 2004de4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dissect/cstruct/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def _enum(self, tokens: TokenConsumer) -> None:
if not val:
val = nextval
else:
val = Expression(self.cstruct, val).evaluate()
val = Expression(self.cstruct, val).evaluate(values)

if enumtype == "flag":
high_bit = val.bit_length() - 1
Expand Down
16 changes: 16 additions & 0 deletions tests/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,19 @@ def test_enum_anonymous_struct(compiled):

t = test(b"\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0A\x00\x00\x00")
assert t.arr == [255, 0, 0, 10]


def test_enum_reference_own_member(compiled):
cdef = """
enum test {
A,
B = A + 3,
C
};
"""
cs = cstruct.cstruct()
cs.load(cdef, compiled=compiled)

assert cs.test.A == 0
assert cs.test.B == 3
assert cs.test.C == 4

0 comments on commit 2004de4

Please sign in to comment.