-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparsing.py
107 lines (93 loc) · 3.38 KB
/
parsing.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
from device import*
from control import*
class Parsing(object):
"""docstring for Parsing"""
def __init__(self, file_name):
super(Parsing, self).__init__()
self.file_name = file_name
self.line_num = 0
self.line_type = ''
self.element_list = []
self.control_list = {'options':[], 'output':[], 'analysis': []}
self.handle_file(self.file_name)
def handle_file(self, file_name):
file_handle = open(file_name, 'r')
while 1:
line = file_handle.readline()
if not line: break
if self.parsing_line(line):
break
file_handle.close()
def parsing_line(self, line):
self.line_num += 1
if (self.line_num==1): return 0
line = line.strip().lower()
line_elem = line.split()
if not line_elem: return 0
#element line
if line_elem[0][0] >= 'a' and line_elem[0][0] <= 'z':
self.line_type = 'element'
device_type = self.type_of_elem(line_elem[0][0])
self.element_list += [device_type(line_elem)]
#control line
elif line_elem[0][0] == '.':
line_elem[0] = line_elem[0][1:]
if line_elem[0] == 'end':
self.line_type = 'end'
return 1
self.line_type = 'control'
ctrl_type = self.type_of_ctrl(line_elem[0])
if line_elem[0] == 'options':
self.control_list['options'] += [ctrl_type(line_elem)]
elif line_elem[0] in {'print','plot','probe'}:
self.control_list['output'] += [ctrl_type(line_elem)]
else:
self.control_list['analysis'] += [ctrl_type(line_elem)]
#comment line
elif line_elem[0][0] == '*':
self.line_type = 'comment'
#continuing line
elif line_elem[0][0] == '+':
line_elem[0] = line_elem[0][1:]
if self.line_type == 'element':
tmp = self.element_list[len(self.element_list)-1]
line_elem = tmp.arg + line_elem
self.element_list[len(self.element_list)-1] = type(tmp)(line_elem)
elif self.line_type == 'control':
tmp = self.control_list[len(self.control_list)-1]
line_elem = tmp.arg + line_elem
self.control_list[len(self.control_list)-1] = type(tmp)(line_elem)
elif self.line_type == 'comment':
pass
else:
pass
return 0
def type_of_elem(self, elem_name):
DEVICE_DICT = {
'r': Resistor,
'c': Capacitor,
'l': Inductor,
'v': Voltage_Source,
'i': Current_Source,
'e': VCVS,
'f': CCCS,
'g': VCCS,
'h': CCVS,
'd': Diode,
'm': MOSFET,
'x': MEMRISTOR,
}
return DEVICE_DICT.get(elem_name, Device)
def type_of_ctrl(self, ctrl_name):
CTRL_DICT = {
'options': Options,
'dc': DC,
'ac': AC,
'tran': TRAN,
'op': OP,
'ic': IC,
'print': PRINT,
'plot': PLOT,
'probe': PROBE,
}
return CTRL_DICT.get(ctrl_name, Control)