Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for enums and attributes #13

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now you're taking shortcuts. Not all if-statements are wrapped in braces.
In C you can declare statements like this:

if(condition) ++x; else ++y;

"""
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})")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to add support for new formatting styles in style.py then support it in writer.py

  • Space after if but before parenthesis? if( vs. if (
  • Space around the condition? (condition) vs. ( 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)