forked from DenerosArmy/pythonscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs_ast.py
More file actions
476 lines (383 loc) · 11.1 KB
/
js_ast.py
File metadata and controls
476 lines (383 loc) · 11.1 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
"""
TODO:
TryCatch(Statement)
"""
class Module(object):
def __init__(self, body, semis=True):
"""
@type body: C{list}
@param body: list of C{Statement}
@type semis: C{bool}
@para semis: whether or not to use semis
"""
self.body = body
self.semis = semis
def __str__(self):
string = ""
for stmt in self.body:
if type(stmt) != NullStatement and str(stmt).strip() != "":
lines = str(stmt).split("\n")
for line in lines:
if line[-1] not in ("{", "(", "}") and self.semis:
string += "{0};\n".format(line)
else:
string += "{0}\n".format(line)
return string
class Expression(object):
def __init__(self):
pass
class Statement(object):
def __init__(self):
pass
class RawExpression(Expression):
def __init__(self, exp):
"""
@type exp: C{str}
"""
self.exp = exp
def __str__(self):
return self.exp
class RawStatement(Statement):
def __init__(self, stmt):
"""
@type exp: C{str}
"""
self.stmt= stmt
def __str__(self):
return self.stmt
class NullStatement(Statement):
def __str__(self):
return ""
pass
class Function(Statement):
def __init__(self, args, body):
"""
@type args: C{list}
@param args: list of L{Name}
@type body: C{list}
@param body: list of L{Statement}
"""
self.args = args
self.body = body
def __str__(self):
string = "function ("
if len(self.args) > 0:
for arg in self.args:
string += str(arg) + ", "
string = string[:-2] + ") {\n"
else:
string += ") {\n"
for stmt in self.body:
if type(stmt) != NullStatement and str(stmt) != "":
lines = str(stmt).split("\n")
for line in lines:
string += " {0}\n".format(line)
string += "}"
return string
class Vars(Statement):
def __init__(self, names):
"""
@type names: C{list}
@param names: list of strings as local variable names
"""
self.names = names
def __str__(self):
if len(self.names) == 0:
return ""
string = "var "
for name in self.names:
string += "{0}, ".format(name)
return string[:-2]
class Return(Statement):
def __init__(self, value):
"""
@type value: L{Statement}
"""
self.value = value
def __str__(self):
return "return {0}".format(self.value)
class DeclareVar(Statement):
def __init__(self, variables):
"""
@type var: C{list}
@param var: list of L{names}
"""
self.variables = variables
def __str__(self):
string = "var "
for var in self.variables:
string += "{0}, ".format(var)
return string[:-2]
class Assign(Statement):
def __init__(self, target, value):
"""
@type target: L{Name}
@type value: L{Expression}
"""
self.target = target
self.value = value
def __str__(self):
return "{0} = {1}".format(self.target, self.value)
class AugAssign(Statement):
def __init__(self, target, op, value):
"""
@type target: L{Name}
@type op: L{BinOp}
@type target: L{Expression}
"""
self.target = target
self.op = op
self.value = value
def __str__(self):
return "{0} {1}= {2}".format(self.target, self.op, self.value)
class Print(Statement):
def __init__(self, value):
"""
@type value: anything that can be printed
"""
self.value = value
def __str__(self):
return "console.log({0})".format(self.value)
class For(Statement):
def __init__(self, target, iterable, body):
"""
@type target: L{Name}
@type iterable: L{Expression}
@param iterable: an expression that returns an iterable object
@type body: C{list}
@param body: list of L{Statement}
"""
self.target = target
self.iterable = iterable
self.body = body
def __str__(self):
string = "for ({0} in {1}) {{\n".format(self.target, self.iterable)
for elem in self.body:
if type(elem) != NullStatement and str(elem) != "":
lines = str(elem).split("\n")
for line in lines:
string += " {0}\n".format(line)
return string + "}"
class While(Statement):
def __init__(self, condition, body):
"""
@type condition: L{expression}
@type body: L{list}
@param body: list of L{Statement}
"""
self.condition = condition
self.body = body
def __str__(self):
string = "while ({0}) {{\n".format(self.condition)
for elem in self.body:
if type(elem) != NullStatement and str(elem) != "":
lines = str(elem).split("\n")
for line in lines:
string += " {0}\n".format(line)
return string + "}"
class If(Statement):
def __init__(self, condition, if_body, else_body):
"""
@type condition: L{Statement}
@type if_body: C{list}
@param if_body: list of L{Statement}
@type else_body: C{list}
@param else_body: list of L{Statement}
"""
self.condition = condition
self.if_body = if_body
self.else_body = else_body
def __str__(self):
string = "if ({0}) {{\n".format(self.condition)
for elem in self.if_body:
if type(elem) != NullStatement and str(elem) != "":
lines = str(elem).split("\n")
for line in lines:
string += " {0}\n".format(line)
if self.else_body:
string += "} else {\n"
for elem in self.else_body:
if type(elem) != NullStatement and str(elem) != "":
lines = str(elem).split("\n")
for line in lines:
string += " {0}\n".format(line)
return string + "}"
class Bool(Expression):
def __init__(self, op, values):
"""
@type op: L{BoolOp}
@type values: C{list}
@param values: list of L{Expression}
"""
self.op = op
self.values = values
def __str__(self):
string = ""
for val in self.values:
string += "{0} {1} ".format(val, self.op)
return string[:-4]
class Bin(Expression):
def __init__(self, op, left, right):
"""
@type op: L{BinOp}
@type left: L{Expression}
@type right: L{Expression}
"""
self.op = op
self.left = left
self.right = right
def __str__(self):
if str(self.op) in ("+", "-"):
return "({0} {1} {2})".format(self.left, self.op, self.right)
return "{0} {1} {2}".format(self.left, self.op, self.right)
class Unary(Expression):
def __init__(self, op, value):
"""
@type op: L{UnaryOp}
@type values: C{list}
@param values: list of L{Expression}
"""
self.op = op
self.value = value
def __str__(self):
if len(self.value.split()) == 1:
return "{0}{1}".format(self.op, self.value)
return "{0}({1})".format(self.op, self.value)
class Dict(Expression):
def __init__(self, keys, values):
"""
@type keys: C{list}
@type values: C{list}
"""
assert len(keys) == len(values)
self.keys = keys
self.values = values
def __str__(self):
string = "{"
for i in xrange(len(self.keys)):
string += "{0}:{1}, ".format(self.keys[i], self.values[i])
if string == "{":
return "{}"
return string[:-2] + "}"
class Compare(Expression):
def __init__(self, op, left, right):
"""
@type op: L{CompareOp}
@type left: L{Expression}
@type right: L{Expression}
"""
self.op = op
self.left = left
self.right = right
def __str__(self):
return "{0} {1} {2}".format(self.left, self.op, self.right)
class Call(Expression):
def __init__(self, func, args):
"""
@type func: L{Name}
@type args: C{list}
@param args: list of L{Expression}
"""
self.func = func
self.args = args
def __str__(self):
string = "{0} (".format(self.func)
if len(self.args) == 0:
return string + ")"
for arg in self.args:
string += str(arg) + ", "
return string[:-2] + ")"
class Num(Expression):
def __init__(self, value):
"""
@type value: C{int} or C{float}
"""
self.value = value
def __str__(self):
return str(self.value)
class Str(Expression):
def __init__(self, value):
"""
@type value: C{str}
"""
self.value = value
def __str__(self):
return '"{0}"'.format(self.value)
class Attribute(Expression):
def __init__(self, value, attr):
"""
@type attr: C{str}
@type value: C{Expression}
"""
self.value = value
self.attr = attr
def __str__(self):
return "{0}.{1}".format(self.value, self.attr)
class Subscript(Expression):
def __init__(self, value, index):
"""
@type value: L{Name}
@type index: C{Expression}
"""
self.value = value
self.index = index
def __str__(self):
return "{0}[{1}]".format(self.value, self.index)
class Name(Expression):
def __init__(self, name):
"""
@type name: C{str}
"""
self.name = name
def __str__(self):
return str(self.name)
class List(Expression):
def __init__(self, elems):
"""
@type elem: C{list}
@param elem: list of C{Expression}
"""
self.elems = elems
def __str__(self):
string = "["
for elem in self.elems:
string += "{0}, ".format(elem)
if string == "[":
return "[]"
return string[:-2] + "]"
class BoolOp(object):
def __init__(self, op):
"""
@type op: C{str}
"""
assert op in ("||", "&&")
self.op = op
def __str__(self):
return self.op
class BinOp(object):
def __init__(self, op):
"""
@type op: C{str}
"""
assert op in ("+", "-", "*", "/", "%")
self.op = op
def __str__(self):
return self.op
class UnaryOp(object):
def __init__(self, op):
"""
@type op: C{str}
"""
assert op in ("!", "~")
self.op = op
def __str__(self):
return self.op
class CompareOp(object):
def __init__(self, op):
"""
@type op: C{str}
"""
assert op in ("==", "===", "!=", "!==", ">", "<", ">=", "<=")
self.op = op
def __str__(self):
return self.op