Skip to content

Commit c17a194

Browse files
committed
feat: add to_kotlin
1 parent e8ce1d8 commit c17a194

File tree

7 files changed

+133
-0
lines changed

7 files changed

+133
-0
lines changed

string_to_code/to_kotlin.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""
2+
provides string_to_rust 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 "println()"
15+
special_chars = {
16+
'"': '\\"',
17+
"\\": "\\\\",
18+
"\t": "\\t",
19+
"{": "{",
20+
"}": "}",
21+
}
22+
res_char = special_chars.get(in_atom.atom_char, in_atom.atom_char)
23+
return f"print('{res_char}')"
24+
25+
26+
_function_call_str = utils.get_function_call_str_fun(_get_function_name, "", "()")
27+
28+
29+
_call_function_or_atom = utils.get_call_function_or_atom(
30+
_atom_to_code, _function_call_str
31+
)
32+
33+
_EMPTY_BODY = " // No code needed"
34+
35+
_body_to_str = utils.get_body_to_str(
36+
"\n", " ", _call_function_or_atom, "", _EMPTY_BODY
37+
)
38+
39+
40+
def _merge_to_full_function(in_function_name, in_function_body):
41+
body_str = ""
42+
if in_function_body:
43+
body_str = "\n" + in_function_body + "\n"
44+
return f"fun {in_function_name}() {{{body_str}}}\n"
45+
46+
47+
_function_to_code = utils.get_function_to_code(
48+
_get_function_name, _body_to_str, _merge_to_full_function
49+
)
50+
51+
52+
def _main_call_to_code(in_initial_call, **kwargs):
53+
call_in_main = (
54+
"\n" + _EMPTY_BODY
55+
if in_initial_call is None
56+
else "\n " + _call_function_or_atom(in_initial_call, **kwargs)
57+
)
58+
return "fun main() {" + call_in_main + "\n}\n"
59+
60+
61+
def _join_to_final(main_call, function_definitions, **_kwargs):
62+
function_definitions_str = (
63+
"\n".join(function_definitions) if function_definitions else ""
64+
)
65+
if function_definitions_str:
66+
function_definitions_str = function_definitions_str + "\n"
67+
return function_definitions_str + main_call
68+
69+
70+
proc_printer_program, proc = utils.get_all_proc_functions(
71+
_main_call_to_code, _function_to_code, _join_to_final
72+
)
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 default-jdk kotlin
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

tests/setup_kotlin.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
setup for the tests of the module string_to_kotlin
3+
"""
4+
5+
import general_utilities as gu
6+
7+
from string_to_code import to_kotlin
8+
9+
_KOTLINC = "kotlinc"
10+
11+
_FILE_EXTENSION = "kt"
12+
_JAVA = "java"
13+
14+
15+
def _compile_code(in_code, tmp_folder):
16+
source_filename = gu.get_unique_filename(tmp_folder, _FILE_EXTENSION)
17+
gu.save_str_to_file(tmp_folder / source_filename, in_code)
18+
jar_filename = gu.get_unique_filename(tmp_folder, "jar")
19+
20+
gu.subprocess_run_with_check(
21+
[
22+
_KOTLINC,
23+
"-Werror",
24+
"-progressive",
25+
source_filename,
26+
"-d",
27+
jar_filename,
28+
],
29+
cwd=str(tmp_folder),
30+
)
31+
return jar_filename
32+
33+
34+
def _run_jar(jar_filename: str, tmp_folder):
35+
return gu.subprocess_run_with_check(
36+
[_JAVA, "-jar", jar_filename],
37+
cwd=str(tmp_folder),
38+
capture_output=True,
39+
text=True,
40+
)
41+
42+
43+
def _run_code(in_code: str, tmp_folder):
44+
return _run_jar(_compile_code(in_code, tmp_folder), tmp_folder)
45+
46+
47+
def get_test_data():
48+
"""returns test data for the module string_to_kotlin"""
49+
return gu.Language(
50+
tool_names=[_KOTLINC, _JAVA],
51+
string_to_code=to_kotlin.proc,
52+
printer_program_to_code=to_kotlin.proc_printer_program,
53+
run_code=_run_code,
54+
id="kotlin",
55+
source_code_file_extension=_FILE_EXTENSION,
56+
)

0 commit comments

Comments
 (0)