[chirp_devel] [PATCH 1 of 2] [RFC] Add ability to define structure types in bitwise
Dan Smith
Tue Apr 10 20:07:40 PDT 2012
# HG changeset patch
# User Dan Smith <dsmith at danplanet.com>
# Date 1334113255 25200
# Node ID ce7239963bc4dd7bef2b8dc1a3bdb6fbe26aab75
# Parent c3b8a85d6e235eb493c790ddac982683414dd251
[RFC] Add ability to define structure types in bitwise
This adds the ability to define structure types in bitwise formats
and then use them in multiple places. Right now, we have a few places where
this is needed and it's badly faked like this:
struct_format = "struct { u8 foo; u8 bar; }"
mem_format =
struct_format + "struct1;" +
struct_format + "struct2;"
This new change to bitwise will allow us to behave more like C and do:
mem_format = """
struct mytype { u8 foo; u8 bar; };
struct mytype struct1;
struct mytype struct2;
"""
The bitwise grammar changed a little, but it's still compatible with the
existing definitions and all of the tests still pass.
#93
diff -r c3b8a85d6e23 -r ce7239963bc4 chirp/bitwise.py
--- a/chirp/bitwise.py Tue Apr 10 19:10:20 2012 -0700
+++ b/chirp/bitwise.py Tue Apr 10 20:00:55 2012 -0700
@@ -590,6 +590,7 @@
self._data = data
self._offset = offset
self._obj = None
+ self._user_types = {}
def do_symbol(self, symdef, gen):
name = symdef[1]
@@ -647,8 +648,11 @@
else:
self._generators[name] = res
- def parse_struct(self, struct):
+ def parse_struct_decl(self, struct):
block = struct[:-1]
+ if block[0][0] == "symbol":
+ # This is a pre-defined struct
+ block = self._user_types[block[0][1]]
deftype = struct[-1]
if deftype[0] == "array":
name = deftype[1][0][1]
@@ -672,6 +676,19 @@
else:
self._generators[name] = result
+ def parse_struct_defn(self, struct):
+ name = struct[0][1]
+ block = struct[1:]
+ self._user_types[name] = block
+
+ def parse_struct(self, struct):
+ if struct[0][0] == "struct_defn":
+ return self.parse_struct_defn(struct[0][1])
+ elif struct [0][0] == "struct_decl":
+ return self.parse_struct_decl(struct[0][1])
+ else:
+ raise Exception("Internal error: What is `%s'?" % struct[0][0])
+
def parse_directive(self, directive):
name = directive[0][0]
if name == "seekto":
@@ -713,6 +730,8 @@
if __name__ == "__main__":
defn = """
+struct mytype { u8 foo; };
+struct mytype bar;
struct {
u8 foo;
u8 highbit:1,
@@ -722,9 +741,11 @@
bbcd fourdigits[2];
} mystruct;
"""
- data = "\x7F\x81abc\x12\x34"
+ data = "\xab\x7F\x81abc\x12\x34"
tree = parse(defn, data)
+ print repr(tree)
+
print "Foo %i" % tree.mystruct.foo
print "Highbit: %i SixZeros: %i: Lowbit: %i" % (tree.mystruct.highbit,
tree.mystruct.sixzeros,
diff -r c3b8a85d6e23 -r ce7239963bc4 chirp/bitwise_grammar.py
--- a/chirp/bitwise_grammar.py Tue Apr 10 19:10:20 2012 -0700
+++ b/chirp/bitwise_grammar.py Tue Apr 10 20:00:55 2012 -0700
@@ -64,8 +64,14 @@
def _block():
return "{", _block_inner, "}"
+def struct_defn():
+ return symbol, _block
+
+def struct_decl():
+ return [symbol, _block], [array, symbol]
+
def struct():
- return keyword("struct"), _block, [array, symbol], ";"
+ return keyword("struct"), [struct_defn, struct_decl], ";"
def _language():
return _block_inner
More information about the chirp_devel
mailing list