Skip to content

Commit

Permalink
add @ operator
Browse files Browse the repository at this point in the history
  • Loading branch information
Calcoph committed Jul 18, 2022
1 parent 20bfebe commit d7cb01b
Show file tree
Hide file tree
Showing 6 changed files with 844 additions and 237 deletions.
12 changes: 6 additions & 6 deletions examples/MANUAL_bin_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
def bytes_to_int(bytes_: list[u8]):
value = 0
for (exponent, byte) in enumerate(bytes_):
value += int(byte) * 256**exponent
value += byte * 256**exponent
return value

with open("binary_files/bin_file.bin", "rb") as f:
byts = f.read()

file = File("file", Dollar(0x00, byts))
file = File("file") @ Dollar(0x00, byts)

print()
print(f"{file.header.header_name=}")
print(f"{int(file.header.header_size)=:02X}")
print(f"{int(file.header.data_size)=:02X}")
print(f"{int(file.header.element_amount)=}")
print(f"{file.header.header_size=:02X}")
print(f"{file.header.data_size=:02X}")
print(f"{file.header.element_amount=}")
for index, datum in enumerate(file.data):
if datum.flags.f_variable == 1:
print(f" datum {index:02}: {bytes_to_int(datum.data)=}")
elif datum.flags.f_u16 == 1 or datum.flags.f_u32 == 1:
print(f" datum {index:02}: {int(datum.data)=}")
print(f" datum {index:02}: {datum.data=}")
2 changes: 1 addition & 1 deletion examples/bin_file.pat
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ bitfield Flags {
unused: 4;
f_variable: 1;
// This comment will appear in the .py file
f_u16: 1; // This comment won't appear in the .py file
f_u16: 1; // This comment won't appear in the .py file. But it will in the docstring
f_u32: 1;
};

Expand Down
93 changes: 60 additions & 33 deletions examples/bin_file.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import List
from primitives import Dollar, Struct, u8, u16, u32, u64, u128, s8, s16, s32, s64, s128, Float, double, char, char16, Bool, Padding, BitField, sizeof, addressof


