-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathast_conversion.py
43 lines (39 loc) · 1.25 KB
/
ast_conversion.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
def convert(ast):
increase_by = {} # count of how many idx to increase the new idx by:
# each time there is a value node
cur = 0
for i, node in enumerate(ast):
increase_by[i] = cur
if "value" in node:
cur += 1
new_dp = []
for i, node in enumerate(ast):
inc = increase_by[i]
if "value" in node:
child = [i + inc + 1]
if "children" in node:
child += [n + increase_by[n] for n in node["children"]]
new_dp.append({"type": node["type"], "children": child})
new_dp.append({"value": node["value"]})
else:
if "children" in node:
node["children"] = [
n + increase_by[n] for n in node["children"]
]
new_dp.append(node)
# sanity check
children = []
for node in new_dp:
if "children" in node:
children += node["children"]
assert len(children) == len(set(children))
return new_dp
def get_dfs(ast, only_leaf=False):
dp = []
for node in ast:
if "value" in node:
dp.append(str(node["value"]))
else:
if not only_leaf:
dp.append("<" + node["type"] + ">")
return dp