-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslator.py
More file actions
65 lines (51 loc) · 1.53 KB
/
Copy pathtranslator.py
File metadata and controls
65 lines (51 loc) · 1.53 KB
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
63
64
65
#!/usr/bin/env python3
import sys
import os
import ast
from core import Transpiler, TranspileError
def main():
if len(sys.argv) < 2:
print("Usage: python translator.py file1.py [file2.py ...] [output.cpp]")
sys.exit(1)
args = sys.argv[1:]
# ultimo argomento .cpp = output esplicito
if args[-1].endswith(".cpp"):
output_path = args[-1]
input_paths = args[:-1]
else:
input_paths = args
first = input_paths[0]
for ext in [".py", ".mpy"]:
if first.endswith(ext):
output_path = first[:-len(ext)] + ".cpp"
break
else:
output_path = first + ".cpp"
if not input_paths:
print("No input files provided.")
sys.exit(1)
modules = []
module_names = []
for p in input_paths:
with open(p, "r", encoding="utf-8") as f:
src = f.read()
try:
tree = ast.parse(src, filename=p)
except SyntaxError as e:
print(f"Syntax error in {p}: {e}")
sys.exit(1)
modules.append(tree)
base = os.path.basename(p)
name, _ = os.path.splitext(base)
module_names.append(name)
try:
transpiler = Transpiler(modules, module_names)
code = transpiler.transpile()
except TranspileError as e:
print("Transpile error:", e)
sys.exit(1)
with open(output_path, "w", encoding="utf-8") as f:
f.write(code)
print("Generated:", output_path)
if __name__ == "__main__":
main()