Skip to content

Commit

Permalink
Added very basic conditions and fixed blank
Browse files Browse the repository at this point in the history
Signed-off-by: Cervenka Dusan <[email protected]>
  • Loading branch information
Hadatko committed Oct 22, 2024
1 parent bd63c99 commit f3811d5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
19 changes: 19 additions & 0 deletions src/cfile/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
6 changes: 6 additions & 0 deletions src/cfile/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
25 changes: 22 additions & 3 deletions src/cfile/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -184,14 +188,20 @@ 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)
elif isinstance(elem, core.Line):
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:
Expand Down Expand Up @@ -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)

0 comments on commit f3811d5

Please sign in to comment.