Skip to content

Commit 31c6452

Browse files
authored
Merge pull request #400 from vil02/add_fortran90
feat: add `to_fortran90`
2 parents ca74210 + 2666d6e commit 31c6452

File tree

8 files changed

+127
-1
lines changed

8 files changed

+127
-1
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.3.0"
3+
version = "0.4.0"
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: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""
2+
provides string_to_fortran90 utilities
3+
"""
4+
5+
from . import core
6+
from . import utils
7+
8+
9+
_get_function_name = utils.get_function_name_fun("fun_")
10+
11+
12+
def _atom_to_code(in_atom: core.Atom) -> str:
13+
if in_atom.atom_char == "\n":
14+
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}"'
22+
23+
24+
_function_call_str = utils.get_function_call_str_fun(_get_function_name, "call ", "()")
25+
26+
_call_function_or_atom = utils.get_call_function_or_atom(
27+
_atom_to_code, _function_call_str
28+
)
29+
30+
31+
_body_to_str = utils.get_body_to_str("\n", " ", _call_function_or_atom, "", "")
32+
33+
34+
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"
38+
return f""" subroutine {in_function_name}()
39+
{body_str} end subroutine {in_function_name}
40+
"""
41+
42+
43+
_function_to_code = utils.get_function_to_code(
44+
_get_function_name, _body_to_str, _merge_to_full_function
45+
)
46+
47+
48+
def _main_call_to_code(in_initial_call: core.InitialCall, **kwargs) -> str:
49+
if in_initial_call is None:
50+
return ""
51+
return _call_function_or_atom(in_initial_call, **kwargs)
52+
53+
54+
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)
61+
return f"""program main
62+
implicit none{main_call_str}{contains_str}end program main
63+
"""
64+
65+
66+
proc_printer_program, proc = utils.get_all_proc_functions(
67+
_main_call_to_code, _function_to_code, _join_to_final
68+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
apt-get install -y --no-install-recommends gfortran

tests/unused_example_data/fortran90/function_used_twice.f90 renamed to tests/example_data/fortran90/function_used_twice.f90

File renamed without changes.

tests/unused_example_data/fortran90/one_empty_function.f90 renamed to tests/example_data/fortran90/one_empty_function.f90

File renamed without changes.
File renamed without changes.
File renamed without changes.

tests/setup_fortran90.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
setup for the tests of the module string_to_fortran90
3+
"""
4+
5+
import general_utilities as gu
6+
7+
from string_to_code import to_fortran90
8+
9+
_GFOTRAN = "gfortran"
10+
11+
_FILE_EXTENSION = "f90"
12+
13+
14+
def _compile_code(in_code, tmp_folder):
15+
source_filename = gu.get_unique_filename(tmp_folder, _FILE_EXTENSION)
16+
gu.save_str_to_file(tmp_folder / source_filename, in_code)
17+
executable_filename = gu.get_unique_filename(tmp_folder, "o")
18+
19+
gu.subprocess_run_with_check(
20+
[
21+
_GFOTRAN,
22+
"-Wall",
23+
"-Wextra",
24+
"-Wpedantic",
25+
"-Waliasing",
26+
"-Wconversion-extra",
27+
"-Wimplicit-interface",
28+
"-Wimplicit-procedure",
29+
"-Wsurprising",
30+
"-Werror",
31+
source_filename,
32+
"-o",
33+
executable_filename,
34+
],
35+
cwd=str(tmp_folder),
36+
)
37+
return executable_filename
38+
39+
40+
def _run_code(in_code, tmp_folder):
41+
return gu.run_executable(_compile_code(in_code, tmp_folder), tmp_folder)
42+
43+
44+
def get_test_data():
45+
"""returns test data for the module string_to_fortran90"""
46+
return gu.Language(
47+
tool_names=[_GFOTRAN],
48+
string_to_code=to_fortran90.proc,
49+
printer_program_to_code=to_fortran90.proc_printer_program,
50+
run_code=_run_code,
51+
id="fortran90",
52+
source_code_file_extension=_FILE_EXTENSION,
53+
)

0 commit comments

Comments
 (0)