Skip to content

Commit 4f0db8f

Browse files
authored
Merge pull request #410 from vil02/add_to_kotlin
feat: add `to_kotlin`
2 parents e8ce1d8 + 94a433b commit 4f0db8f

File tree

8 files changed

+133
-1
lines changed

8 files changed

+133
-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.4.1"
3+
version = "0.5.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_kotlin.py

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