Skip to content

Commit f0f95e9

Browse files
authored
Merge pull request #401 from vil02/simplify_to_fortran90
style: shorten `to_fortran90`
2 parents 31c6452 + d0e005d commit f0f95e9

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "string-to-code"
3-
version = "0.4.0"
3+
version = "0.4.1"
44
description = "Generates a piece of messy code in a given language displaying a given string "
55
authors = ["piotr.idzik <[email protected]>"]
66
readme = "./string_to_code/README.md"

string_to_code/to_fortran90.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
def _atom_to_code(in_atom: core.Atom) -> str:
1313
if in_atom.atom_char == "\n":
1414
return "write (*, *)"
15-
res = 'write (*, "(A)", advance="no") '
16-
if in_atom.atom_char == '"':
17-
return res + "'\"'"
18-
if in_atom.atom_char == "\t":
19-
return res + "char(9)"
20-
21-
return res + f'"{in_atom.atom_char}"'
15+
special_chars = {
16+
'"': "'\"'",
17+
"\t": "char(9)",
18+
}
19+
return 'write (*, "(A)", advance="no") ' + special_chars.get(
20+
in_atom.atom_char, f'"{in_atom.atom_char}"'
21+
)
2222

2323

2424
_function_call_str = utils.get_function_call_str_fun(_get_function_name, "call ", "()")
@@ -32,9 +32,7 @@ def _atom_to_code(in_atom: core.Atom) -> str:
3232

3333

3434
def _merge_to_full_function(in_function_name: str, in_function_body: str) -> str:
35-
body_str = ""
36-
if in_function_body:
37-
body_str = in_function_body + "\n"
35+
body_str = in_function_body + "\n" if in_function_body else ""
3836
return f""" subroutine {in_function_name}()
3937
{body_str} end subroutine {in_function_name}
4038
"""
@@ -52,12 +50,12 @@ def _main_call_to_code(in_initial_call: core.InitialCall, **kwargs) -> str:
5250

5351

5452
def _join_to_final(main_call: str, function_definitions: list[str], **_kwargs) -> str:
55-
main_call_str = ""
56-
if main_call:
57-
main_call_str = "\n " + main_call
58-
contains_str = "\n"
59-
if function_definitions:
60-
contains_str = "\ncontains\n" + "\n".join(function_definitions)
53+
main_call_str = "\n " + main_call if main_call else ""
54+
contains_str = (
55+
"\ncontains\n" + "\n".join(function_definitions)
56+
if function_definitions
57+
else "\n"
58+
)
6159
return f"""program main
6260
implicit none{main_call_str}{contains_str}end program main
6361
"""

0 commit comments

Comments
 (0)