Skip to content

Commit cb5d084

Browse files
authored
Merge pull request #391 from vil02/add_to_lua
feat: add `to_lua`
2 parents 8342bea + dfcc512 commit cb5d084

File tree

10 files changed

+167
-21
lines changed

10 files changed

+167
-21
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.2.15"
3+
version = "0.3.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_lua.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
"""
2+
provides string_to_lua utilities
3+
"""
4+
5+
from . import core
6+
from . import utils
7+
8+
9+
def _get_table_name(**kwargs) -> str:
10+
return kwargs.get("table_name", "main")
11+
12+
13+
def _get_function_name(in_function_id: int, **kwargs) -> str:
14+
prefix = _get_table_name(**kwargs) + "."
15+
return prefix + kwargs.get("function_id_to_name", core.get_function_namer("fun_"))(
16+
in_function_id
17+
)
18+
19+
20+
def atom_to_code(in_atom: core.Atom) -> str:
21+
"""
22+
returns a string/piece of lua code resulting in printing the
23+
in_atom.atom_char to the standard output
24+
"""
25+
if in_atom.atom_char == "\n":
26+
return "print()"
27+
if in_atom.atom_char == '"':
28+
return "io.write('\"')"
29+
special_chars = {"\\": "\\\\", "\t": "\\t"}
30+
res_char = special_chars.get(in_atom.atom_char, in_atom.atom_char)
31+
write_arg = f'"{res_char}"'
32+
return f"io.write({write_arg})"
33+
34+
35+
_function_call_str = utils.get_function_call_str_fun(_get_function_name, "", "()")
36+
37+
_call_function_or_atom = utils.get_call_function_or_atom(
38+
atom_to_code, _function_call_str
39+
)
40+
41+
42+
_body_to_str = utils.get_body_to_str("\n", "\t", _call_function_or_atom, "", "")
43+
44+
45+
def _merge_to_full_function(in_function_name, in_function_body):
46+
function_body = " "
47+
if in_function_body:
48+
function_body = "\n" + in_function_body + "\n"
49+
return f"function {in_function_name}()" + function_body + "end\n"
50+
51+
52+
_function_to_code = utils.get_function_to_code(
53+
_get_function_name, _body_to_str, _merge_to_full_function
54+
)
55+
56+
57+
def _main_call_to_code(in_initial_call, **kwargs):
58+
return (
59+
_call_function_or_atom(in_initial_call, **kwargs) + "\n"
60+
if in_initial_call is not None
61+
else ""
62+
)
63+
64+
65+
def _join_to_final(main_call, function_definitions, **kwargs):
66+
res = "\n".join(function_definitions + [main_call])
67+
if function_definitions:
68+
res = "local " + _get_table_name(**kwargs) + " = {}\n\n" + res
69+
return res
70+
71+
72+
proc_printer_program, proc = utils.get_all_proc_functions(
73+
_main_call_to_code, _function_to_code, _join_to_final
74+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
apt-get install -y --no-install-recommends luarocks
6+
luarocks install luacheck
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
local main = {}
2+
3+
function main.fun_0()
4+
io.write("{")
5+
io.write("%")
6+
end
7+
8+
function main.fun_1()
9+
main.fun_0()
10+
print()
11+
main.fun_0()
12+
end
13+
14+
main.fun_1()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
local main = {}
2+
3+
function main.fun_0() end
4+
5+
function main.fun_1()
6+
main.fun_0()
7+
io.write("C")
8+
end
9+
10+
main.fun_1()
File renamed without changes.

tests/setup_lua.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
setup for the tests of the module string_to_lua
3+
"""
4+
5+
import general_utilities as gu
6+
7+
from string_to_code import to_lua
8+
9+
10+
def get_lua():
11+
"""returns the name of the lua interpreter"""
12+
return "lua"
13+
14+
15+
def get_luacheck():
16+
"""returns the name of luacheck"""
17+
return "luacheck"
18+
19+
20+
def _get_source_code_file_extension():
21+
return "lua"
22+
23+
24+
def run_lua_code(in_code, tmp_folder):
25+
"""
26+
Runs the lua code in_code.
27+
Returns the output of the program.
28+
"""
29+
source_filename = gu.get_unique_filename(
30+
tmp_folder, _get_source_code_file_extension()
31+
)
32+
gu.save_str_to_file(tmp_folder / source_filename, in_code)
33+
34+
gu.subprocess_run_with_check(
35+
[
36+
get_luacheck(),
37+
source_filename,
38+
],
39+
cwd=str(tmp_folder),
40+
capture_output=True,
41+
text=True,
42+
)
43+
44+
res = gu.subprocess_run_with_check(
45+
[get_lua(), source_filename],
46+
cwd=str(tmp_folder),
47+
capture_output=True,
48+
text=True,
49+
)
50+
return res
51+
52+
53+
def get_test_data():
54+
"""returns test data for the module string_to_lua"""
55+
return gu.Language(
56+
tool_names=[get_lua(), get_luacheck()],
57+
string_to_code=to_lua.proc,
58+
printer_program_to_code=to_lua.proc_printer_program,
59+
run_code=run_lua_code,
60+
id="lua",
61+
source_code_file_extension=_get_source_code_file_extension(),
62+
)

tests/unused_example_data/lua/function_used_twice.lua

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/unused_example_data/lua/one_empty_function.lua

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)