forked from liquidiert/epal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepal_parser.py
304 lines (300 loc) · 15.8 KB
/
epal_parser.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import sys
import re
import os
def epal_parser():
args = sys.argv[1:]
variables = []
classes = []
functions = []
with open(str(args[0]), 'r') as parse_file:
cpp_output = str(args[0]).split(".")
with open(cpp_output[0] + ".cpp", 'w+') as parsed_file:
parsed_file.write("#include <iostream>\nusing namespace std;\n")
in_loop = False
if_case = False
switch = False
pre_line = None
block_comment = False
block_comment_index = 0
class_declaration = False
end_string = False
current_tabs = 0
for line in parse_file:
index = 0
line = line.split()
if block_comment:
if "end" in line:
parsed_file.write("*/\n")
block_comment = False
break
elif block_comment_index == 1:
comments = " ".join(line[1:])
parsed_file.write(comments + "\n")
block_comment_index += 1
else:
comments = " ".join(line)
parsed_file.write(comments + "\n")
block_comment_index += 1
else:
for word in line:
if re.match("[0-9]", word): # special characters section
operator = line[index - 1]
if not in_loop:
value = word
parsed_file.write(value + ";\n")
elif operator == "+" or operator == "add" or operator == "-" or operator == "sub" \
or operator == "*" or operator == "mul" or operator == "/" or operator == "div" \
or operator == "%" or operator == "mod" or operator == "is" or operator == "="\
and if_case:
value = word
parsed_file.write(value + ";\n")
index += 1
elif ' ' in line:
if len(line) > 3:
if line[index - 1] == " " or line[index + 1] == " ":
current_tabs += 1
else:
current_tabs += 1
index += 1
elif word == "class": # class section
if not len(line[2:]) == 0:
class_args = "(" + str(line[2:]) + ")"
else:
class_args = ""
classes.append(line[index + 1])
parsed_file.write("class " + line[index + 1] + " " + class_args + " {\n")
for i in range(int(current_tabs/4)):
parsed_file.write("\t")
parsed_file.write("private:\n") # classes are per default private
for i in range(int(current_tabs/4)):
parsed_file.write("\t")
class_declaration = True
index += 1
break
elif word == "public":
for i in range(current_tabs):
parsed_file.write("\t")
parsed_file.write("\npublic:\n")
for i in range(int(current_tabs/4)):
parsed_file.write("\t")
index += 1
elif "." in word:
parsed_file.write(word + " ")
elif word == "main": # main section
parsed_file.write("int main() {\n")
index += 1
elif word == "is" or word == "=": # operators
parsed_file.write(" = ")
index += 1
elif word == "add" or word == "+":
parsed_file.write(" + ")
index += 1
elif word == "sub" or word == "-":
parsed_file.write(" - ")
index += 1
elif word == "mul" or word == "*":
parsed_file.write(" * ")
index += 1
elif word == "div" or word == "/":
parsed_file.write(" / ")
index += 1
elif word == "mod" or word == "%":
if not if_case:
parsed_file.write(" % ")
index += 1
elif word == "equals" or word == "==":
if not if_case:
parsed_file.write(" == ")
index += 1
elif word == "nequal" or word == "!=":
parsed_file.write(" != ")
index += 1
elif word == "and" or word == "&&":
parsed_file.write(" && ")
index += 1
elif word == "or" or word == "||":
parsed_file.write("||")
index += 1
elif word == "end":
if switch:
switch = False
for i in range(int(current_tabs/4)):
parsed_file.write("\t")
parsed_file.write('default:\n\tcout << "Switch case error" << endl;\n}\n')
elif class_declaration:
class_declaration = False
parsed_file.write("};\n")
elif in_loop:
parsed_file.write("}\n")
in_loop = False
elif if_case:
parsed_file.write("}\n")
if_case = False
else:
for i in range(int(current_tabs/4)):
parsed_file.write("\t")
parsed_file.write("}\n")
current_tabs = 0
index += 1
elif word == "do": # useless words
index += 1
pass
elif word == "for":
index += 1
elif word == "in":
index += 1
elif word == "range":
index += 1
elif word == "loop": # loop section
loop_value = 0
iter_var = None
for inner_word in line:
try:
loop_value = int(inner_word)
except ValueError:
pass
if len(inner_word) == 1:
iter_var = inner_word
if "for" in line:
for i in range(int(current_tabs/4)):
parsed_file.write("\t")
parsed_file.write("for (int " + iter_var + " = 0; " + iter_var
+ " < " + str(loop_value) + "; " + iter_var + "++) {\n")
for i in range(int(current_tabs/4)):
parsed_file.write("\t")
else:
for i in range(int((current_tabs/4))):
parsed_file.write("\t")
parsed_file.write("while (" + iter_var + " > " + str(loop_value) + ") {\n")
for i in range(int(current_tabs/4)):
parsed_file.write("\t")
in_loop = True
index += 1
break
elif word == "if": # if section
if_case = True
conditions = " ".join(line[1:])
conditions = conditions.replace("equals", " == ")
for i in range(int(current_tabs/4)):
parsed_file.write("\t")
parsed_file.write("if (")
parsed_file.write(conditions)
parsed_file.write(") {\n")
for i in range(int(current_tabs/4)):
parsed_file.write("\t")
index += 1
break
elif word == "else":
for i in range(int(current_tabs/4)):
parsed_file.write("\t")
parsed_file.write("else {\n")
for i in range(int(current_tabs/4)):
parsed_file.write("\t")
index += 1
elif word == "elif":
conditions = " ".join(line[1:])
conditions = conditions.replace("equals", " == ")
for i in range(int(current_tabs/4)):
parsed_file.write("\t")
parsed_file.write(" else if (" + conditions + ") {\n")
for i in range(int(current_tabs/4)):
parsed_file.write("\t")
index += 1
elif word == "print": # builtins
class_var = None
try:
class_var = line[index + 1].split(".")[1]
except IndexError:
pass
if line[index + 1] in variables:
for i in range(int(current_tabs/4)):
parsed_file.write("\t")
parsed_file.write("cout << " + line[index + 1] + " << endl;\n")
elif class_var in variables:
for i in range(int(current_tabs/4)):
parsed_file.write("\t")
parsed_file.write("cout << " + line[index + 1] + " << endl;\n")
else:
for i in range(int(current_tabs/4)):
parsed_file.write("\t")
parsed_file.write('cout << "' + line[index + 1] + '" << endl;\n')
index += 1
break
elif word == "switch": # switch section
if_case = True
in_loop = True
switch = True
for i in range(int(current_tabs/4)):
parsed_file.write("\t")
parsed_file.write("switch (" + str(line[index + 1]) + ") {\n")
for i in range(int(current_tabs/4)):
parsed_file.write("\t")
index += 1
break
elif word == "break":
for i in range(int(current_tabs/4)):
parsed_file.write("\t")
parsed_file.write("break;\n")
index += 1
elif word == "case":
parsed_file.write("case " + str(line[index + 1]) + ":\n")
index += 1
elif word == "//": # comment section
comments = " ".join(line[1:])
parsed_file.write("//" + str(comments) + "\n")
index += 1
break
elif word == "/*":
block_comment = True
block_comment_index += 1
parsed_file.write("/* ")
index += 1
else: # default case
variable_name = word
class_case = False
for inner_word in line:
if inner_word in classes:
class_case = True
if class_case:
parsed_file.write(line[index + 2] + " " + word + " = " + line[index + 2] + "();\n")
break
elif not in_loop and not if_case:
try:
test_value = None
try:
test_value = int(line[index + 2])
except:
pass
if isinstance(test_value, int):
for i in range(int(current_tabs / 4)):
parsed_file.write("\t")
parsed_file.write("int " + variable_name)
if variable_name not in variables:
variables.append(variable_name)
elif isinstance(line[index + 2], str):
for i in range(int(current_tabs / 4)):
parsed_file.write("\t")
parsed_file.write("string " + variable_name)
end_string = True
if variable_name not in variables:
variables.append(variable_name)
except IndexError:
if not end_string:
parsed_file.write(variable_name)
else:
parsed_file.write('"' + variable_name + '";\n')
if variable_name not in variables:
variables.append(variable_name)
else:
if word not in pre_line:
for i in range(int(current_tabs / 4)):
parsed_file.write("\t")
parsed_file.write(variable_name)
if variable_name not in variables:
variables.append(variable_name)
index += 1
pre_line = line
parsed_file.write("\treturn 0;\n}")
os.system("g++ -std=c++17 -g " + cpp_output[0] + ".cpp -o " + sys.argv[2])
if __name__ == "__main__":
epal_parser()