Skip to content

Commit

Permalink
Fixing bug in previous #56 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cacharle committed Aug 10, 2023
1 parent e861f5e commit 83f78ba
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 9 deletions.
28 changes: 21 additions & 7 deletions c_formatter_42/formatters/hoist.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,26 @@ def hoist(content: str) -> str:

input_lines = content.split("\n")

lines = []
# split assignment
for line in input_lines:
m = re.match(
def match_declaration_assignment(s):
return re.match(
r"^(?P<indent>\s+)"
r"(?P<type>{t})\s+"
r"(?P<name>{d})\s+=\s+"
r"(?P<value>.+);$".format(t=helper.REGEX_TYPE, d=helper.REGEX_DECL_NAME),
line,
s,
)
if m is not None and re.match(r".*\[.*\].*", m.group("name")) is None:

def is_declaration_assignment_array(s):
m = match_declaration_assignment(s)
if m is None:
return False
return re.match(r".*\[.*\].*", m.group("name")) is not None

lines = []
# split assignment
for line in input_lines:
m = match_declaration_assignment(line)
if m is not None and not is_declaration_assignment_array(line):
lines.append(f"\t{m.group('type')}\t{m.group('name')};")
lines.append(
"{}{} = {};".format(
Expand All @@ -70,7 +79,12 @@ def hoist(content: str) -> str:
decl_regex = r"^\s*{t}\s+{d};$".format(
t=helper.REGEX_TYPE, d=helper.REGEX_DECL_NAME
)
declarations = [line for line in lines if re.match(decl_regex, line) is not None]
declarations = [
line
for line in lines
if re.match(decl_regex, line) is not None
or is_declaration_assignment_array(line)
]
body = [line for line in lines if line not in declarations and line != ""]
lines = declarations
if len(declarations) != 0:
Expand Down
63 changes: 61 additions & 2 deletions tests/formatters/test_hoist.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,72 @@ def test_hoist_array_initialization():
input = """
void foo(void)
{
\tint xs[4] = {1, 2, 3, 4};
\tint\txs[4] = {1, 2, 3, 4};
\txs[0] = -1;
}
"""
output = """
void foo(void)
{
\tint xs[4] = {1, 2, 3, 4};
\tint\txs[4] = {1, 2, 3, 4};
\txs[0] = -1;
}
"""
assert output == hoist(input)


def test_hoist_array_initialization_multiple_variable():
input = """
void foo(void)
{
\tint\ta = 1;
\ta++;
\tint\txs[4] = {1, 2, 3, 4};
\txs[0] = -1;
\tint\tb = 2;
\tb++;
}
"""
output = """
void foo(void)
{
\tint\ta;
\tint\txs[4] = {1, 2, 3, 4};
\tint\tb;
\ta = 1;
\ta++;
\txs[0] = -1;
\tb = 2;
\tb++;
}
"""
assert output == hoist(input)


def test_hoist_array_initialization_issue_56_example():
input = """
int main(void)
{
\tint\tlength;
\tint\tval;
\tint\tarray[] = {1, 2, 3, 4};
\tlength = sizeof(array) / sizeof(array[0]);
\tval = 2;
\treturn (0);
}
"""
output = """
int main(void)
{
\tint\tlength;
\tint\tval;
\tint\tarray[] = {1, 2, 3, 4};
\tlength = sizeof(array) / sizeof(array[0]);
\tval = 2;
\treturn (0);
}
"""
assert output == hoist(input)
Expand Down

0 comments on commit 83f78ba

Please sign in to comment.