|
1 | | -""" This script can be used to test the equivalence in parsing between |
| 1 | +"""This script can be used to test the equivalence in parsing between |
2 | 2 | rustpython and cpython. |
3 | 3 |
|
4 | 4 | Usage example: |
|
8 | 8 | $ diff cpython.txt rustpython.txt |
9 | 9 | """ |
10 | 10 |
|
11 | | - |
12 | 11 | import ast |
13 | 12 | import sys |
14 | 13 | import symtable |
15 | 14 | import dis |
16 | 15 |
|
17 | 16 | filename = sys.argv[1] |
18 | | -print('Crawling file:', filename) |
| 17 | +print("Crawling file:", filename) |
19 | 18 |
|
20 | 19 |
|
21 | | -with open(filename, 'r') as f: |
| 20 | +with open(filename, "r") as f: |
22 | 21 | source = f.read() |
23 | 22 |
|
24 | 23 | t = ast.parse(source) |
25 | 24 | print(t) |
26 | 25 |
|
27 | 26 | shift = 3 |
| 27 | + |
| 28 | + |
28 | 29 | def print_node(node, indent=0): |
29 | | - indents = ' ' * indent |
| 30 | + indents = " " * indent |
30 | 31 | if isinstance(node, ast.AST): |
31 | | - lineno = 'row={}'.format(node.lineno) if hasattr(node, 'lineno') else '' |
| 32 | + lineno = "row={}".format(node.lineno) if hasattr(node, "lineno") else "" |
32 | 33 | print(indents, "NODE", node.__class__.__name__, lineno) |
33 | 34 | for field in node._fields: |
34 | | - print(indents,'-', field) |
| 35 | + print(indents, "-", field) |
35 | 36 | f = getattr(node, field) |
36 | 37 | if isinstance(f, list): |
37 | 38 | for f2 in f: |
38 | | - print_node(f2, indent=indent+shift) |
| 39 | + print_node(f2, indent=indent + shift) |
39 | 40 | else: |
40 | | - print_node(f, indent=indent+shift) |
| 41 | + print_node(f, indent=indent + shift) |
41 | 42 | else: |
42 | | - print(indents, 'OBJ', node) |
| 43 | + print(indents, "OBJ", node) |
| 44 | + |
43 | 45 |
|
44 | 46 | print_node(t) |
45 | 47 |
|
46 | 48 | # print(ast.dump(t)) |
47 | 49 | flag_names = [ |
48 | | - 'is_referenced', |
49 | | - 'is_assigned', |
50 | | - 'is_global', |
51 | | - 'is_local', |
52 | | - 'is_parameter', |
53 | | - 'is_free', |
| 50 | + "is_referenced", |
| 51 | + "is_assigned", |
| 52 | + "is_global", |
| 53 | + "is_local", |
| 54 | + "is_parameter", |
| 55 | + "is_free", |
54 | 56 | ] |
55 | 57 |
|
| 58 | + |
56 | 59 | def print_table(table, indent=0): |
57 | | - indents = ' ' * indent |
58 | | - print(indents, 'table:', table.get_name()) |
59 | | - print(indents, ' ', 'name:', table.get_name()) |
60 | | - print(indents, ' ', 'type:', table.get_type()) |
61 | | - print(indents, ' ', 'line:', table.get_lineno()) |
62 | | - print(indents, ' ', 'identifiers:', table.get_identifiers()) |
63 | | - print(indents, ' ', 'Syms:') |
| 60 | + indents = " " * indent |
| 61 | + print(indents, "table:", table.get_name()) |
| 62 | + print(indents, " ", "name:", table.get_name()) |
| 63 | + print(indents, " ", "type:", table.get_type()) |
| 64 | + print(indents, " ", "line:", table.get_lineno()) |
| 65 | + print(indents, " ", "identifiers:", table.get_identifiers()) |
| 66 | + print(indents, " ", "Syms:") |
64 | 67 | for sym in table.get_symbols(): |
65 | 68 | flags = [] |
66 | 69 | for flag_name in flag_names: |
67 | 70 | func = getattr(sym, flag_name) |
68 | 71 | if func(): |
69 | 72 | flags.append(flag_name) |
70 | | - print(indents, ' sym:', sym.get_name(), 'flags:', ' '.join(flags)) |
| 73 | + print(indents, " sym:", sym.get_name(), "flags:", " ".join(flags)) |
71 | 74 | if table.has_children(): |
72 | | - print(indents, ' ', 'Child tables:') |
| 75 | + print(indents, " ", "Child tables:") |
73 | 76 | for child in table.get_children(): |
74 | | - print_table(child, indent=indent+shift) |
| 77 | + print_table(child, indent=indent + shift) |
| 78 | + |
75 | 79 |
|
76 | | -table = symtable.symtable(source, 'a', 'exec') |
| 80 | +table = symtable.symtable(source, "a", "exec") |
77 | 81 | print_table(table) |
78 | 82 |
|
79 | 83 | print() |
80 | | -print('======== dis.dis ========') |
| 84 | +print("======== dis.dis ========") |
81 | 85 | print() |
82 | | -co = compile(source, filename, 'exec') |
| 86 | +co = compile(source, filename, "exec") |
83 | 87 | dis.dis(co) |
0 commit comments