Skip to content
Snippets Groups Projects
Commit 8ac7e4b8 authored by braught2's avatar braught2
Browse files

wip

parent 94dd8d53
No related branches found
No related tags found
No related merge requests found
#parse python file #parse python file
#REQUIRES PYTHON 3.8!
from cgitb import reset from cgitb import reset
#import clang.cindex #import clang.cindex
import typing import typing
...@@ -14,35 +15,49 @@ import ast ...@@ -14,35 +15,49 @@ import ast
#import clang.cfg #import clang.cfg
class Statement: class Statement:
def __init__(self, code, mode, modeType): def __init__(self, code, mode, modeType):
self.code = code self.code = code
self.modeType = modeType self.modeType = modeType
self.mode = mode self.mode = mode
def __init__(self, code): def __init__(self, code):
self.code = code self.code = code
self.modeType = None self.modeType = None
self.mode = None self.mode = None
def print(self):
print(self.code)
class Guard(Statement): class Guard(Statement):
def __init__(self, code, mode, modeType): def __init__(self, code, mode, modeType):
super().__init__(self, code, mode, modeType) super().__init__(code, mode, modeType)
def __init__(self, code): def __init__(self, code):
super().__init__(self, code) super().__init__(code)
def isModeCheck(self):
return self.modeType != None
def isModeCheck(): def parseGuard(node, code):
return modeType != None #TODO parse out mode and modeType
return Guard(ast.get_source_segment(code, node.test))
class Reset(Statement): class Reset(Statement):
def __init__(self, code, mode, modeType): def __init__(self, code, mode, modeType):
super().__init__(self, code, mode, modeType) super().__init__(code, mode, modeType)
def __init__(self, code): def __init__(self, code):
super().__init__(self, code) super().__init__(code)
def isModeUpdate(): def isModeUpdate(self):
return modeType != None return self.modeType != None
def parseReset(node, code):
#TODO parse out mode and modeType
return Reset(ast.get_source_segment(code, node))
def walktree(code, tree): def walktree(code, tree):
vars = [] vars = []
...@@ -50,7 +65,7 @@ def walktree(code, tree): ...@@ -50,7 +65,7 @@ def walktree(code, tree):
for node in ast.walk(tree): #don't think we want to walk the whole thing because lose ordering/depth for node in ast.walk(tree): #don't think we want to walk the whole thing because lose ordering/depth
if isinstance(node, ast.FunctionDef): if isinstance(node, ast.FunctionDef):
if node.name == 'controller': if node.name == 'controller':
print(node.body) #print(node.body)
out = parsenodelist(code, node.body, False) out = parsenodelist(code, node.body, False)
#args = node.args #args = node.args
#for arg in args: #for arg in args:
...@@ -66,60 +81,55 @@ def parsenodelist(code, nodes, addResets): ...@@ -66,60 +81,55 @@ def parsenodelist(code, nodes, addResets):
found = [] found = []
for childnode in nodes: for childnode in nodes:
if isinstance(childnode, ast.Assign) and addResets: if isinstance(childnode, ast.Assign) and addResets:
#found.append(str(childnode.targets[0].id)+'=' + str(childnode.value.id)) #TODO, does this work with a value? reset = Reset.parseReset(childnode, code)
print("found reset" + ast.get_source_segment(code, childnode)) print("found reset: " + reset.code)
childrens_resets.append(ast.get_source_segment(code, childnode)) childrens_resets.append(reset)
if isinstance(childnode, ast.If): if isinstance(childnode, ast.If):
childrens_guards.append(ast.get_source_segment(code, childnode.test)) guard = Guard.parseGuard(childnode, code)
#childrens_guards.append(childnode.test.id) childrens_guards.append(guard)
print("found if statement: " + ast.get_source_segment(code, childnode.test)) print("found if statement: " + guard.code)
results = parsenodelist(code, childnode.body, True) tempresults = parsenodelist(code, childnode.body, True)
for result in results: for result in tempresults:
found.append(results) found.append(results)
#TODO - can add support for elif and else #TODO - can add support for elif and else
print("********")
print("Begin ending iteration:")
print("We have found this many items before: " + str(len(found)))
for item in found:
if isinstance(item, Statement):
print(item.code)
else:
print(item)
print("And now we want to add these -----")
for item in childrens_guards:
print(item.code)
for item in childrens_resets:
print(item.code)
print("-------")
if len(found) == 0 and len(childrens_resets) > 0: if len(found) == 0 and len(childrens_resets) > 0:
found.append([]) found.append([])
for item in found: for item in found:
for reset in childrens_resets: for reset in childrens_resets:
#for item in found: #for item in found:
item.append([reset, 'r']) item.append(reset)
results.append(item) results.append(item)
for guard in childrens_guards: for guard in childrens_guards:
item.append([guard, 'g']) item.append(guard)
print("now we generated these results -----")
for result in results:
for item in result:
if isinstance(item, Statement):
print(item.code)
else:
print(item)
print("----------")
print("********")
return results return results
def parsenodes(innode):
for node in innode:
#print(node)
if isinstance(node, ast.If):
print("found if")
print(node.body)
#walktree(node.body)
#walktree(node)
#print(node.name)
class BinOpVisitor(ast.NodeVisitor):
def visit_BinOp(self, node):
print(f"found BinOp at line: {node.lineno}")
self.generic_visit(node)
class Analyzer(ast.NodeVisitor):
def __init__(self):
self.stats = {'if': []}
def visit_If(self, node):
# Add the "s" attribute access here
print("If:", node.test.left)
self.stats["if"].append(node.body)
self.generic_visit(node)
def report(self):
pprint(self.stats)
##main code### ##main code###
#print(sys.argv) #print(sys.argv)
...@@ -146,9 +156,11 @@ if __name__ == "__main__": ...@@ -146,9 +156,11 @@ if __name__ == "__main__":
#tree = ast.parse() #tree = ast.parse()
results = walktree(code, tree) results = walktree(code, tree)
print("resultsssss:") print("resultsssss:")
for result in results: #for result in results:
print(result) # for item in result:
print() # item.print()
# print()
print(results)
#a = Analyzer() #a = Analyzer()
#a.visit(tree) #a.visit(tree)
...@@ -157,7 +169,7 @@ if __name__ == "__main__": ...@@ -157,7 +169,7 @@ if __name__ == "__main__":
#cfge = pycfg.PyCFGExtractor() #cfge = pycfg.PyCFGExtractor()
#for node in ast.walk(tree): #for node in ast.walk(tree):
# print(node.__class__.__name__) # print(node.__class__.__name__)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment