-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtranspile_operations.py
More file actions
80 lines (73 loc) * 2.89 KB
/
transpile_operations.py
File metadata and controls
80 lines (73 loc) * 2.89 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
READ AND INSTALL ONLY.
DO NOT COPY AND RELEASE AS YOUR OWN.
If you want to improve it, fork and contribute, or write from scratch.
"""
tkmsg.showinfo("Compile", "Compiling. This may take some time.")
toParse = text_box.get(1.0, END)
lineBreak = """
""" # This functions.
parse_dict = {
"True": "true",
"False": "false",
"#end": "end",
"#class": "}",
"def": "local function",
"None": "nil",
"str(": "tostring(",
"int(": "tonumber(",
"!=": "~=",
"#": "--",
"eval(": "loadstring(",
"exec(": "loadstring(",
" + ": " .. ",
"elif": "elseif",
"time.sleep(": "wait(",
"random.randint(": "math.random(",
"random.randrange(": "math.random(",
".replace(": ":gsub(",
".format(": "(:format(" # for some weird reason, lua wants to end the function before formatting.
}
# All above this are transpiled with NO condition.
parser2 = {"prtonumber": "print"} # Second parsing with NO condition, done after the first.
# SPLIT
split_array = toParse.split(lineBreak)
split_length = len(split_array)
finished = text_box.get(1.0, END)
for k, v in parse_dict.items():
finished = finished.replace(k, v)
for k,v in parser2.items():
finished = finished.replace(k, v)
# SPLIT
split_array = finished.split(lineBreak)
result_array = []
for line in split_array:
if "#exclude" not in line:
if "while" in line:
result_array.append(line.replace(":", " do"))
elif "if" in line:
result_array.append(line.replace(":", " then"))
elif "class" in line:
result_array.append(line.replace("class ", "local ").replace(":", " = {"))
elif " = " in line and " " not in line and " " not in line: # doublespace and tab.
result_array.append(line.replace(line, "local " + line)\
.replace("[", "{").replace("]", "}"))
elif "else:" in line:
result_array.append(line.replace(":", ""))
elif "max(" in line and ")" in line:
result_array.append(line.replace("max(", "math.max("))
elif "min(" in line and ")" in line:
result_array.append(line.replace("min(", "math.min("))
elif "lamba" in line and ":" in line and " " in line:
result_array.append(line.replace("lamda", "function").replace(" ", "(").replace(":", ")"))
elif "lambda" in line and ":" in line and " " not in line:
result_array.append(line.replace("lambda", "function").replace(":", "()"))
elif "for " in line and ":" in line:
result_array.append(line.replace(":", " do"))
else:
result_array.append(line)
text_box.delete(1.0, END)
text_box.insert(1.0, '-- Result:\n' + "\n".join(result_array))
"""Optional to write file directly in dir."""
#newFile = open("result.txtl", "+w")
#newFile.write('\n'.join(result_array))