Skip to content

Commit 76e3881

Browse files
committed
Refactor "compiler" script
1 parent 8a49385 commit 76e3881

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

compiler.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import argparse as ap
2+
import os
23
from pathlib import Path
3-
from re import DOTALL, sub, match
4+
from re import DOTALL, MULTILINE, match, sub
45
from typing import Union
5-
import os
66

77

88
def parse_args():
@@ -32,30 +32,35 @@ def remove_comments(text: str) -> str:
3232
"""
3333
lines = text.splitlines()
3434
# remove inline comments with regex
35-
for i, line in enumerate(lines):
36-
lines[i] = sub(r"(?: +)?//.*", "", line)
35+
lines = [sub(r"(?: +)?//.*", "", line) for line in lines]
3736
# Remove block comments
3837
text = "\n".join(lines)
3938
text = sub(r"/\*.*?\*/", "", text, flags=DOTALL)
39+
# Remove empty curlied-comment
40+
text = sub(r"^\{\s*\}", "", text, flags=DOTALL | MULTILINE)
4041
return text
4142

4243

4344
def remove_blank_lines(text: str) -> str:
4445
"""
45-
Remove multiple blank lines from output file to only one
46+
Remove multiple blank lines from the input text, ensuring only single blank lines remain.
4647
4748
Args:
4849
text (str): Text to sanitize
4950
5051
Returns:
5152
str: Sanitized text
5253
"""
53-
# if the line only contains whitespaces, remove it as well
54-
lines = text.splitlines()
55-
for i, line in enumerate(lines):
56-
lines[i] = sub(r"^\s+$", "", line)
54+
# Remove lines that contain only whitespace
55+
lines = [line for line in text.splitlines() if line.strip() != ""]
56+
57+
# Join lines and replace multiple newlines with a single newline
5758
text = "\n".join(lines)
58-
text = sub(r"[\n]+", "\n", text)
59+
text = sub(r"\n{2,}", "\n", text)
60+
61+
# Remove a leading newline if present
62+
text = text.lstrip("\n")
63+
5964
return text
6065

6166

@@ -83,6 +88,7 @@ def resolve_imports(script_path: Union[str, Path]) -> str:
8388

8489
return "".join(sli)
8590

91+
8692
def main():
8793
args = parse_args()
8894
script = resolve_imports(args.input)

0 commit comments

Comments
 (0)