-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimised_code_generator.py
More file actions
387 lines (323 loc) · 14 KB
/
Copy pathoptimised_code_generator.py
File metadata and controls
387 lines (323 loc) · 14 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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import argparse
import sys
from parser import Token, Parser
from scanner import scanner
import_flag = False
aggr_methods = {
"average": "mean",
"sum": "sum",
"max": "max",
"min": "min",
"count": "nunique"
}
operator_map = {
'<>': '!=',
'=': '==',
}
path_map = {}
active_path = ""
active_tag = "a0"
declared_tags = []
declared_paths = {}
def convert_to_shift(expr):
num1, num2 = map(int, expr.split('*'))
shifts = []
position = 0
while num2 > 0:
if num2 & 1:
shifts.append(f"({num1} << {position})")
num2 >>= 1
position += 1
shift_expr = " + ".join(shifts)
return shift_expr
def generate_filter_expression(node, tag):
res = []
for i, child in enumerate(node.children):
if child.node_type == 'CONDITION':
res.append(generate_filter_expression(child, tag))
elif child.node_type == 'COLUMN':
if i > 0 and node.children[i - 1].node_type == 'OPERATOR':
res.append(child.value)
else:
res.append(f'{tag}[{child.value}]')
elif child.node_type == 'OPERATOR':
res.append(operator_map[child.value]
if operator_map.keys().__contains__(child.value)
else child.value)
elif child.node_type == 'NUMBER':
res.append(child.value)
return process_sub_expression(res)
def process_sub_expression(sub_expression):
if isinstance(sub_expression, str):
return sub_expression
if isinstance(sub_expression, list):
if len(sub_expression) == 3 and sub_expression[1] in '+-/%*':
if sub_expression[1] == '*':
return str(convert_to_shift(f"{sub_expression[0]} {sub_expression[1]} {sub_expression[2]}"))
else:
return str(eval(f"{sub_expression[0]} {sub_expression[1]} {sub_expression[2]}"))
elif len(sub_expression) == 3 and isinstance(sub_expression[1], str) and sub_expression[1] in ['&', '|']:
left_expr = process_sub_expression(sub_expression[0])
operator = sub_expression[1]
right_expr = process_sub_expression(sub_expression[2])
return f"({left_expr} {operator} {right_expr})"
else:
sub_expressions = [process_sub_expression(item) for item in sub_expression]
return f"({' '.join(sub_expressions)})"
def generate_python_code(node):
global import_flag
global active_path
global active_tag
code = ""
if node.node_type == "PROGRAM":
for child in node.children:
code += generate_python_code(child) + "\n"
elif node.node_type == "LOAD-STMT":
path = ""
header = ""
tag = "a0"
if not import_flag:
code += f"from pathlib import Path\n"
code += f"import pandas as pd\n\n"
import_flag = True
for child in node.children:
if child is None: continue
if child.node_type == "PATH":
path = child.value
elif child.node_type == "HEADER-ATTR":
header = f"{child.children[2].value}"
elif child.node_type == "TAG-ATTR":
tag = f"{child.children[2].value[1:-1]}"
path_map[tag] = path
active_path = path
active_tag = tag
declared_tags.append(tag)
if declared_paths.keys().__contains__(path) and declared_paths[path]['header'] == header:
code += f"{tag} = {declared_paths[path]['tag']}"
else:
declared_paths[path] = {'header': header, 'tag': tag}
code += f"{tag} = pd.read_csv({path}, header={'[0]' if header == 'true' else 'None'})"
elif node.node_type == "DISPLAY-STMT":
columns = []
num = None
sort_columns = []
filter_cond = None
tag = active_tag
col_index_flag = False
sort_col_index_flag = False
for child in node.children:
if child is None: continue
if child.node_type == "COLUMN-LIST" or child.node_type == "COL-INDEX-LIST":
if child.node_type == "COL-INDEX-LIST": col_index_flag = True
for val in child.children:
if val.node_type == "COLUMN-EXPR" or val.node_type == "COL-INDEX-EXPR":
code += f'a1 = {active_tag if tag != "a1" else tag}.copy()'
code += (f'\na1[{val.children[0].value if val.node_type == "COLUMN-EXPR" else int(val.children[0].value) - 1}] = '
f'a1[{val.children[0].value if val.node_type == "COLUMN-EXPR" else int(val.children[0].value) - 1}] {val.children[1].value} {val.children[2].value}\n')
tag = 'a1'
columns = [col.value.strip('"') if col.node_type == 'COLUMN' or col.node_type == 'COL-INDEX' else
col.children[0].value.strip('"')for col in child.children]
elif child.node_type == "NUM-ATTR":
num = child.children[2].value
elif child.node_type == "SORT-ATTR":
if child.children[2].node_type == 'COL-INDEX-LIST':
sort_col_index_flag = True
sort_columns = [int(col.value)-1 for col in child.children[2].children]
else:
sort_columns = [col.value.strip('"') for col in child.children[2].children]
elif child.node_type == "FILTER-ATTR":
filter_cond = child.children[2]
elif child.node_type == "TAG-ATTR":
tag = f"{child.children[2].value[1:-1]}" if tag != 'a1' else 'a1'
if sort_col_index_flag:
sort_columns = f'[{tag}.columns[i] for i in {sort_columns}]'
if filter_cond is None:
if num is None:
code += f'a4 = ({tag}.sort_values(by={sort_columns}))'
else:
code += f'a4 = ({tag}.sort_values(by={sort_columns}).head({num}))'
else:
if num is None:
code += (f'a4 = ({tag}.sort_values(by={sort_columns}).loc['
f'{generate_filter_expression(filter_cond, tag)}])')
else:
code += (f'a4 = ({tag}.sort_values(by={sort_columns}).loc['
f'{generate_filter_expression(filter_cond, tag)}].head'
f'({num}))')
code += f'\nprint(a4.loc[:, {columns}])' if col_index_flag == False\
else f'\nprint(a4.iloc[:, {[int(col)-1 for col in columns]}])'
code += "\nprint("")"
elif node.node_type == "STORE-STMT":
columns = []
num = None
sort_columns = []
filter_cond = None
tag = active_tag
col_index_flag = False
path = ""
for child in node.children:
if child is None: continue
if child.node_type == "COLUMN-LIST" or child.node_type == "COL-INDEX-LIST":
if child.node_type == "COL-INDEX-LIST": col_index_flag = True
for val in child.children:
if val.node_type == "COLUMN-EXPR" or val.node_type == "COL-INDEX-EXPR":
code += f'a1 = {active_tag if tag != "a1" else tag}.copy()'
code += (
f'\na1[{val.children[0].value if val.node_type == "COLUMN-EXPR" else int(val.children[0].value) - 1}] = '
f'a1[{val.children[0].value if val.node_type == "COLUMN-EXPR" else int(val.children[0].value) - 1}] {val.children[1].value} {val.children[2].value}\n')
tag = 'a1'
columns = [
col.value.strip('"') if col.node_type == 'COLUMN' or col.node_type == 'COL-INDEX' else col.children[
0].value.strip('"') for col in child.children]
elif child.node_type == "NUM-ATTR":
num = child.children[2].value
elif child.node_type == "SORT-ATTR":
sort_columns = [col.value.strip('"') for col in child.children[2].children]
elif child.node_type == "FILTER-ATTR":
filter_cond = child.children[2]
elif child.node_type == "TAG-ATTR":
tag = f"{child.children[2].value[1:-1]}" if tag != 'a1' else 'a1'
elif child.node_type == "PATH-ATTR":
path = child.children[2].value
if filter_cond is None:
if num is None:
code += f'a4 = ({tag}.sort_values(by={sort_columns}))'
else:
code += f'a4 = ({tag}.sort_values(by={sort_columns}).head({num}))'
else:
if num is None:
code += (f'a4 = ({tag}.sort_values(by={sort_columns}).loc['
f'{generate_filter_expression(filter_cond, tag)}])')
else:
code += (f'a4 = ({tag}.sort_values(by={sort_columns}).loc['
f'{generate_filter_expression(filter_cond, tag)}].head'
f'({num}))')
code += f'\na4.loc[:, {columns}].to_csv({path}, index=False)' if col_index_flag == False else f'\na4.iloc[:, {[int(col) - 1 for col in columns]}].to_csv({path}, index=False)'
code += '\nprint("")'
elif node.node_type == "PRINT-STMT":
message = ""
tag = active_tag
column = ""
aggr_method = ""
for child in node.children:
if child is None: continue
if child.node_type == "MESSAGE":
message = child.value
elif child.node_type == "AGGR-FUNC":
aggr_func = child.value
column = child.children[0]
aggr_method = aggr_methods[str(aggr_func).lower()]
elif child.node_type == "TAG-ATTR":
tag = f"{child.children[2].value[1:-1]}"
column = column.value if column.node_type == 'COLUMN' else f'{tag}.columns[{column.value}]'
code += f"print({message}, {tag}[{column}].{aggr_method}())" if aggr_method else f"print({message})"
elif node.node_type == "MERGE-STMT":
csv_list = []
path = ""
save = "false"
for child in node.children:
if child is None: continue
if child.node_type == "PATH-ATTR":
path = child.children[2].value
elif child.node_type == "SAVE-ATTR":
save = child.children[2].value
elif child.node_type == "TAG-LIST":
csv_list = [node.value[1:-1] for node in child.children]
code += f"print(pd.concat([{', '.join(csv_list)}]))\nprint("")" \
if save == "false" else f"pd.concat([{', '.join(csv_list)}]).to_csv({path}, index=False)"
elif node.node_type == "DELETE-STMT":
tag = ""
for child in node.children:
if child.node_type == "TAG-ATTR":
tag = f"{child.children[2].value[1:-1]}"
code += f'file_path = Path({path_map[tag]})'
code += f'\nfile_path.unlink()'
elif node.node_type == "CREATE-STMT":
path = ""
for child in node.children:
if child.node_type == "PATH":
path = child.value
active_path = path
active_tag = 'a0'
code += f'open({path}, "w").close()'
elif node.node_type == "ADD-STMT":
cols = []
for child in node.children[0].children:
if child.node_type == "TUPLE":
cols.append([child.value for child in child.children])
code += f'with open({active_path}, "a") as file:'
for col in cols:
code += f'\n\tfile.write("{",".join([val[1:-1] for val in col])}\\n")'
code += f"\n{active_tag} = pd.read_csv({active_path}, header=[0])"
elif node.node_type == "REMOVE-STMT":
cols = []
for child in node.children[0].children:
if child.node_type == "TUPLE":
cols.append([child.value for child in child.children])
formatted_cols = [
tuple(
float(val.strip('"')) if val.strip('"').replace('.', '', 1).isdigit() else val.strip('"')
for val in col
)
for col in cols
]
code += f'''
a2 = {active_tag}.apply(lambda row: tuple(row.dropna().values) in {formatted_cols}, axis=1)
{active_tag} = {active_tag}[~a2]
{active_tag}.to_csv({active_path}, index=False, header=False)
'''
return code
def optimize_code(code: str):
tags_to_be_removed = []
for tag in declared_tags:
if not(code.__contains__(f'{tag}.') or code.__contains__(f'{tag}[')):
tags_to_be_removed.append(tag)
filtered_code = "\n".join(
line for line in code.splitlines()
if not any(unwanted_substring in line for unwanted_substring in tags_to_be_removed)
)
return filtered_code + '\n'
def main():
arg_parser = argparse.ArgumentParser(description = "Code Generator for CSV Lang")
arg_parser.add_argument("file", help = "Path to the CSV Lang source code")
args = arg_parser.parse_args()
# Read the file
try:
with open(args.file, "r") as file:
source_code = file.read()
except FileNotFoundError:
print(f"\nError: File {args.file} not found.\n")
sys.exit(1)
global path_map
global declared_tags
global active_path
global active_tag
global declared_paths
path_map= {}
active_path = ""
active_tag = "a0"
declared_tags = []
declared_paths = {}
tokens, errors = scanner(source_code)
if errors is None or len(errors) == 0:
scanned_tokens = []
for token in tokens:
scanned_tokens.append(Token(token[0], token[1]))
parser = Parser(scanned_tokens)
ast, is_success = parser.parse()
if is_success:
global import_flag
import_flag = False
generated_code = optimize_code(generate_python_code(ast))
print("\nGenerated Python Code:\n")
print(generated_code)
print("CSVLang Output\n")
exec(generated_code)
print("")
else:
print("\nLexical Errors Found:\n")
for error in errors:
print(error)
print("")
if __name__ == "__main__":
main()