-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathtest_AST.html
More file actions
338 lines (266 loc) · 5.49 KB
/
Copy pathtest_AST.html
File metadata and controls
338 lines (266 loc) · 5.49 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
<html>
<head>
<script src="pythonscript.js"></script>
<script>
__BRYTHON__ = {
scope : {}
}
// from py_utils.js
function $last(item){
if(typeof item=="string"){return item.charAt(item.length-1)}
else if(typeof item=="object"){return item[item.length-1]}
}
</script>
<script src="libs/brython/py2js.js"></script>
<script src="bindings/ast.py"></script>
<pre id="pythonjs_source">
source = $PYTHONJS
</pre>
<script type="text/python">
from ast import *
source = """
@mydecorator1
@mydecorator2
def func(a,b, mykey=1, *S, **K):
c = a+b
return c
func(100, 200, 'hi', mykey=1)
@myclassdecorator
class A(X,Y):
def method1(self, x,y,z):
w = x+y+z
return w
def method2(self, XXX):
self.xxx = XXX ## TODO fixme
a = b = c = x = y = z = 1
if 1 and True or False:
print('hi')
print('world')
elif True:
pass
else:
pass
return XXX
"""
source_test_elif = """
if False:
pass
elif True:
print( 'hi elif' )
else:
a = 1 + 1
print('hi else')
if False:
a = 2+2
elif True:
print( 'hi elif2' )
else:
a = 3 + 3
print('hi else3')
"""
source_for = """
for i in range(10):
a = i * i
i = 0
while i < 10:
i += 1
"""
source_list_tuple_dict = """
if 1 <= 2:
b = not a
x = [1,2]
y = (3,4)
z = {'k1':'val1', 'k2':'val2'}
v = z[ 'k1' ]
v = x[0]
"""
source_slice = """
if True:
a = []
b = a[1:2]
c = a[1:2:3]
"""
source_ops = """
if True:
a = 1 / 2
b = 2 // 2
c = a is b
d = a is not b
e = a**2
g = -e
g = not e
"""
source_import = """
import a,b,c
from x import y
"""
source_try = """
try:
a = 1+1
except SomeError:
pass
"""
source_in = """
if True:
x in a
x not in a
"""
source_raise = """
raise RuntimeError
"""
source_list_comp = """
if True:
a = [x for x in 'abc']
"""
source_tuple_for = """
for i,c in enumerate('abc'):
print( i )
"""
source_null_slice = """
if True:
a = []
b = a[1:]
"""
source_null_return = """
def func():
return
"""
source_lambda = """
if True:
f = lambda x,y,z: x+y+z
"""
source_inplace_assignment = """
if True:
a = 100
a += 1
b += 2
class XXX(object):
def some_method(self):
self.level += 1
"""
source = """
class XXX:
def mymethod(self):
if self._with_dart:
pass
elif self._with_js or javascript:
a = 1
if True: ## TODO this this
b = 2
"""
#source = document.getElementById('pythonjs_source').firstChild.nodeValue
def test():
global module
module = parse( source )
print '........AST..........'
DebugVisitor( module )
class DebugVisitor( NodeVisitor ):
def visit_While(self, node):
print 'while', self.visit(node.test), ':'
for a in node.body:
r = self.visit(a)
if r: print ' ', r
def visit_For(self, node):
print 'for', self.visit(node.target), 'in'
self.visit(node.iter)
print ': ## for body'
for a in node.body:
r = self.visit(a)
if r: print ' ', r
def visit_IfExp(self, node):
print 'if ', self.visit(node.test), ':'
for a in node.body:
r = self.visit(a)
if r: print ' ', r
if len(node.orelse):
print 'else:'
for a in node.orelse:
r = self.visit( a )
if r: print ' ', r
def visit_Assign(self, node):
a = [ self.visit( t ) for t in node.targets ]
a = '='.join( a )
b = self.visit( node.value )
return a + '=' + b
def visit_AugAssign(self, node):
return self.visit(node.target) + node.op + self.visit(node.value)
def visit_Subscript(self, node):
return '%s[%s]' %(self.visit(node.value), self.visit(node.slice))
def visit_Index(self, node):
return self.visit(node.value)
def visit_Slice(self, node):
lower = ''
upper = ''
step = ''
if node.lower: lower = self.visit(node.lower)
if node.upper: upper = self.visit(node.upper)
if node.step: step = self.visit(node.step)
if node.step:
return lower + ':' + upper + ':' + step
else:
return lower + ':' + upper
def visit_Dict(self, node):
s = []
for i in range( len(node.keys) ):
k = self.visit( node.keys[i] )
v = self.visit( node.values[i] )
#s.append( '%s:%s'%(k,v) )
s.append( k+':'+v )
return '{' + ','.join(s) + '}'
def visit_List(self, node):
s = []
for e in node.elts:
s.append( self.visit(e) )
return '[' + ','.join(s) + ']'
def visit_Tuple(self, node):
s = []
for e in node.elts:
s.append( self.visit(e) )
return '(' + ','.join(s) + ')'
def visit_UnaryOp(self, node):
return self.visit(node.op) + self.visit(node.operand)
def visit_BinOp(self, node):
return self.visit( node.left ) + self.visit(node.op) + self.visit( node.right )
def visit_FunctionDef(self, node):
for dec in node.decorator_list:
print '@', self.visit(dec)
args = []
for a in node.args.args:
args.append( self.visit(a) )
if node.args.vararg:
args.append( '*'+node.args.vararg )
if node.args.kwarg:
args.append( '**'+node.args.kwarg )
args = ','.join( args )
print 'def ', node.name + '('+args+'):'
for n in node.body:
print ' ', self.visit(n)
def visit_Return(self, node):
if node.value:
return 'return ' + self.visit(node.value)
else:
return 'return'
def visit_Call(self, node):
print self.visit(node.func) + '('
for a in node.args:
print ' ', self.visit(a)
for k in node.keywords:
print ' ', k.arg, '=', self.visit(k.value)
print ')'
def visit_ClassDef(self, node):
for dec in node.decorator_list:
print '@', self.visit(dec)
print 'class '+node.name + ':'
for n in node.body:
a = self.visit(n)
if a: print '->', a
def visit_Attribute(self, node):
a = self.visit(node.value)
return a + '.' + node.attr
test()
</script>
</head>
<body>
<button onclick="test()">click me</button>
</body>
</html>