-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreader.py
More file actions
37 lines (31 loc) · 912 Bytes
/
threader.py
File metadata and controls
37 lines (31 loc) · 912 Bytes
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
import AST
from AST import addToClass
@addToClass(AST.Node)
def thread(self, lastNode):
for c in self.children:
lastNode = c.thread(lastNode)
lastNode.addNext(self)
return self
@addToClass(AST.WhileNode)
def thread(self, lastNode):
beforeCond = lastNode
exitCond = self.children[0].thread(lastNode)
exitCond.addNext(self)
exitBody = self.children[1].thread(self)
exitBody.addNext(beforeCond.next[-1])
return self
def thread(tree):
entry = AST.EntryNode()
tree.thread(entry)
return entry
if __name__ == "__main__":
from parser5 import parse
import sys, os
prog = open(sys.argv[1]).read()
ast = parse(prog)
entry = thread(ast)
graph = ast.makegraphicaltree()
entry.threadTree(graph)
name = os.path.splitext(sys.argv[1])[0]+'-ast-threaded.pdf'
graph.write_pdf(name)
print ("wrote threaded ast to", name)