-
Notifications
You must be signed in to change notification settings - Fork 4
/
CircuitParser.py
70 lines (54 loc) · 2.41 KB
/
CircuitParser.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
class CircuitParser:
def __init__(self, input_file_path, output_file_path):
self.input_file_path = input_file_path
self.output_file_path = output_file_path
self.new_n_dict = {}
def parse_circuit(self):
with open(self.input_file_path, 'r') as f:
lines = f.readlines()
comments = lines[0].rstrip()
lines = lines[1:]
output = []
in_order = ""
out_order = ""
current_line = ""
for line in lines:
line = line.strip()
current_line += " " + line
if line.endswith(";"):
if current_line.startswith(" INORDER"):
in_order += current_line
elif current_line.startswith(" OUTORDER"):
out_order += current_line
elif current_line.startswith(" new_n"):
new_n_name, new_n_expr = current_line.split(" = ")
self.new_n_dict[new_n_name.strip()] = new_n_expr.strip(";")
else:
output.append(self.replace_new_n(current_line))
current_line = ""
# Add INORDER and OUTORDER lines
output.insert(0, in_order.strip())
output.insert(1, out_order.strip())
for key in self.new_n_dict:
self.new_n_dict[key] = self.replace_new_n(self.new_n_dict[key])
output = [self.replace_new_n(line).lstrip() for line in output ]
output[2:] = [f"{expr.split('=')[0]} = ({expr.split('=')[1].replace(';', '')});" for expr in output[2:]]
# for `!` replace to `! `
output[2:] = [expr.replace("!", "! ") for expr in output[2:]]
# insert comments in the beginning
output.insert(0, comments)
return "\n".join(output)
def replace_new_n(self, expr):
for key in self.new_n_dict:
expr = expr.replace(key, "("+self.new_n_dict[key]+")")
return expr
def write_to_file(self, content):
with open(self.output_file_path, 'w') as f:
f.write(content)
def process(self):
parsed_content = self.parse_circuit()
self.write_to_file(parsed_content)
# input_file_path = "/data/guangyuh/coding_env/E-Brush/test_data/raw_circuit.txt"
# output_file_path = "/data/guangyuh/coding_env/E-Brush/test_data/original_circuit.txt"
# parser = CircuitParser(input_file_path, output_file_path)
# parser.process()