-
Notifications
You must be signed in to change notification settings - Fork 0
/
transpiler.py
135 lines (131 loc) · 5.65 KB
/
transpiler.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import sys
translated_code = """import sys, math\n"""
class FreeLangPyTranspiler:
def __init__(self, filename="main.free"):
global translated_code
self.filename = filename
self.tabs = ""
f = open(self.filename)
c = f.readlines()
self.code = []
for i in c:
if i != "":
self.code.append(i.removesuffix("\n"))
self.PC = 0
def run_code(self):
global translated_code
exec(translated_code)
def translate_code(self):
global translated_code
for self.PC in range(len(self.code)):
self.execute_command(self.code[self.PC])
return translated_code
def execute_command(self, line):
global translated_code
args = line.split(" ")
cmd = args.pop(0)
if cmd.startswith("#") or cmd.startswith("//") or cmd.startswith("--"): return
if line.replace(" ", "") == "": return
if cmd == "PRINT":
text = ""
for i in args:
if i.startswith("VAR:"):
i = i.removeprefix("VAR:")
if "::" in i:
x = i.split("::")
text += f"{x[0]}[{x[1]}]"+","
else:
text += f"{i}"+","
else:
text += f"'{i}'"+","
translated_code += self.tabs+f"print({text})"+"\n"
elif cmd == "VAR":
if args[0] == "INT":
var = args[2].removesuffix("-FREE")
translated_code += self.tabs+f"{args[1]} = int({var})"+"\n"
elif args[0] == "STR":
var = ""
for i in args[2:]: var += i+" "
translated_code += self.tabs+f"{args[1]} = str('{var}')"+"\n"
elif args[0] == "LIST":
var = "["
for i in args[2:]: var += i+","
else:
var = var.removesuffix(",")
var += "]"
translated_code += self.tabs+f"{args[1]} = {var}"+"\n"
elif args[0] == "LIST_OBJECT":
var = ""
x = args[1].split("::")
for i in args[2:]: var += i+" "
translated_code += self.tabs+f"{x[0]}.append({var})"+"\n"
elif cmd == "ADD":
args[0] = args[0].removeprefix("VAR:")
if args[1].endswith("-FREE"):
args[1] = args[1].removesuffix("-FREE")
translated_code += self.tabs+f"{args[0]} += {args[1]}"+"\n"
else:
translated_code += self.tabs+f"{args[0]} += '{args[1]}'"+"\n"
elif cmd == "SUB":
args[0] = args[0].removeprefix("VAR:")
args[1] = args[1].removesuffix("-FREE")
translated_code += self.tabs+f"{args[0]} -= {args[1]}"+"\n"
elif cmd == "MUL":
args[0] = args[0].removeprefix("VAR:")
args[1] = args[1].removesuffix("-FREE")
translated_code += self.tabs+f"{args[0]} *= {args[1]}"+"\n"
elif cmd == "DIV":
args[0] = args[0].removeprefix("VAR:")
args[1] = args[1].removesuffix("-FREE")
translated_code += self.tabs+f"{args[0]} /= {args[1]}"+"\n"
elif cmd == "POW":
args[0] = args[0].removeprefix("VAR:")
args[1] = args[1].removesuffix("-FREE")
translated_code += self.tabs+f"{args[0]} **= {args[1]}"+"\n"
elif cmd == "SQRT":
args[0] = args[0].removeprefix("VAR:")
translated_code += self.tabs+f"{args[0]} = math.sqrt({args[0]})"+"\n"
elif cmd == "IF":
if args[0].startswith("VAR:"): args[0] = args[0].removeprefix("VAR:")
if args[0].endswith("-FREE"): args[0] = args[0].removesuffix("-FREE")
if args[2].startswith("VAR:"):
args[2] = args[2].removeprefix('VAR:')
translated_code += self.tabs+f"if {args[0]} {args[1]} {args[2]}:"+"\n"
else:
if args[2].endswith("-FREE"):
args[2] = args[2].removesuffix('-FREE')
translated_code += self.tabs+f"if {args[0]} {args[1]} {args[2]}:"+"\n"
else:
translated_code += self.tabs+f"if {args[0]} {args[1]} '{args[2]}':"+"\n"
self.tabs += " "
elif cmd == "WHILE":
if args[0].startswith("VAR:"): args[0] = args[0].removeprefix("VAR:")
if args[0].endswith("-FREE"): args[0] = args[0].removesuffix("-FREE")
if args[2].startswith("VAR:"):
args[2] = args[2].removeprefix('VAR:')
translated_code += self.tabs+f"while {args[0]} {args[1]} {args[2]}:"+"\n"
else:
if args[2].endswith("-FREE"):
args[2] = args[2].removesuffix('-FREE')
translated_code += self.tabs+f"while {args[0]} {args[1]} {args[2]}:"+"\n"
else:
translated_code += self.tabs+f"while {args[0]} {args[1]} '{args[2]}':"+"\n"
self.tabs += " "
elif cmd == "LOOP":
translated_code += self.tabs+f"while True:"+"\n"
self.tabs += " "
elif cmd == "END":
try:
s_obj = slice(0, 4)
#print(self.tabs[s_obj])
except:
pass
elif cmd == "SET":
var = ""
x = args[0].split("::")
for i in args[1:]: var += i+" "
translated_code += self.tabs+f"{x[0]}[{x[1]}] = {var}"+"\n"
elif cmd == "QUIT":
translated_code += self.tabs+f"sys.exit()"+"\n"
else:
raise SyntaxError("Command not found")