-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmod_ply.py
332 lines (256 loc) · 14.7 KB
/
mod_ply.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
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
#Embedded file name: /build/PyCLIP/android/app/mod_ply.py
import sys
sys.path.insert(0, '../..')
sys.path.insert(0, './ply')
import ply.lex as lex
import ply.yacc as yacc
import time
class Parser():
tokens = ()
precedence = ()
def __init__(self, **kw):
self.names = {}
lex.lex(module=self)
yacc.yacc(module=self)
def calculate(self, value):
return yacc.parse(value, debug=0)
class Calc(Parser):
result = ''
tokens = ('NAME', 'HEXSTR', 'NUMBER', 'HEX', 'FLOAT', 'PLUS', 'MINUS', 'BAND', 'EXP', 'TIMES', 'DIVIDE', 'EQUALS', 'NEQ', 'LPAREN', 'RPAREN', 'LT', 'GT', 'GE', 'LE', 'AND', 'OR', 'SHIFT', 'QUEST', 'DOTS', 'STRCONST', 'HEXTOASCII', 'HEXTOSTR', 'HEXTODEC')
t_OR = '\\|\\|'
t_AND = '\\&\\&'
t_BAND = '\\&'
t_EQUALS = '=='
t_NEQ = '\\!='
t_PLUS = '\\+'
t_MINUS = '-'
t_EXP = '\\*\\*'
t_TIMES = '\\*'
t_DIVIDE = '/'
t_LPAREN = '\\('
t_RPAREN = '\\)'
t_SHIFT = '\\#'
t_QUEST = '\\?'
t_DOTS = '\\:'
t_LT = '<'
t_GT = '>'
t_LE = '<='
t_GE = '>='
t_STRCONST = '\\"([^\\\\\\n]|(\\\\.))*?\\"'
t_ignore = ' \t'
def t_HEXTOASCII(self, t):
return t
t_HEXTOASCII.func_doc = '\\$HexaToAscii\\$\\([a-fA-F0-9][a-fA-F0-9]*\\)'
def t_HEXTOSTR(self, t):
return t
t_HEXTOSTR.func_doc = '\\$HexaToString\\$\\([a-fA-F0-9][a-fA-F0-9]*\\)'
def t_HEXTODEC(self, t):
return t
t_HEXTODEC.func_doc = '\\$HexaToDec\\$\\([a-fA-F0-9][a-fA-F0-9]*\\)'
def t_HEX(self, t):
t.value = int(t.value, 0)
return t
t_HEX.func_doc = '0x[a-fA-F0-9][a-fA-F0-9]*'
def t_HEXSTR(self, t):
t.value = t.value
return t
t_HEXSTR.func_doc = '[a-fA-F0-9]*[a-fA-F][a-fA-F0-9]*'
def t_NAME(self, t):
return t
t_NAME.func_doc = '[a-zA-Z_][a-zA-Z0-9_]*'
def t_FLOAT(self, t):
t.value = float(t.value)
return t
t_FLOAT.func_doc = '((\\d*\\.\\d+)(E[\\+-]?\\d+)?|([1-9]\\d*E[\\+-]?\\d+))'
def t_NUMBER(self, t):
t.value = int(t.value)
return t
t_NUMBER.func_doc = '\\d+'
def t_newline(self, t):
t.lexer.lineno += t.value.count('\n')
t_newline.func_doc = '\\n+'
def t_error(self, t):
print "PLY:Parser:Illegal character '%s'" % t.value[0]
t.lexer.skip(1)
precedence = (('left', 'PLUS', 'MINUS'),
('left', 'TIMES', 'DIVIDE'),
('left', 'EXP'),
('left', 'SHIFT'),
('left', 'LE'),
('left', 'GE'),
('left', 'LT'),
('left', 'GT'),
('left', 'OR'),
('left', 'AND'),
('right', 'UMINUS'))
def p_statement_expr(self, p):
p[0] = p[1]
p_statement_expr.func_doc = 'statement : expression'
def p_expression_binop(self, p):
if p[2] == '+':
p[0] = p[1] + p[3]
elif p[2] == '-':
p[0] = p[1] - p[3]
elif p[2] == '*':
p[0] = p[1] * p[3]
elif p[2] == '/':
p[0] = p[1] / p[3]
elif p[2] == '**':
p[0] = p[1] ** p[3]
elif p[2] == '&':
p[0] = p[1] & p[3]
p_expression_binop.func_doc = '\n expression : expression MINUS expression\n | expression TIMES expression\n | expression DIVIDE expression\n | expression EXP expression\n | expression BAND expression\n '
def p_expression_plus(self, p):
if type(p[1]) is str or type(p[3]) is str:
p[0] = str(p[1]).replace('"', '') + str(p[3])
else:
p[0] = p[1] + p[3]
p_expression_plus.func_doc = 'expression : expression PLUS expression'
def p_expression_uminus(self, p):
p[0] = -p[2]
p_expression_uminus.func_doc = 'expression : MINUS expression %prec UMINUS'
def p_expression_equals(self, p):
if p[1] == p[3]:
p[0] = 1
else:
p[0] = 0
p_expression_equals.func_doc = 'expression : expression EQUALS expression'
def p_expression_notequals(self, p):
if p[1] != p[3]:
p[0] = 1
else:
p[0] = 0
p_expression_notequals.func_doc = 'expression : expression NEQ expression'
def p_expression_le(self, p):
if p[1] <= p[3]:
p[0] = 1
else:
p[0] = 0
p_expression_le.func_doc = 'expression : expression LE expression'
def p_expression_lt(self, p):
if p[1] < p[3]:
p[0] = 1
else:
p[0] = 0
p_expression_lt.func_doc = 'expression : expression LT expression'
def p_expression_ge(self, p):
if p[1] >= p[3]:
p[0] = 1
else:
p[0] = 0
p_expression_ge.func_doc = 'expression : expression GE expression'
def p_expression_gt(self, p):
if p[1] > p[3]:
p[0] = 1
else:
p[0] = 0
p_expression_gt.func_doc = 'expression : expression GT expression'
def p_expression_or(self, p):
if p[1] or p[3]:
p[0] = 1
else:
p[0] = 0
p_expression_or.func_doc = 'expression : expression OR expression'
def p_expression_and(self, p):
if p[1] and p[3]:
p[0] = 1
else:
p[0] = 0
p_expression_and.func_doc = 'expression : expression AND expression'
def p_expression_shift(self, p):
p[0] = p[1] >> p[3] & 1
p_expression_shift.func_doc = 'expression : expression SHIFT expression'
def p_expression_condition(self, p):
if p[1]:
p[0] = p[3]
else:
p[0] = p[5]
p_expression_condition.func_doc = 'expression : expression QUEST expression DOTS expression'
def p_expression_strconst(self, p):
p[0] = p[1]
p_expression_strconst.func_doc = 'expression : STRCONST'
def p_expression_HexaToDec(self, p):
tmp = p[1]
tmp = tmp.replace('$HexaToDec$(', '')
tmp = tmp.replace(')', '')
p[0] = tmp
p_expression_HexaToDec.func_doc = 'expression : HEXTODEC'
def p_expression_HexaToString(self, p):
tmp = p[1]
tmp = tmp.replace('$HexaToString$(', '')
tmp = tmp.replace(')', '')
p[0] = tmp
p_expression_HexaToString.func_doc = 'expression : HEXTOSTR'
def p_expression_HexaToAscii(self, p):
tmp = p[1]
tmp = tmp.replace('$HexaToAscii$(', '')
tmp = tmp.replace(')', '')
tmp = tmp.replace(' ', '')
if len(tmp) % 2:
tmp = '0' + tmp
p[0] = tmp.decode('hex')
p_expression_HexaToAscii.func_doc = 'expression : HEXTOASCII'
def p_expression_group(self, p):
p[0] = p[2]
p_expression_group.func_doc = 'expression : LPAREN expression RPAREN'
def p_expression_number(self, p):
p[0] = p[1]
p_expression_number.func_doc = 'expression : NUMBER'
def p_expression_hex(self, p):
p[0] = p[1]
p_expression_hex.func_doc = 'expression : HEX'
def p_expression_hexstr(self, p):
p[0] = p[1]
p_expression_hexstr.func_doc = 'expression : HEXSTR'
def p_expression_float(self, p):
p[0] = p[1]
p_expression_float.func_doc = 'expression : FLOAT'
def p_expression_name(self, p):
try:
p[0] = self.names[p[1]]
except LookupError:
print "PLY:Parser:Undefined name '%s'" % p[1]
p[0] = 0
p_expression_name.func_doc = 'expression : NAME'
def p_error(self, p):
print "PLY:Parser:Syntax error at '%s'" % p.value
if __name__ == '__main__':
tb = time.time()
calc = Calc()
te = time.time()
print 'init:' + str(te - tb)
value = '(((_STATUSDTC&0x1F)== 0x01)&& ((_STATUSDTC#5== 0x01)&&(((_STATUSDTC&0x1F)== 0x01)||((_STATUSDTC&0x1F)== 0x02)||((_STATUSDTC&0x1F)== 0x03)||((_STATUSDTC&0x9F)== 0x9F)||((_STATUSDTC&0x9F)== 0x8E)||((_STATUSDTC&0x9F)== 0x88)||((_STATUSDTC&0x9F)== 0x89)||((_STATUSDTC&0x9F)== 0x93))))?2:((((_STATUSDTC&0x1F)== 0x02)&& ((_STATUSDTC#5== 0x01)&&(((_STATUSDTC&0x1F)== 0x01)||((_STATUSDTC&0x1F)== 0x02)||((_STATUSDTC&0x1F)== 0x03)||((_STATUSDTC&0x9F)== 0x9F)||((_STATUSDTC&0x9F)== 0x8E)||((_STATUSDTC&0x9F)== 0x88)||((_STATUSDTC&0x9F)== 0x89)||((_STATUSDTC&0x9F)== 0x93))))?4:((((_STATUSDTC&0x1F)== 0x03)&& ((_STATUSDTC#5== 0x01)&&(((_STATUSDTC&0x1F)== 0x01)||((_STATUSDTC&0x1F)== 0x02)||((_STATUSDTC&0x1F)== 0x03)||((_STATUSDTC&0x9F)== 0x9F)||((_STATUSDTC&0x9F)== 0x8E)||((_STATUSDTC&0x9F)== 0x88)||((_STATUSDTC&0x9F)== 0x89)||((_STATUSDTC&0x9F)== 0x93))))?6:((((_STATUSDTC&0x9F)== 0x9F)&& ((_STATUSDTC#5== 0x01)&&(((_STATUSDTC&0x1F)== 0x01)||((_STATUSDTC&0x1F)== 0x02)||((_STATUSDTC&0x1F)== 0x03)||((_STATUSDTC&0x9F)== 0x9F)||((_STATUSDTC&0x9F)== 0x8E)||((_STATUSDTC&0x9F)== 0x88)||((_STATUSDTC&0x9F)== 0x89)||((_STATUSDTC&0x9F)== 0x93))))?8:((((_STATUSDTC&0x9F)== 0x8E)&& ((_STATUSDTC#5== 0x01)&&(((_STATUSDTC&0x1F)== 0x01)||((_STATUSDTC&0x1F)== 0x02)||((_STATUSDTC&0x1F)== 0x03)||((_STATUSDTC&0x9F)== 0x9F)||((_STATUSDTC&0x9F)== 0x8E)||((_STATUSDTC&0x9F)== 0x88)||((_STATUSDTC&0x9F)== 0x89)||((_STATUSDTC&0x9F)== 0x93))))?10:((((_STATUSDTC&0x9F)== 0x89)&& ((_STATUSDTC#5== 0x01)&&(((_STATUSDTC&0x1F)== 0x01)||((_STATUSDTC&0x1F)== 0x02)||((_STATUSDTC&0x1F)== 0x03)||((_STATUSDTC&0x9F)== 0x9F)||((_STATUSDTC&0x9F)== 0x8E)||((_STATUSDTC&0x9F)== 0x88)||((_STATUSDTC&0x9F)== 0x89)||((_STATUSDTC&0x9F)== 0x93))))?14:((((_STATUSDTC&0x9F)== 0x93)&& ((_STATUSDTC#5== 0x01)&&(((_STATUSDTC&0x1F)== 0x01)||((_STATUSDTC&0x1F)== 0x02)||((_STATUSDTC&0x1F)== 0x03)||((_STATUSDTC&0x9F)== 0x9F)||((_STATUSDTC&0x9F)== 0x8E)||((_STATUSDTC&0x9F)== 0x88)||((_STATUSDTC&0x9F)== 0x89)||((_STATUSDTC&0x9F)== 0x93))))?16:((((_STATUSDTC&0x9F)== 0x88)&& ((_STATUSDTC#5== 0x01)&&(((_STATUSDTC&0x1F)== 0x01)||((_STATUSDTC&0x1F)== 0x02)||((_STATUSDTC&0x1F)== 0x03)||((_STATUSDTC&0x9F)== 0x9F)||((_STATUSDTC&0x9F)== 0x8E)||((_STATUSDTC&0x9F)== 0x88)||((_STATUSDTC&0x9F)== 0x89)||((_STATUSDTC&0x9F)== 0x93))))?12:((((_STATUSDTC&0x1F)== 0x01)&& ((_STATUSDTC#6== 0x01)&&(((_STATUSDTC&0x1F)== 0x01)||((_STATUSDTC&0x1F)== 0x02)||((_STATUSDTC&0x1F)== 0x03)||((_STATUSDTC&0x9F)== 0x9F)||((_STATUSDTC&0x9F)== 0x8E)||((_STATUSDTC&0x9F)== 0x88)||((_STATUSDTC&0x9F)== 0x89)||((_STATUSDTC&0x9F)== 0x93))))?1:((((_STATUSDTC&0x1F)== 0x02)&& ((_STATUSDTC#6== 0x01)&&(((_STATUSDTC&0x1F)== 0x01)||((_STATUSDTC&0x1F)== 0x02)||((_STATUSDTC&0x1F)== 0x03)||((_STATUSDTC&0x9F)== 0x9F)||((_STATUSDTC&0x9F)== 0x8E)||((_STATUSDTC&0x9F)== 0x88)||((_STATUSDTC&0x9F)== 0x89)||((_STATUSDTC&0x9F)== 0x93))))?3:((((_STATUSDTC&0x1F)== 0x03)&& ((_STATUSDTC#6== 0x01)&&(((_STATUSDTC&0x1F)== 0x01)||((_STATUSDTC&0x1F)== 0x02)||((_STATUSDTC&0x1F)== 0x03)||((_STATUSDTC&0x9F)== 0x9F)||((_STATUSDTC&0x9F)== 0x8E)||((_STATUSDTC&0x9F)== 0x88)||((_STATUSDTC&0x9F)== 0x89)||((_STATUSDTC&0x9F)== 0x93))))?5:((((_STATUSDTC&0x9F)== 0x9F)&& ((_STATUSDTC#6== 0x01)&&(((_STATUSDTC&0x1F)== 0x01)||((_STATUSDTC&0x1F)== 0x02)||((_STATUSDTC&0x1F)== 0x03)||((_STATUSDTC&0x9F)== 0x9F)||((_STATUSDTC&0x9F)== 0x8E)||((_STATUSDTC&0x9F)== 0x88)||((_STATUSDTC&0x9F)== 0x89)||((_STATUSDTC&0x9F)== 0x93))))?7:((((_STATUSDTC&0x9F)== 0x8E)&& ((_STATUSDTC#6== 0x01)&&(((_STATUSDTC&0x1F)== 0x01)||((_STATUSDTC&0x1F)== 0x02)||((_STATUSDTC&0x1F)== 0x03)||((_STATUSDTC&0x9F)== 0x9F)||((_STATUSDTC&0x9F)== 0x8E)||((_STATUSDTC&0x9F)== 0x88)||((_STATUSDTC&0x9F)== 0x89)||((_STATUSDTC&0x9F)== 0x93))))?9:((((_STATUSDTC&0x9F)== 0x89)&& ((_STATUSDTC#6== 0x01)&&(((_STATUSDTC&0x1F)== 0x01)||((_STATUSDTC&0x1F)== 0x02)||((_STATUSDTC&0x1F)== 0x03)||((_STATUSDTC&0x9F)== 0x9F)||((_STATUSDTC&0x9F)== 0x8E)||((_STATUSDTC&0x9F)== 0x88)||((_STATUSDTC&0x9F)== 0x89)||((_STATUSDTC&0x9F)== 0x93))))?13:((((_STATUSDTC&0x9F)== 0x93)&& ((_STATUSDTC#6== 0x01)&&(((_STATUSDTC&0x1F)== 0x01)||((_STATUSDTC&0x1F)== 0x02)||((_STATUSDTC&0x1F)== 0x03)||((_STATUSDTC&0x9F)== 0x9F)||((_STATUSDTC&0x9F)== 0x8E)||((_STATUSDTC&0x9F)== 0x88)||((_STATUSDTC&0x9F)== 0x89)||((_STATUSDTC&0x9F)== 0x93))))?15:((((_STATUSDTC&0x9F)== 0x88)&& ((_STATUSDTC#6== 0x01)&&(((_STATUSDTC&0x1F)== 0x01)||((_STATUSDTC&0x1F)== 0x02)||((_STATUSDTC&0x1F)== 0x03)||((_STATUSDTC&0x9F)== 0x9F)||((_STATUSDTC&0x9F)== 0x8E)||((_STATUSDTC&0x9F)== 0x88)||((_STATUSDTC&0x9F)== 0x89)||((_STATUSDTC&0x9F)== 0x93))))?11:0)))))))))))))))'
value = value.replace('&', '&')
value = value.replace('_STATUSDTC', '0x' + 'C9')
tb = time.time()
print calc.calculate(value)
te = time.time()
print 'calc:' + str(te - tb)
value = '((_STATUSDTC#5== 0x01)&&(((_STATUSDTC&0x1F)== 0x01)||((_STATUSDTC&0x1F)== 0x02)||((_STATUSDTC&0x1F)== 0x03)||((_STATUSDTC&0x9F)== 0x9F)||((_STATUSDTC&0x9F)== 0x8E)||((_STATUSDTC&0x9F)== 0x88)||((_STATUSDTC&0x9F)== 0x89)||((_STATUSDTC&0x9F)== 0x93)))?1:(((_STATUSDTC#6== 0x01)&&(((_STATUSDTC&0x1F)== 0x01)||((_STATUSDTC&0x1F)== 0x02)||((_STATUSDTC&0x1F)== 0x03)||((_STATUSDTC&0x9F)== 0x9F)||((_STATUSDTC&0x9F)== 0x8E)||((_STATUSDTC&0x9F)== 0x88)||((_STATUSDTC&0x9F)== 0x89)||((_STATUSDTC&0x9F)== 0x93)))?2:0)'
value = value.replace('&', '&')
value = value.replace('_STATUSDTC', '0x' + 'D3')
tb = time.time()
print calc.calculate(value)
te = time.time()
print 'calc:' + str(te - tb)
value = '(1.0 * (100*_VXX_SPG_PWM_SP)) /256'
value = value.replace('&', '&')
value = value.replace('_VXX_SPG_PWM_SP', '0x' + 'D3')
tb = time.time()
print calc.calculate(value)
te = time.time()
print 'calc:' + str(te - tb)
value = '(_MODULE_ABSENT_9!= 0x00)?(((_BOOLEEN_CONTACT_EMBRAYAGE== 0x00)||(_BOOLEEN_CONTACT_EMBRAYAGE== 0x01))?((_BOOLEEN_CONTACT_EMBRAYAGE== 0x01)?1:((_BOOLEEN_CONTACT_EMBRAYAGE== 0x00)?0:1)):0):0'
value = value.replace('&', '&')
value = value.replace('_MODULE_ABSENT_9', '0x' + '01')
value = value.replace('_BOOLEEN_CONTACT_EMBRAYAGE', '0x' + '00')
tb = time.time()
print calc.calculate(value)
te = time.time()
print 'calc:' + str(te - tb)
value = '_VIN'
value = value.replace('_VIN', '11223344556677889900AABBCCDDEEFF1122')
tb = time.time()
print calc.calculate(value)
te = time.time()
print 'calc:' + str(te - tb)