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

Fix #70 #71

Merged
merged 4 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 1 addition & 2 deletions c_formatter_42/formatters/align.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ def align_scope(content: str, scope: Literal["local", "global"]) -> str:

# Minimum alignment required for each line
min_alignment = max(
(len(prefix.expandtabs(4)) // 4 + 1 for _, prefix, _ in aligned),
default=1,
(len(prefix.expandtabs(4)) // 4 + 1 for _, prefix, _ in aligned), default=1
)
for i, prefix, suffix in aligned:
alignment = len(prefix.expandtabs(4)) // 4
Expand Down
9 changes: 4 additions & 5 deletions c_formatter_42/formatters/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
# ::: :::::::: #
# helper.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: root <root@student.42.fr> +#+ +:+ +#+ #
# By: leo <leo@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/10/04 11:38:00 by cacharle #+# #+# #
# Updated: 2023/09/03 11:05:57 by root ### ########.fr #
# Updated: 2023/09/22 15:21:49 by leo ### ########.fr #
# #
# **************************************************************************** #

from __future__ import annotations

import re
Expand All @@ -22,9 +23,7 @@
# regex for a c variable/function name
REGEX_NAME = r"\**[a-zA-Z_*()]\w*"
# regex for a name in a declaration context (with array and function ptr)
REGEX_DECL_NAME = r"\(?{name}(\[.*\])*(\s\=\s{{.*}})?(\)\(.*\))?".format(
name=REGEX_NAME
)
REGEX_DECL_NAME = r"\(?{name}(\[.*\])*(\s\=\s.*)?(\)\(.*\))?".format(name=REGEX_NAME)


def locally_scoped(func: Callable[[str], str]) -> Callable[[str], str]:
Expand Down
14 changes: 9 additions & 5 deletions c_formatter_42/formatters/hoist.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# ############################################################################ #
# **************************************************************************** #
# #
# ::: :::::::: #
# hoist.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: cacharle <[email protected]> +#+ +:+ +#+ #
# By: leo <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/10/04 11:16:28 by cacharle #+# #+# #
# Updated: 2021/02/11 20:13:29 by charles ### ########.fr #
# Updated: 2023/09/22 16:16:01 by leo ### ########.fr #
# #
# ############################################################################ #
# **************************************************************************** #

import re

Expand Down Expand Up @@ -59,7 +59,11 @@ def hoist(content: str) -> str:
# If line is a declaration + assignment on the same line,
# create 2 new lines, one for the declaration and one for the assignment
# NOTE: edge case for array declarations which can't be hoisted (See #56)
if m is not None and re.match(r".*\[.*\].*", m.group("name")) is None:
if (
m is not None
and re.match(r".*\[.*\].*", m.group("name")) is None
and re.match(r"\s*(const|static)\s.*", line) is None
):
lines.append(f"\t{m.group('type')}\t{m.group('name')};")
lines.append(
"{}{} = {};".format(
Expand Down
6 changes: 3 additions & 3 deletions tests/formatters/test_align.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# ::: :::::::: #
# test_align.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: root <root@student.42.fr> +#+ +:+ +#+ #
# By: leo <leo@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/10/04 12:19:45 by cacharle #+# #+# #
# Updated: 2023/09/02 20:26:16 by root ### ########.fr #
# Updated: 2023/09/23 11:06:39 by leo ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -188,7 +188,7 @@ def test_align_local_multiple_functions():
output = """
int\t\t\tf()
{
\tint a = 0;
\tint\ta = 0;
}
int\t\t\tg()
{
Expand Down
11 changes: 8 additions & 3 deletions tests/formatters/test_hoist.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# ::: :::::::: #
# test_hoist.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: root <root@student.42.fr> +#+ +:+ +#+ #
# By: leo <leo@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/10/04 12:29:07 by cacharle #+# #+# #
# Updated: 2023/09/02 22:43:13 by root ### ########.fr #
# Updated: 2023/09/23 10:55:14 by leo ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -197,21 +197,26 @@ def test_hoist_empty_function():
assert output == hoist(input)


def test_hoist_array_initialization():
def test_hoist_initializations():
input = """
void foo(void)
{
\tint a = 2;
\tstatic int\txs[4] = {1, 2, 3, 4};
\tconst int\tb = 2;
\tlong int\tc = 2;
}
"""
output = """
void foo(void)
{
\tint\ta;
\tstatic int\txs[4] = {1, 2, 3, 4};
\tconst int\tb = 2;
\tlong int\tc;

\ta = 2;
\tc = 2;
}
"""
assert output == hoist(input)
Expand Down
Loading