-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathto_algol68.py
60 lines (40 loc) · 1.71 KB
/
to_algol68.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
provides string_to_algol68 utilities
"""
from . import core
from . import utils
_get_function_name = utils.get_function_name_fun("p")
def atom_to_code(in_atom: core.Atom) -> str:
"""
returns a string/piece of ALGOL 68 code resulting in printing the
in_atom.atom_char to the standard output
"""
special_char_dict = {"\n": "newline", '"': '""""'}
res_char = special_char_dict.get(in_atom.atom_char, f'"{in_atom.atom_char}"')
return f"print({res_char});"
_function_call_str = utils.get_function_call_str_fun(_get_function_name, "", ";")
_call_function_or_atom = utils.get_call_function_or_atom(
atom_to_code, _function_call_str
)
_body_to_str = utils.get_body_to_str("\n", " ", _call_function_or_atom, "", "")
def _merge_to_full_function(in_function_name: str, in_function_body: str) -> str:
body_str = ""
if in_function_body:
assert in_function_body[-1] == ";" # nosec B101
body_str = "\n" + in_function_body[:-1] + "\n"
return f"PROC {in_function_name} = VOID :({body_str});\n"
_function_to_code = utils.get_function_to_code(
_get_function_name, _body_to_str, _merge_to_full_function
)
def _main_call_to_code(in_initial_call: core.InitialCall, **kwargs) -> str:
if in_initial_call is None:
return 'print("")'
res = _call_function_or_atom(in_initial_call, **kwargs)
assert res[-1] == ";" # nosec B101
return res[:-1]
def _join_to_final(main_call: str, function_definitions: list[str], **_kwargs) -> str:
assert main_call[-1] != "\n" # nosec B101
return "\n\n".join(function_definitions + [main_call + "\n"])
proc_printer_program, proc = utils.get_all_proc_functions(
_main_call_to_code, _function_to_code, _join_to_final
)