From dbad100aa64729469385e74821adcd1cfbb8f2ce Mon Sep 17 00:00:00 2001 From: l Date: Fri, 22 Sep 2023 16:07:41 +0200 Subject: [PATCH 1/4] stopped hoisting lines staring with const or static and treat initialization like declaration instead of assignment --- c_formatter_42/formatters/align.py | 3 +-- c_formatter_42/formatters/helper.py | 9 ++++----- c_formatter_42/formatters/hoist.py | 16 +++++++++++----- tests/formatters/test_hoist.py | 9 +++++++-- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/c_formatter_42/formatters/align.py b/c_formatter_42/formatters/align.py index f199a54..85b2822 100644 --- a/c_formatter_42/formatters/align.py +++ b/c_formatter_42/formatters/align.py @@ -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 diff --git a/c_formatter_42/formatters/helper.py b/c_formatter_42/formatters/helper.py index bc22278..16f7be9 100644 --- a/c_formatter_42/formatters/helper.py +++ b/c_formatter_42/formatters/helper.py @@ -3,12 +3,13 @@ # ::: :::::::: # # helper.py :+: :+: :+: # # +:+ +:+ +:+ # -# By: root +#+ +:+ +#+ # +# By: leo +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # 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 @@ -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]: diff --git a/c_formatter_42/formatters/hoist.py b/c_formatter_42/formatters/hoist.py index 35d0c4b..feb8f96 100644 --- a/c_formatter_42/formatters/hoist.py +++ b/c_formatter_42/formatters/hoist.py @@ -1,14 +1,14 @@ -# ############################################################################ # +# **************************************************************************** # # # # ::: :::::::: # # hoist.py :+: :+: :+: # # +:+ +:+ +:+ # -# By: cacharle +#+ +:+ +#+ # +# By: leo +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/10/04 11:16:28 by cacharle #+# #+# # -# Updated: 2021/02/11 20:13:29 by charles ### ########.fr # +# Updated: 2023/09/22 15:47:45 by leo ### ########.fr # # # -# ############################################################################ # +# **************************************************************************** # import re @@ -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( @@ -77,5 +81,7 @@ def hoist(content: str) -> str: lines = declarations if len(declarations) != 0: lines.append("") + print(declarations) + print(body) lines.extend(body) return "\n".join(lines) diff --git a/tests/formatters/test_hoist.py b/tests/formatters/test_hoist.py index b682cea..8b12a8a 100644 --- a/tests/formatters/test_hoist.py +++ b/tests/formatters/test_hoist.py @@ -3,10 +3,10 @@ # ::: :::::::: # # test_hoist.py :+: :+: :+: # # +:+ +:+ +:+ # -# By: root +#+ +:+ +#+ # +# By: leo +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/10/04 12:29:07 by cacharle #+# #+# # -# Updated: 2023/09/02 22:43:13 by root ### ########.fr # +# Updated: 2023/09/22 15:51:03 by leo ### ########.fr # # # # **************************************************************************** # @@ -203,6 +203,8 @@ def test_hoist_array_initialization(): { \tint a = 2; \tstatic int\txs[4] = {1, 2, 3, 4}; +\tconst int\tb = 2; +\tlong int\tc = 2; } """ output = """ @@ -210,8 +212,11 @@ def test_hoist_array_initialization(): { \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) From 6140a4f6b6bb10d28a17f228d7e2875ed4ae328f Mon Sep 17 00:00:00 2001 From: l Date: Fri, 22 Sep 2023 16:15:34 +0200 Subject: [PATCH 2/4] removed debugging prints --- c_formatter_42/formatters/hoist.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/c_formatter_42/formatters/hoist.py b/c_formatter_42/formatters/hoist.py index feb8f96..bff3fdc 100644 --- a/c_formatter_42/formatters/hoist.py +++ b/c_formatter_42/formatters/hoist.py @@ -6,7 +6,7 @@ # By: leo +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/10/04 11:16:28 by cacharle #+# #+# # -# Updated: 2023/09/22 15:47:45 by leo ### ########.fr # +# Updated: 2023/09/22 16:15:11 by leo ### ########.fr # # # # **************************************************************************** # @@ -81,7 +81,5 @@ def hoist(content: str) -> str: lines = declarations if len(declarations) != 0: lines.append("") - print(declarations) - print(body) lines.extend(body) return "\n".join(lines) From b95ad5f929f071bc682a28ae3a6ef742c24390c6 Mon Sep 17 00:00:00 2001 From: l Date: Fri, 22 Sep 2023 16:16:30 +0200 Subject: [PATCH 3/4] black --- c_formatter_42/formatters/hoist.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/c_formatter_42/formatters/hoist.py b/c_formatter_42/formatters/hoist.py index bff3fdc..8d12586 100644 --- a/c_formatter_42/formatters/hoist.py +++ b/c_formatter_42/formatters/hoist.py @@ -6,7 +6,7 @@ # By: leo +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/10/04 11:16:28 by cacharle #+# #+# # -# Updated: 2023/09/22 16:15:11 by leo ### ########.fr # +# Updated: 2023/09/22 16:16:01 by leo ### ########.fr # # # # **************************************************************************** # From 71ca8381b48f902ba293a7a7fb1520ae2de920b8 Mon Sep 17 00:00:00 2001 From: l Date: Sat, 23 Sep 2023 11:08:49 +0200 Subject: [PATCH 4/4] changed expected test output --- tests/formatters/test_align.py | 6 +++--- tests/formatters/test_hoist.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/formatters/test_align.py b/tests/formatters/test_align.py index b9274be..ccbdd8a 100644 --- a/tests/formatters/test_align.py +++ b/tests/formatters/test_align.py @@ -3,10 +3,10 @@ # ::: :::::::: # # test_align.py :+: :+: :+: # # +:+ +:+ +:+ # -# By: root +#+ +:+ +#+ # +# By: leo +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # 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 # # # # **************************************************************************** # @@ -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() { diff --git a/tests/formatters/test_hoist.py b/tests/formatters/test_hoist.py index 8b12a8a..875b6f3 100644 --- a/tests/formatters/test_hoist.py +++ b/tests/formatters/test_hoist.py @@ -6,7 +6,7 @@ # By: leo +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/10/04 12:29:07 by cacharle #+# #+# # -# Updated: 2023/09/22 15:51:03 by leo ### ########.fr # +# Updated: 2023/09/23 10:55:14 by leo ### ########.fr # # # # **************************************************************************** # @@ -197,7 +197,7 @@ def test_hoist_empty_function(): assert output == hoist(input) -def test_hoist_array_initialization(): +def test_hoist_initializations(): input = """ void foo(void) {