forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_walker.py
More file actions
121 lines (103 loc) · 3.54 KB
/
generate_walker.py
File metadata and controls
121 lines (103 loc) · 3.54 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
# Licensed to the .NET Foundation under one or more agreements.
# The .NET Foundation licenses this file to you under the Apache 2.0 License.
# See the LICENSE file in the project root for more information.
from generate import generate
import System
import clr
# This generates PythonWalker.Generated.cs
# usage is just:
# ipyd generate_walker.py
# it wills can all types in IronPython and detect AST nodes based on type inheritence.
def inherits(t, p):
if not t:
return False
elif t.FullName == p:
return True
else:
return inherits(t.BaseType, p)
def get_ast(assembly, roots):
import clr
sets = {}
for root in roots:
sets[root] = set()
for node in assembly.GetTypes():
# skip abstract types
if node.IsAbstract: continue
for root in roots:
if inherits(node, root) and node.IsPublic:
sets[root].add(node.Name)
break
result = []
for root in roots:
result.extend(sorted(list(sets[root])))
return result
def gen_walker(cw, nodes, method, value):
space = 0
for node in nodes:
if space: cw.write("")
cw.write("// %s" % node)
cw.write("%s bool Walk(%s node) { return %s; }" % (method, node, value))
cw.write("%s void PostWalk(%s node) { }" % (method, node))
space = 1
def get_python_nodes():
nodes = get_ast(
clr.LoadAssemblyByPartialName("IronPython"),
[
"IronPython.Compiler.Ast.Expression",
"IronPython.Compiler.Ast.Statement",
"IronPython.Compiler.Ast.Node"
]
)
return nodes
def gen_python_walker(cw):
gen_walker(cw, get_python_nodes(), "public virtual", "true")
def gen_python_walker_nr(cw):
gen_walker(cw, get_python_nodes(), "public override", "false")
def gen_python_name_binder(cw):
# Each of these exclusions is here because there is some reason
# to not generate the default content.
exclusions = ["DictionaryComprehension",
"ListComprehension",
"SetComprehension",
"ExecStatement",
"DelStatement",
"ClassDefinition",
"FunctionDefinition",
"AugmentedAssignStatement",
"AssignmentStatement",
"RaiseStatement",
"ForStatement",
"WhileStatement",
"BreakStatement",
"ContinueStatement",
"ReturnStatement",
"WithStatement",
"FromImportStatement",
"GlobalStatement",
"NameExpression",
"PythonAst",
"ImportStatement",
"TryStatement",
"ComprehensionFor",
"CallExpression",
"NonlocalStatement"
]
nodes = get_python_nodes()
nodes.sort()
for node in nodes:
if node not in exclusions:
space = 0
if space: cw.write("")
cw.write("// %s" % node)
cw.enter_block("public override bool Walk(%s node)" % node)
cw.write("node.Parent = _currentScope;")
cw.write("return base.Walk(node);")
cw.exit_block()
def main():
return generate(
("Python AST Walker", gen_python_walker),
("Python AST Walker Nonrecursive", gen_python_walker_nr),
("Python Name Binder Propagate Current Scope", gen_python_name_binder),
)
if __name__=="__main__":
main()