Repository navigation
Expand file tree
/
Copy pathgenerate_walker.py
More file actions
88 lines (73 loc) · 2.5 KB
/
Copy pathgenerate_walker.py
File metadata and controls
88 lines (73 loc) · 2.5 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
#####################################################################################
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# This source code is subject to terms and conditions of the Apache License, Version 2.0. A
# copy of the license can be found in the License.html file at the root of this distribution. If
# you cannot locate the Apache License, Version 2.0, please send an email to
# [email protected]. By using this source code in any fashion, you are agreeing to be bound
# by the terms of the Apache License, Version 2.0.
#
# You must not remove this notice, or any other, from this software.
#
#
#####################################################################################
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 main():
return generate(
("Python AST Walker", gen_python_walker),
("Python AST Walker Nonrecursive", gen_python_walker_nr),
)
if __name__=="__main__":
main()