-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgaugescript.py
More file actions
executable file
·498 lines (426 loc) · 11.2 KB
/
gaugescript.py
File metadata and controls
executable file
·498 lines (426 loc) · 11.2 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#!/usr/bin/env python
# This program interprets Microsoft ESP gauge script.
# Gauge script is a postfix language so this program
# uses a stack to interpret the script.
# A really nice feature would allow this interpreter to connect
# to ESP via simconnect and access live variables from the
# simulator (ie either read or write). This may be possible
# using ctypes to acess the simconnect dll functions
#
# Commands
# - pstack : prints the stack
# - pvars : prints the var dictionary
# - quit : quit the interpreter
#
#
# Status
# This is still in a very primordial state
#
# TODO
# - Full support of all gaugescript elements
# - help command
# - default units for vars
# - unit translation
# - debugging
# - connect to FSX etc using simconnect
#
import sys
import re
import math
lexTable = [
( 'STRING', re.compile( r'\'(.*)\'' ) ),
( 'FLOAT', re.compile( r'\d+\.\d+' ) ),
( 'INT', re.compile( r'\d+' ) ),
( 'OP', re.compile( r'\&|\||\^|\~|\>\>|\<\<' ) ),
( 'OP', re.compile( r'(\=\=)|(\!\=)|\!|\&\&|\|\|' ) ),
( 'OP', re.compile( r'\<|\>|\>\=|\<\=' ) ),
( 'OP', re.compile( r'(\+\+)|(\-\-)' ) ),
( 'OP', re.compile( r'\/\-\/|\?' ) ),
( 'OP', re.compile( r'[\+\-\*\/\%]' ) ),
( 'OP', re.compile( r'if\{|\}|els\{' ) ),
( 'ID', re.compile( r'[a-zA-Z_]+' ) ),
( 'SPACE', re.compile( r'\s+') ),
( 'VAREXPR', re.compile( r'\(([a-zA-Z_: ]+)(,\s*([a-zA-Z]+))?\)' ) ),
( 'VARASSIGN', re.compile( r'\(\>([a-zA-Z_: ]+)(,\s*([a-zA-Z]+))?\)' ) ),
]
varDict = {}
def pop( stack ):
stack.pop()
def add( stack ):
stack.append( stack.pop() + stack.pop() )
def sub( stack ):
B = stack.pop()
A = stack.pop()
stack.append( A - B )
def mul( stack ):
stack.append( stack.pop() * stack.pop() )
def div( stack ):
B = stack.pop()
A = stack.pop()
stack.append( A / B )
def mod( stack ):
B = stack.pop()
A = stack.pop()
stack.append( math.fmod( A, B ) )
def incr( stack ):
X = stack.pop()
stack.append( X + 1 )
def decr( stack ):
X = stack.pop()
stack.append( X - 1 )
def neg( stack ):
X = stack.pop()
stack.append( X * -1 )
def eq( stack ):
B = stack.pop()
A = stack.pop()
stack.append( A == B )
def ne( stack ):
B = stack.pop()
A = stack.pop()
stack.append( A != B )
def gt( stack ):
B = stack.pop()
A = stack.pop()
stack.append( A > B )
def lt( stack ):
B = stack.pop()
A = stack.pop()
stack.append( A < B )
def ge( stack ):
B = stack.pop()
A = stack.pop()
stack.append( A >= B )
def le( stack ):
B = stack.pop()
A = stack.pop()
stack.append( A <= B )
def choose( stack ):
C = stack.pop()
B = stack.pop()
A = stack.pop()
stack.append( A if C else B )
def bitAnd( stack ):
B = stack.pop()
A = stack.pop()
stack.append( A & B )
def bitOr( stack ):
B = stack.pop()
A = stack.pop()
stack.append( A | B )
def bitXor( stack ):
B = stack.pop()
A = stack.pop()
stack.append( A ^ B )
def bitNot( stack ):
A = stack.pop()
stack.append( ~A )
def bitRShift( stack ):
B = stack.pop()
A = stack.pop()
stack.append( A >> B )
def bitLShift( stack ):
B = stack.pop()
A = stack.pop()
stack.append( A << B )
def logicalNot( stack ):
A = stack.pop()
stack.append( not A )
def logicalAnd( stack ):
B = stack.pop()
A = stack.pop()
stack.append( A and B )
def logicalOr( stack ):
B = stack.pop()
A = stack.pop()
stack.append( A or B )
#
# Conditional expressions
#
# Uses a stack to allow for nested expressions
# If a condition evaluates to false, tokens are discarded
# until the expression ends, though conditional tokens are
# evaluated to keep the stack state current.
#
# Example:
# false if{ op op } els{ op op }
# condval: f f t
# condsttack: f [] t []
# discard: t f f f
#
# true if{ op op false if{ op op } op } els{ op op true if{ op op } op }
# condval: t f t f
# condsttack: t tf t [] f ff f []
# discard: f t f f t t t f
#
discardState = False
condVal = None
condStack = []
def if_( stack ):
global condVal
global condStack
global discardState
if discardState:
A = False
else:
A = stack.pop()
condVal = A
condStack.append( condVal )
if not condVal:
discardState = True
def els_( stack ):
global condVal
global condStack
global discardState
condVal = not condVal
condStack.append( condVal )
if not condVal:
discardState = True
def endif_( stack ):
global condVal
global condStack
global discardState
condStack.pop()
if len( condStack ):
condVal = condStack[ -1 ]
discardState = not condVal
else:
discardState = False
opTable = {
'+' : add,
'-' : sub,
'*' : mul,
'/' : div,
'%' : mod,
'p' : pop,
'++' : incr,
'--' : decr,
'/-/' : neg,
'==' : eq,
'!=' : ne,
'>' : gt,
'<' : lt,
'>=' : ge,
'<=' : le,
'?' : choose,
'&' : bitAnd,
'|' : bitOr,
'^' : bitXor,
'~' : bitNot,
'>>' : bitRShift,
'<<' : bitLShift,
'not' : logicalNot,
'!' : logicalNot,
'or' : logicalOr,
'||' : logicalOr,
'and' : logicalAnd,
'&&' : logicalAnd,
'if{' : if_,
'els{' : els_,
'}' : endif_,
}
def abs_( stack ):
A = stack.pop()
stack.append( abs( A ) )
def int_( stack ):
A = stack.pop()
stack.append( int( A ) )
def rng_( stack ):
C = stack.pop()
B = stack.pop()
A = stack.pop()
stack.append( A <= C and C <= B )
def pi_( stack ):
stack.append( math.pi )
def cos_( stack ):
A = stack.pop()
stack.append( math.cos( A ) )
def log10_( stack ):
A = stack.pop()
stack.append( math.log10( A ) )
def min_( stack ):
B = stack.pop()
A = stack.pop()
stack.append( A if ( A <= B ) else B )
def sin_( stack ):
A = stack.pop()
stack.append( math.sin( A ) )
def acos_( stack ):
A = stack.pop()
stack.append( math.acos( A ) )
def cot_( stack ):
A = stack.pop()
stack.append( 1.0 / math.tan( A ) )
def log_( stack ):
A = stack.pop()
stack.append( math.log( A ) )
def square_( stack ):
A = stack.pop()
stack.append( A * A )
def asin_( stack ):
A = stack.pop()
stack.append( math.asin( A ) )
# Note that this returns a fixed epsilon which might
# not be right. Gaugescript eps takes an argument implying
# that eps depends on the value given.
def eps_( stack ):
stack.pop()
stack.append( sys.float_info.epsilon )
def logN_( stack ):
B = stack.pop()
A = stack.pop()
stack.append( math.log( A, B ) )
def sqrt_( stack ):
A = stack.pop()
stack.append( math.sqrt( A ) )
def atan2_( stack ):
B = stack.pop()
A = stack.pop()
stack.append( math.atan2( A, B ) )
def exp_( stack ):
A = stack.pop()
stack.append( math.exp( A ) )
def max_( stack ):
B = stack.pop()
A = stack.pop()
stack.append( A if A >= B else B )
def pow_( stack ):
B = stack.pop()
A = stack.pop()
stack.append( math.pow( A, B ) )
def tan_( stack ):
A = stack.pop()
stack.append( math.tan( A ) )
def atan_( stack ):
A = stack.pop()
stack.append( math.atan( A ) )
def div_( stack ):
B = stack.pop()
A = stack.pop()
stack.append( int(A)/int(B) )
def ceil_( stack ):
A = stack.pop()
stack.append( math.ceil( A ) )
def round_( stack ):
A = stack.pop()
stack.append( round( A ) )
def dnor_( stack ):
A = stack.pop()
stack.append( A % 360 )
def rddg_( stack ):
A = stack.pop()
stack.append( math.degrees( A ) )
def dgrd_( stack ):
A = stack.pop()
stack.append( math.radians( A ) )
def rnor_( stack ):
A = stack.pop()
stack.append( A % ( 2 * math.pi ) )
funcTable = {
'abs' : abs_,
'int' : int_,
'flr' : int_,
'rng' : rng_,
'pi' : pi_,
'cos' : cos_,
'lg' : log10_,
'min' : min_,
'sin' : sin_,
'acos' : acos_,
'ctg' : cot_,
'ln' : log_,
'sqr' : square_,
'asin' : asin_,
'eps' : eps_,
'log' : logN_,
'sqrt' : sqrt_,
'atg2' : atan2_,
'exp' : exp_,
'max' : max_,
'pow' : pow_,
'tg' : tan_,
'atg' : atan_,
'div' : div_,
'ceil' : ceil_,
'near' : round_,
'dnor' : dnor_,
'd360' : dnor_,
'rdeg' : dnor_,
'rddg' : rddg_,
'dgrd' : dgrd_,
'rnor' : rnor_,
}
def printHelp():
print "Commands:"
print "pstack\tPrint the stack"
print "pvars\tPrint the variables"
print "quit\tQuit the program (shortcut q)"
def main():
global condVal
global discardState
repl = True
stack = []
while repl:
try:
s = raw_input( '> ' )
except:
print
repl = False
continue
if s == 'quit' or s == 'q':
repl = False
continue
if s == 'help':
printHelp()
continue
if s == 'pstack':
print stack
continue
if s == 'pvars':
print varDict
continue
i = 0
while i < len( s ):
match = False
for tokType, regex in lexTable:
result = regex.match(s, i)
if result:
i = result.end()
match = True
if discardState and result.group(0) not in ['}','if{','els{']:
break
#print "match", tokType, result.group(0)
if tokType == 'INT':
stack.append( int( result.group( 0 ) ) )
elif tokType == 'FLOAT':
stack.append( float( result.group( 0 ) ) )
elif tokType == 'STRING':
stack.append( str( result.group( 1 ) ) )
elif tokType == 'OP' or tokType == 'ID':
id = result.group( 0 ).lower()
if id in opTable:
func = opTable[ id ]
func( stack )
if id in funcTable:
func = funcTable[ id ]
func( stack )
elif tokType == 'VAREXPR':
id = result.group( 1 )
if result.group( 3 ) is not None:
units = result.group( 3 )
#print 'units:', units
if id in varDict:
stack.append( varDict[ id ] )
else:
print 'Undefined:', id
elif tokType == 'VARASSIGN':
id = result.group( 1 )
if result.group( 3 ) is not None:
units = result.group( 3 )
#print 'units:', units
varDict[ id ] = stack.pop()
break
if not match:
raise Exception('lexical error at {0}'.format(i))
if __name__ == '__main__':
main()