Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
OUTDATED Verse-library
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
AutonomyCourse_ECEIllinois
OUTDATED Verse-library
Commits
8ac7e4b8
Commit
8ac7e4b8
authored
2 years ago
by
braught2
Browse files
Options
Downloads
Patches
Plain Diff
wip
parent
94dd8d53
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
pythonparser.py
+70
-58
70 additions, 58 deletions
pythonparser.py
with
70 additions
and
58 deletions
pythonparser.py
+
70
−
58
View file @
8ac7e4b8
#parse python file
#REQUIRES PYTHON 3.8!
from
cgitb
import
reset
#import clang.cindex
import
typing
...
...
@@ -14,35 +15,49 @@ import ast
#import clang.cfg
class
Statement
:
def
__init__
(
self
,
code
,
mode
,
modeType
):
self
.
code
=
code
self
.
modeType
=
modeType
self
.
mode
=
mode
def
__init__
(
self
,
code
,
mode
,
modeType
):
self
.
code
=
code
self
.
modeType
=
modeType
self
.
mode
=
mode
def
__init__
(
self
,
code
):
self
.
code
=
code
self
.
modeType
=
None
self
.
mode
=
None
def
print
(
self
):
print
(
self
.
code
)
class
Guard
(
Statement
):
def
__init__
(
self
,
code
,
mode
,
modeType
):
super
().
__init__
(
self
,
code
,
mode
,
modeType
)
super
().
__init__
(
code
,
mode
,
modeType
)
def
__init__
(
self
,
code
):
super
().
__init__
(
self
,
code
)
super
().
__init__
(
code
)
def
isModeCheck
(
self
):
return
self
.
modeType
!=
None
def
isModeCheck
():
return
modeType
!=
None
def
parseGuard
(
node
,
code
):
#TODO parse out mode and modeType
return
Guard
(
ast
.
get_source_segment
(
code
,
node
.
test
))
class
Reset
(
Statement
):
def
__init__
(
self
,
code
,
mode
,
modeType
):
super
().
__init__
(
self
,
code
,
mode
,
modeType
)
super
().
__init__
(
code
,
mode
,
modeType
)
def
__init__
(
self
,
code
):
super
().
__init__
(
self
,
code
)
super
().
__init__
(
code
)
def
isModeUpdate
():
return
modeType
!=
None
def
isModeUpdate
(
self
):
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
):
vars
=
[]
...
...
@@ -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
if
isinstance
(
node
,
ast
.
FunctionDef
):
if
node
.
name
==
'
controller
'
:
print
(
node
.
body
)
#
print(node.body)
out
=
parsenodelist
(
code
,
node
.
body
,
False
)
#args = node.args
#for arg in args:
...
...
@@ -66,60 +81,55 @@ def parsenodelist(code, nodes, addResets):
found
=
[]
for
childnode
in
nodes
:
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?
print
(
"
found reset
"
+
ast
.
get_source_segment
(
code
,
childn
ode
)
)
childrens_resets
.
append
(
ast
.
get_source_segment
(
code
,
childnode
)
)
reset
=
Reset
.
parseReset
(
childnode
,
code
)
print
(
"
found reset
:
"
+
reset
.
c
ode
)
childrens_resets
.
append
(
reset
)
if
isinstance
(
childnode
,
ast
.
If
):
childrens_guards
.
append
(
ast
.
get_source_segment
(
code
,
childnode
.
test
)
)
#
childrens_guards.append(
childnode.test.i
d)
print
(
"
found if statement:
"
+
ast
.
get_source_segment
(
code
,
childnode
.
test
)
)
results
=
parsenodelist
(
code
,
childnode
.
body
,
True
)
for
result
in
results
:
guard
=
Guard
.
parseGuard
(
childnode
,
code
)
childrens_guards
.
append
(
guar
d
)
print
(
"
found if statement:
"
+
guard
.
code
)
temp
results
=
parsenodelist
(
code
,
childnode
.
body
,
True
)
for
result
in
temp
results
:
found
.
append
(
results
)
#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
:
found
.
append
([])
for
item
in
found
:
for
reset
in
childrens_resets
:
#for item in found:
item
.
append
(
[
reset
,
'
r
'
]
)
item
.
append
(
reset
)
results
.
append
(
item
)
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
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###
#print(sys.argv)
...
...
@@ -146,9 +156,11 @@ if __name__ == "__main__":
#tree = ast.parse()
results
=
walktree
(
code
,
tree
)
print
(
"
resultsssss:
"
)
for
result
in
results
:
print
(
result
)
print
()
#for result in results:
# for item in result:
# item.print()
# print()
print
(
results
)
#a = Analyzer()
#a.visit(tree)
...
...
@@ -157,7 +169,7 @@ if __name__ == "__main__":
#cfge = pycfg.PyCFGExtractor()
#for node in ast.walk(tree):
# print(node.__class__.__name__)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment