-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathast.py
232 lines (165 loc) · 6.45 KB
/
ast.py
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
# OSL Abstact Syntax Tree
from __future__ import (absolute_import, division,
print_function, unicode_literals)
import os, sys
class Node:
def __init__(self, type, *children):
self.type = type
self.children = list(children)
self.tmp = None
def append(self, child):
self.children.append(child)
def insert(self, num, child):
self.children.insert(num, child)
def num_childs(self):
return len(self.children)
def get_child(self, type_or_idx):
if isinstance(type_or_idx, str) or (sys.version_info[0] == 2 and isinstance(type_or_idx, unicode)):
for c in self.children:
if isinstance(c, Node) and c.type == type_or_idx:
return c
else:
return self.children[type_or_idx]
return None
def set_child(self, index, node):
self.children[index] = node
def traverse_nodes(self, callback, node=None):
'''DFS'''
if node is None:
node = self
callback(node)
for c in node.children:
if isinstance(c, Node):
self.traverse_nodes(callback, c)
def find_nodes(self, *types):
'''Find all children nodes of given type(s)'''
out = []
def cb(node):
# NOTE: not working in python2
#nonlocal out
for type in types:
if node.type == type:
out.append(node)
self.traverse_nodes(cb)
return out
def find_node(self, type):
'''Find first child node of given type'''
nodes = self.find_nodes(type)
if len(nodes):
return nodes[0]
else:
return None
def get_ancestor(self, ast):
'''Get nearest parent node'''
ast.assign_tmp_parents()
return self.tmp
def find_ancestor_node(self, ast, type):
'''Find parent node of the given type(s)'''
ast.assign_tmp_parents()
node = self
while True:
parent = node.tmp
if parent:
if parent.type == type:
return parent
else:
node = parent
else:
return None
def assign_tmp_parents(self):
'''For internal use'''
# root node
self.tmp = None
def cb(node):
for c in node.children:
if isinstance(c, Node):
# parent
c.tmp = node
self.traverse_nodes(cb)
def get_shader_name(self):
return self.find_node('shader-declaration').get_child(1)
def get_variables(self):
variables = {}
decl_nodes = self.find_nodes('variable-declaration',
'function-formal-param',
'shader-formal-param')
for dn in decl_nodes:
type = dn.get_child('typespec').get_typespec_type()
if dn.type == 'variable-declaration':
for expr in dn.find_nodes('def-expression'):
name = expr.get_child(0)
variables[name] = type
else:
name = dn.get_child(2)
variables[name] = type
return variables
def get_typespec_type(self):
assert self.type == 'typespec'
if self.get_child('simple-typename'):
return self.get_child('simple-typename').get_child(0)
else:
return self.get_child(0)
def get_functions(self):
functions = {}
decl_nodes = self.find_nodes('function-declaration')
for dn in decl_nodes:
type = dn.get_child('typespec').get_typespec_type()
name = dn.get_child(1)
params = dn.get_child(2)
spec = [type]
if params:
for param in params.children:
spec.append(param.get_child('typespec').get_typespec_type())
functions[name] = tuple(spec)
return functions
def get_shader_params(self):
inputs = []
outputs = []
shader_name = self.get_shader_name()
decl_nodes = self.find_nodes('shader-formal-param')
for dn in decl_nodes:
is_in = True if dn.get_child('outputspec').get_child(0) is None else False
type = dn.get_child('typespec').get_child('simple-typename').get_child(0)
name = dn.get_child(2)
if is_in:
init_ast = None
init_node = dn.get_child('initializer')
if init_node:
param = dn.clone()
param.get_child('outputspec').set_child(0, 'output')
init_ast = self.create_shader(shader_name + '_init_' + str(decl_nodes.index(dn)),
[param],
[Node('statement-semi', Node('def-expression', name, init_node.clone()))])
uses_gl_var = False
for gl_var_name in ['P', 'I', 'N', 'u', 'v']:
if init_ast.uses_variable(gl_var_name):
uses_gl_var = True
if init_ast and uses_gl_var:
inputs.append((type, name, init_ast))
else:
inputs.append((type, name, None))
else:
outputs.append((type, name, None))
return inputs, outputs
def uses_variable(self, name):
var_nodes = self.find_nodes('variable-ref')
for vn in var_nodes:
if vn.find_node('variable-lvalue').get_child(0) == name:
return True
return False
def create_shader(self, name, params, statements):
params = Node('shader-formal-params', *params)
statements = Node('statement-list', *statements)
return Node('shader-file', Node('shader-declaration', 'shader', name, None, params, statements))
def clone(self):
cloned_children = [c.clone() if isinstance(c, Node) else c for c in self.children]
return Node(self.type, *cloned_children)
def print_tree(self, level=0, indent=2, node=None):
if node is None:
node = self
print(' ' * indent * level + node.type)
for c in node.children:
if isinstance(c, Node):
self.print_tree(level+1, indent, c)
else:
print(' ' * indent * (level + 1) + str(c))