-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonToAST.py
More file actions
88 lines (69 loc) · 2.44 KB
/
pythonToAST.py
File metadata and controls
88 lines (69 loc) · 2.44 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
from tree_sitter import Language, Parser
import tree_sitter_python as tspython
from ASTNode import *
# print("Testing execution on terminal")
PY_LANGUAGE = Language(tspython.language())
parser = Parser(PY_LANGUAGE)
def pythonToAST(code):
"""Returns a Tree object by parsing the tree and getting the root node"""
tree = parser.parse(bytes(code, encoding="utf-8"))
objAST = tree.root_node
assert tree.root_node.type == "module" # Ensures that the root node of Python code is "module"
return objAST
# def nodeToDict(node):
# """Returns a dictionary format of the AST"""
# return {
# "type": node.type,
# "start_point": node.start_point,
# "end_point": node.end_point,
# "children": [nodeToDict(child) for child in node.children]
# }
def ASTSummarization(objAST): # ---> The AST of the code snippet
# tree = parser.parse(bytes(objAST, encoding="utf-8"))
"""Perform a recursive function for an in-order traversal starting from the AST's root node"""
def findBodyNode(node):
if node.type == 'block':
return node
for child in node.children:
res = findBodyNode(child)
if res:
return res
return None
try:
body_node = findBodyNode(objAST)
listSeqs = []
for child in body_node.children:
listSeqs.append(child.type)
return listSeqs
except AttributeError:
print("Could not find the root body node")
return []
def ASTSumRepresentation(listSeqs):
"""Returns a tree representation of ASTSum after extracting from the original AST"""
root = None
repr = []
for node_type in listSeqs:
non_terminal_node = ASTNode(node_type)
if not root:
root = non_terminal_node
repr.append(root)
else:
parent_node = repr[0]
parent_node.add_child(non_terminal_node)
repr = [non_terminal_node]
return root
def test():
code = """
def process_data(input_list):
# Filter out even numbers
odd_numbers = [num for num in input_list if num % 2 != 0]
# Multiply each remaining number by 3
multiplied_numbers = [num * 3 for num in odd_numbers]
# Return the sum of these numbers
return sum(multiplied_numbers)
"""
objAST = pythonToAST(code)
listSeqs = ASTSummarization(objAST)
print(listSeqs)
# print(ASTSumRepresentation(listSeqs))
test()