|
| 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 | +) |
0 commit comments