Expand All @@ -8,26 +9,34 @@ class Flags(BitField):
bitfield Flags {
unused: 4;
f_variable: 1;
f_u16: 1;
// This comment will appear in the .py file
f_u16: 1; // This comment won't appear in the .py file
f_u32: 1;
};
```"""
def __init__(self, _thisbitfield_s_name: str, _dollar___offset: Dollar):
```"""
def __init__(self, name: str=""):
"""
bitfield
Args:
_thisbitfield_s_name (str): The name of this instance. Can be whatever you want or just an empty string
_dollar___offset (Dollar): Dollar pointing to the start of this bitfield
name (str, optional): The name of this instance. Can be whatever you want or just an empty string. Defaults to "".
"""

super().__init__(name)

def __matmul__(self, _dollar___offset):
if not isinstance(_dollar___offset, Dollar):
raise Exception(f'An object of class "Dollar" must be used with the "@" operator. {type(_dollar___offset_copy)} was used instead')
_dollar___offset_copy = _dollar___offset.copy()
cur_byte = _dollar___offset.read(1)[0]
self.unused: int = (cur_byte >>0) & self._bit_field___masks_dict[4]
self.f_variable: int = (cur_byte >>4) & self._bit_field___masks_dict[1]
# This comment will appear in the .py file
self.f_u16: int = (cur_byte >>5) & self._bit_field___masks_dict[1]
self.f_u32: int = (cur_byte >>6) & self._bit_field___masks_dict[1]
super().__init__(_thisbitfield_s_name, _dollar___offset_copy, _dollar___offset.copy())
super().init_struct(_dollar___offset_copy, _dollar___offset.copy())
return self

class Data(Struct):
"""
Expand All @@ -46,34 +55,40 @@ class Data(Struct):
u32 data;
}
};
```"""
def __init__(self, _thisstruct_s_name: str, _dollar___offset: Dollar):
def __init__(self, name: str=""):
"""
struct
Args:
_thisstruct_s_name (str): The name of this instance. Can be whatever you want or just an empty string
_dollar___offset (Dollar): Dollar pointing to the start of this struct
name (str, optional): The name of this instance. Can be whatever you want or just an empty string. Defaults to "".
"""
super().__init__(name)

def __matmul__(self, _dollar___offset):
if not isinstance(_dollar___offset, Dollar):
raise Exception(f'An object of class "Dollar" must be used with the "@" operator. {type(_dollar___offset_copy)} was used instead')
_dollar___offset_copy = _dollar___offset.copy()
flags: Flags = Flags('flags', _dollar___offset)
flags: Flags = Flags('flags') @ _dollar___offset
self.flags = flags
if (flags.f_variable == 1):
amount: u8 = u8('amount', _dollar___offset)
amount: u8 = u8('amount') @ _dollar___offset
self.amount = amount
self.data: list[u8] = []
self.data: List[u8] = []
for i in range(0,eval('amount')):
self.data.append(u8(f'data_{i}', _dollar___offset))
self.data.append(u8(f'data_{i}') @ _dollar___offset)

if (flags.f_u16 == 1):
data: u16 = u16('data', _dollar___offset)
data: u16 = u16('data') @ _dollar___offset
self.data = data

if (flags.f_u32 == 1):
data: u32 = u32('data', _dollar___offset)
data: u32 = u32('data') @ _dollar___offset
self.data = data

super().__init__(_thisstruct_s_name, _dollar___offset_copy, _dollar___offset.copy())
super().init_struct(_dollar___offset_copy, _dollar___offset.copy())
return self

class Header(Struct):
"""
Expand All @@ -85,26 +100,32 @@ class Header(Struct):
u32 data_size;
u8 element_amount;
};
```"""
def __init__(self, _thisstruct_s_name: str, _dollar___offset: Dollar):
def __init__(self, name: str=""):
"""
struct
Args:
_thisstruct_s_name (str): The name of this instance. Can be whatever you want or just an empty string
_dollar___offset (Dollar): Dollar pointing to the start of this struct
name (str, optional): The name of this instance. Can be whatever you want or just an empty string. Defaults to "".
"""
super().__init__(name)

def __matmul__(self, _dollar___offset):
if not isinstance(_dollar___offset, Dollar):
raise Exception(f'An object of class "Dollar" must be used with the "@" operator. {type(_dollar___offset_copy)} was used instead')
_dollar___offset_copy = _dollar___offset.copy()
self.header_name: list[char] = []
self.header_name: List[char] = []
for i in range(0,eval('8')):
self.header_name.append(char(f'header_name_{i}', _dollar___offset))
header_size: u32 = u32('header_size', _dollar___offset)
self.header_name.append(char(f'header_name_{i}') @ _dollar___offset)
header_size: u32 = u32('header_size') @ _dollar___offset
self.header_size = header_size
data_size: u32 = u32('data_size', _dollar___offset)
data_size: u32 = u32('data_size') @ _dollar___offset
self.data_size = data_size
element_amount: u8 = u8('element_amount', _dollar___offset)
element_amount: u8 = u8('element_amount') @ _dollar___offset
self.element_amount = element_amount
super().__init__(_thisstruct_s_name, _dollar___offset_copy, _dollar___offset.copy())
super().init_struct(_dollar___offset_copy, _dollar___offset.copy())
return self

class File(Struct):
"""
Expand All @@ -115,20 +136,26 @@ class File(Struct):
padding[3];
Data data[header.element_amount];
};
```"""
def __init__(self, _thisstruct_s_name: str, _dollar___offset: Dollar):
def __init__(self, name: str=""):
"""
struct
Args:
_thisstruct_s_name (str): The name of this instance. Can be whatever you want or just an empty string
_dollar___offset (Dollar): Dollar pointing to the start of this struct
name (str, optional): The name of this instance. Can be whatever you want or just an empty string. Defaults to "".
"""
super().__init__(name)

def __matmul__(self, _dollar___offset):
if not isinstance(_dollar___offset, Dollar):
raise Exception(f'An object of class "Dollar" must be used with the "@" operator. {type(_dollar___offset_copy)} was used instead')
_dollar___offset_copy = _dollar___offset.copy()
header: Header = Header('header', _dollar___offset)
header: Header = Header('header') @ _dollar___offset
self.header = header
self.padding_0: Padding = Padding('padding_0', _dollar___offset, eval('3'))
self.data: list[Data] = []
self.padding_0: Padding = Padding(eval('3'), 'padding_0') @ _dollar___offset
self.data: List[Data] = []
for i in range(0,eval('header.element_amount')):
self.data.append(Data(f'data_{i}', _dollar___offset))
super().__init__(_thisstruct_s_name, _dollar___offset_copy, _dollar___offset.copy())
self.data.append(Data(f'data_{i}') @ _dollar___offset)
super().init_struct(_dollar___offset_copy, _dollar___offset.copy())
return self
Loading

0 comments on commit d7cb01b

Please sign in to comment.