forked from RustPython/RustPython
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrawl_sourcecode.py
More file actions
Latest commit
83 lines (69 loc) · 2.16 KB
/
Copy pathcrawl_sourcecode.py
File metadata and controls
83 lines (69 loc) · 2.16 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
""" This script can be used to test the equivalence in parsing between
rustpython and cpython.
Usage example:
$ python crawl_sourcecode.py crawl_sourcecode.py > cpython.txt
$ cargo run crawl_sourcecode.py crawl_sourcecode.py > rustpython.txt
$ diff cpython.txt rustpython.txt
"""
importast
importsys
importsymtable
importdis
filename=sys.argv[1]
print('Crawling file:', filename)
withopen(filename, 'r') asf:
source=f.read()
t=ast.parse(source)
print(t)
shift=3
defprint_node(node, indent=0):
indents=' '*indent
ifisinstance(node, ast.AST):
lineno='row={}'.format(node.lineno) ifhasattr(node, 'lineno') else''
print(indents, "NODE", node.__class__.__name__, lineno)
forfieldinnode._fields:
print(indents,'-', field)
f=getattr(node, field)
ifisinstance(f, list):
forf2inf:
print_node(f2, indent=indent+shift)
else:
print_node(f, indent=indent+shift)
else:
print(indents, 'OBJ', node)
print_node(t)
# print(ast.dump(t))
flag_names= [
'is_referenced',
'is_assigned',
'is_global',
'is_local',
'is_parameter',
'is_free',
]
defprint_table(table, indent=0):
indents=' '*indent
print(indents, 'table:', table.get_name())
print(indents, ' ', 'name:', table.get_name())
print(indents, ' ', 'type:', table.get_type())
print(indents, ' ', 'line:', table.get_lineno())
print(indents, ' ', 'identifiers:', table.get_identifiers())
print(indents, ' ', 'Syms:')
forsymintable.get_symbols():
flags= []
forflag_nameinflag_names:
func=getattr(sym, flag_name)
iffunc():
flags.append(flag_name)
print(indents, ' sym:', sym.get_name(), 'flags:', ' '.join(flags))
iftable.has_children():
print(indents, ' ', 'Child tables:')
forchildintable.get_children():
print_table(child, indent=indent+shift)
table=symtable.symtable(source, 'a', 'exec')
print_table(table)
print()
print('======== dis.dis ========')
print()
co=compile(source, filename, 'exec')
dis.dis(co)