diff --git a/src/cfile/core.py b/src/cfile/core.py index 4a5d5d0..74e8edf 100644 --- a/src/cfile/core.py +++ b/src/cfile/core.py @@ -540,3 +540,22 @@ class Block(Sequence): """ A sequence wrapped in braces """ + + +class ConditionType(Enum): + IF = 0 + ELSE_IF = 1 + ELSE = 2 + + +class Condition(Block): + """ + A condition wrapped in braces + """ + def __init__(self, condition: str = "", type: ConditionType = ConditionType.ELSE): + super().__init__() + self.condition = condition + self.type = type + + if (condition and type == ConditionType.ELSE) or (not condition and type != ConditionType.ELSE): + raise Exception("Condition and type didn't match") diff --git a/src/cfile/factory.py b/src/cfile/factory.py index db3808b..4723a86 100644 --- a/src/cfile/factory.py +++ b/src/cfile/factory.py @@ -246,3 +246,9 @@ def declaration(self, New declaration """ return core.Declaration(element, init_value) + + def condition(self, condition:str, type:core.ConditionType): + """ + New condition + """ + return core.Condition(condition, type) diff --git a/src/cfile/writer.py b/src/cfile/writer.py index 3d438a5..ff6628e 100644 --- a/src/cfile/writer.py +++ b/src/cfile/writer.py @@ -76,8 +76,11 @@ def _dedent(self): self.indentation_str = self.indentation_char * \ (self.indentation_level * self.indent_width) - def _start_line(self): - self.fh.write(self.indentation_str) + def _start_line(self, indent:bool = True): + if indent: + self.fh.write(self.indentation_str) + else: + self.fh.write("") def _write(self, text): self.fh.write(text) @@ -125,6 +128,7 @@ def __init__(self, style: c_style.StyleOptions) -> None: "IfndefDirective": self._write_ifndef_directive, "EndifDirective": self._write_endif_directive, "Extern": self._write_extern, + "Condition": self._write_condition, } self.last_element = ElementType.NONE @@ -184,6 +188,9 @@ def _write_sequence(self, sequence: core.Sequence) -> None: self._start_line() self._write_line_comment(elem) self._eol() + elif isinstance(elem, core.Condition): + self._start_line() + self._write_condition(elem) elif isinstance(elem, core.Block): self._start_line() self._write_block(elem) @@ -191,7 +198,10 @@ def _write_sequence(self, sequence: core.Sequence) -> None: self._start_line() self._write_line_element(elem) else: - self._start_line() + if isinstance(elem, core.Blank): + self._start_line(False) + else: + self._start_line() class_name = elem.__class__.__name__ write_method = self.switcher_all.get(class_name, None) if write_method is not None: @@ -762,3 +772,12 @@ def _write_endif_directive(self, elem: core.EndifDirective) -> None: def _write_extern(self, elem: core.Extern) -> None: self._write(f'extern "{elem.language}"') self.last_element = ElementType.DIRECTIVE + + def _write_condition(self, elem:core.Condition) -> None: + if elem.type == core.ConditionType.IF: + self._write(f"if ({elem.condition})") + elif elem.type == core.ConditionType.ELSE_IF: + self._write(f"else if ({elem.condition})") + elif elem.type == core.ConditionType.ELSE: + self._write(f"else") + self._write_block(elem)