Skip to content

Commit

Permalink
zcbor.py: Various cleanup and refactoring
Browse files Browse the repository at this point in the history
Mostly to improve readability and debuggability.
Also removed some code that was completely unneeded.

Signed-off-by: Øyvind Rønningstad <[email protected]>
  • Loading branch information
oyvindronningstad committed Jul 26, 2024
1 parent 9ef6fbc commit cbda284
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions zcbor/zcbor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,7 @@ def set_skipped(self, skipped):
self.skipped = True
else:
self.skipped = skipped
return

def delegate_type_condition(self):
"""Whether to use the C type of the first child as this type's C type"""
Expand All @@ -1091,8 +1092,9 @@ def set_access_prefix(self, prefix, is_delegated=False):
list(map(lambda child: child.set_skipped(child.skip_condition()),
self.value))
list(map(lambda child: child.set_access_prefix(
self.var_access(), is_delegated=self.delegate_type_condition()
or (is_delegated and self.skip_condition())),
self.var_access(),
is_delegated=(self.delegate_type_condition()
or (is_delegated and self.skip_condition()))),
self.value))
elif self in self.my_types.values() and self.type != "OTHER":
self.set_skipped(not self.multi_member())
Expand Down Expand Up @@ -1147,10 +1149,12 @@ def var_access(self):
def val_access(self):
""""Path" to access this element's actual value variable."""
if self.is_unambiguous_repeated():
return "NULL"
ret = "NULL"
elif self.skip_condition() or self.is_delegated_type():
return self.var_access()
return self.access_append(self.var_name())
ret = self.var_access()
else:
ret = self.access_append(self.var_name())
return ret

def repeated_val_access(self):
if self.is_unambiguous_repeated():
Expand Down Expand Up @@ -1924,8 +1928,7 @@ def child_single_declarations(self):
decl = list()
for child in self.value:
if not child.is_unambiguous_repeated():
decl.extend(child.add_var_name(
child.single_var_type(), anonymous=True))
decl.extend(child.single_declaration())
return decl

def simple_func_condition(self):
Expand Down Expand Up @@ -2038,8 +2041,9 @@ def add_var_name(self, var_type, full=False, anonymous=False):
assert (var_type[-1][-1] == "}" or len(var_type) == 1), \
f"Expected single var: {var_type!r}"
if not anonymous or var_type[-1][-1] != "}":
var_type[-1] += " %s%s" % (
self.var_name(), "[%s]" % self.max_qty if full and self.max_qty != 1 else "")
var_name = self.var_name()
array_part = f"[{self.max_qty}]" if full and self.max_qty != 1 else ""
var_type[-1] += f" {var_name}{array_part}"
var_type = add_semicolon(var_type)
return var_type

Expand All @@ -2063,6 +2067,9 @@ def union_type(self):
declaration = self.enclose("union", self.child_single_declarations())
return declaration

def single_declaration(self):
return self.add_var_name(self.single_var_type(), anonymous=True)

def repeated_declaration(self):
"""Declaration of the repeated part of this element."""
if self.is_unambiguous_repeated():
Expand Down Expand Up @@ -2275,16 +2282,6 @@ def single_func_prim(self, access, union_int=None, ptr_result=False):
else:
assert False, "Should not come here."

min_val = None
max_val = None

if self.value is None:
if self.type in ["BSTR", "TSTR"]:
min_val = self.min_size
max_val = self.max_size
else:
min_val = self.min_value
max_val = self.max_value
return (func_name, arg)

def single_func(self, access=None, union_int=None):
Expand Down

0 comments on commit cbda284

Please sign in to comment.