-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathimport_to_call.py
executable file
·62 lines (55 loc) · 2.11 KB
/
import_to_call.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
61
62
#!/bin/python3.9
import sys
from typing import List
def get_args(sig: str):
start_index = sig.index("(", sig.index("fn ") + 3) + 1
end_index = sig.index(")", start_index)
arg_list = sig[start_index:end_index]
out_args: List[str] = []
arg_count = arg_list.count(": ")
current_arg = 0
while current_arg < arg_count:
end_index = arg_list.index(": ")
out_args.append(arg_list[:end_index])
if current_arg + 1 != arg_count:
start_index = arg_list.index(", ") + 2
arg_list = arg_list[start_index:]
current_arg += 1
return out_args
def get_name(sig: str):
start_index = sig.index("fn ") + 3
end_index = sig.index("(", start_index)
return sig[start_index:end_index]
if len(sys.argv) != 3:
print("./import_to_call.py <infile> <outfile>")
sys.exit(2)
in_file = open(sys.argv[1], 'r')
out_file = open(sys.argv[2], 'w')
lines = in_file.readlines()
in_file.close()
out_file.write("use crate::*;\n\nmod impl_ {\n use crate::*;\n\n extern \"C\" {\n")
filtered_lines: List[str] = []
for line in lines:
out_file.write(" " + line)
line = line.strip()
if not line.startswith("pub(super) fn"):
continue
filtered_lines.append(line)
out_file.write(" }\n}\n\n")
for line in filtered_lines:
params = get_args(line)
func_name = get_name(line)
func_call = "pub fn" + line.removeprefix("pub(super) fn")
func_call = func_call.removesuffix(";") + " {"
returns_vec = func_call.endswith("-> cpp::simd::Vector2 {") or func_call.endswith("-> cpp::simd::Vector3 {") or func_call.endswith("-> cpp::simd::Vector4 {")
func_call = func_call.replace("-> cpp::simd::Vector", "-> phx::Vec").replace("*const ", "&").replace("*mut", "&mut").replace("&mut lua_State", "*mut lua_State")
func_call += "\n unsafe {\n impl_::" + func_name + "("
for idx, param in enumerate(params):
func_call += param
if idx + 1 != len(params):
func_call += ", "
func_call += ")"
if returns_vec:
func_call += ".into()"
func_call += "\n }\n}"
out_file.write(func_call + "\n\n")