decompiler: Allow separating blocks

This will be useful for closure arguments.
This commit is contained in:
Jan Tojnar 2023-11-18 21:04:45 +01:00
parent 638b5f1dc2
commit 479fe17589

View file

@ -52,6 +52,7 @@ class TextFragment:
@dataclass @dataclass
class BlockInfo: class BlockInfo:
terminator: T.Optional[TextFragment] = None terminator: T.Optional[TextFragment] = None
separator: T.Optional[TextFragment] = None
class LineType(Enum): class LineType(Enum):
@ -105,9 +106,17 @@ class DecompileCtx:
if terminator := block.terminator: if terminator := block.terminator:
self.print(terminator.text, terminator.newline_after, terminator.indent) self.print(terminator.text, terminator.newline_after, terminator.indent)
def separate_block_elements(self) -> None:
block = self._blocks[-1]
if separator := block.separator:
self.print(separator.text, separator.newline_after, separator.indent)
def end_block_with(self, text: str, newline_after: bool = True, indent: bool = True) -> None: def end_block_with(self, text: str, newline_after: bool = True, indent: bool = True) -> None:
self._blocks[-1].terminator = TextFragment(text, newline_after, indent) self._blocks[-1].terminator = TextFragment(text, newline_after, indent)
def separate_block_elements_with(self, text: str, newline_after: bool = False, indent: bool = False) -> None:
self._blocks[-1].separator = TextFragment(text, newline_after, indent)
def print(self, line: str, newline: bool = True, indent: bool = True) -> None: def print(self, line: str, newline: bool = True, indent: bool = True) -> None:
if line == "}" or line == "]": if line == "}" or line == "]":
self._indent -= 1 self._indent -= 1
@ -206,7 +215,11 @@ def _decompile_element(
ctx.start_block() ctx.start_block()
gir = decompiler(ctx, gir, **args) gir = decompiler(ctx, gir, **args)
first = True
for child in xml.children: for child in xml.children:
if not first:
ctx.separate_block_elements()
first = False
_decompile_element(ctx, gir, child) _decompile_element(ctx, gir, child)
ctx.end_block() ctx.end_block()