Skip to content

Commit

Permalink
Part 12. Nested procedures.
Browse files Browse the repository at this point in the history
  • Loading branch information
rspivak committed Nov 27, 2016
1 parent 53ad5d2 commit 106e401
Show file tree
Hide file tree
Showing 5 changed files with 1,172 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Source code for the series **Let's Build A Simple Interpreter**
+ [Let's Build A Simple Interpreter. Part 9.](http://ruslanspivak.com/lsbasi-part9/)
+ [Let's Build A Simple Interpreter. Part 10.](http://ruslanspivak.com/lsbasi-part10/)
+ [Let's Build A Simple Interpreter. Part 11.](http://ruslanspivak.com/lsbasi-part11/)
+ [Let's Build A Simple Interpreter. Part 12.](http://ruslanspivak.com/lsbasi-part12/)
182 changes: 182 additions & 0 deletions part12/python/genastdot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
###############################################################################
# AST visualizer - generates a DOT file for Graphviz. #
# #
# To generate an image from the DOT file run $ dot -Tpng -o ast.png ast.dot #
# #
###############################################################################
import argparse
import textwrap

from spi import Lexer, Parser, NodeVisitor


class ASTVisualizer(NodeVisitor):
def __init__(self, parser):
self.parser = parser
self.ncount = 1
self.dot_header = [textwrap.dedent("""\
digraph astgraph {
node [shape=circle, fontsize=12, fontname="Courier", height=.1];
ranksep=.3;
edge [arrowsize=.5]
""")]
self.dot_body = []
self.dot_footer = ['}']

def visit_Program(self, node):
s = ' node{} [label="Program"]\n'.format(self.ncount)
self.dot_body.append(s)
node._num = self.ncount
self.ncount += 1

self.visit(node.block)

s = ' node{} -> node{}\n'.format(node._num, node.block._num)
self.dot_body.append(s)

def visit_Block(self, node):
s = ' node{} [label="Block"]\n'.format(self.ncount)
self.dot_body.append(s)
node._num = self.ncount
self.ncount += 1

for declaration in node.declarations:
self.visit(declaration)
self.visit(node.compound_statement)

for decl_node in node.declarations:
s = ' node{} -> node{}\n'.format(node._num, decl_node._num)
self.dot_body.append(s)

s = ' node{} -> node{}\n'.format(
node._num,
node.compound_statement._num
)
self.dot_body.append(s)

def visit_VarDecl(self, node):
s = ' node{} [label="VarDecl"]\n'.format(self.ncount)
self.dot_body.append(s)
node._num = self.ncount
self.ncount += 1

self.visit(node.var_node)
s = ' node{} -> node{}\n'.format(node._num, node.var_node._num)
self.dot_body.append(s)

self.visit(node.type_node)
s = ' node{} -> node{}\n'.format(node._num, node.type_node._num)
self.dot_body.append(s)

def visit_ProcedureDecl(self, node):
s = ' node{} [label="ProcDecl:{}"]\n'.format(
self.ncount,
node.proc_name
)
self.dot_body.append(s)
node._num = self.ncount
self.ncount += 1

self.visit(node.block_node)
s = ' node{} -> node{}\n'.format(node._num, node.block_node._num)
self.dot_body.append(s)

def visit_Type(self, node):
s = ' node{} [label="{}"]\n'.format(self.ncount, node.token.value)
self.dot_body.append(s)
node._num = self.ncount
self.ncount += 1

def visit_Num(self, node):
s = ' node{} [label="{}"]\n'.format(self.ncount, node.token.value)
self.dot_body.append(s)
node._num = self.ncount
self.ncount += 1

def visit_BinOp(self, node):
s = ' node{} [label="{}"]\n'.format(self.ncount, node.op.value)
self.dot_body.append(s)
node._num = self.ncount
self.ncount += 1

self.visit(node.left)
self.visit(node.right)

for child_node in (node.left, node.right):
s = ' node{} -> node{}\n'.format(node._num, child_node._num)
self.dot_body.append(s)

def visit_UnaryOp(self, node):
s = ' node{} [label="unary {}"]\n'.format(self.ncount, node.op.value)
self.dot_body.append(s)
node._num = self.ncount
self.ncount += 1

self.visit(node.expr)
s = ' node{} -> node{}\n'.format(node._num, node.expr._num)
self.dot_body.append(s)

def visit_Compound(self, node):
s = ' node{} [label="Compound"]\n'.format(self.ncount)
self.dot_body.append(s)
node._num = self.ncount
self.ncount += 1

for child in node.children:
self.visit(child)
s = ' node{} -> node{}\n'.format(node._num, child._num)
self.dot_body.append(s)

def visit_Assign(self, node):
s = ' node{} [label="{}"]\n'.format(self.ncount, node.op.value)
self.dot_body.append(s)
node._num = self.ncount
self.ncount += 1

self.visit(node.left)
self.visit(node.right)

for child_node in (node.left, node.right):
s = ' node{} -> node{}\n'.format(node._num, child_node._num)
self.dot_body.append(s)

def visit_Var(self, node):
s = ' node{} [label="{}"]\n'.format(self.ncount, node.value)
self.dot_body.append(s)
node._num = self.ncount
self.ncount += 1

def visit_NoOp(self, node):
s = ' node{} [label="NoOp"]\n'.format(self.ncount)
self.dot_body.append(s)
node._num = self.ncount
self.ncount += 1

def gendot(self):
tree = self.parser.parse()
self.visit(tree)
return ''.join(self.dot_header + self.dot_body + self.dot_footer)


def main():
argparser = argparse.ArgumentParser(
description='Generate an AST DOT file.'
)
argparser.add_argument(
'fname',
help='Pascal source file'
)
args = argparser.parse_args()
fname = args.fname
text = open(fname, 'r').read()

lexer = Lexer(text)
parser = Parser(lexer)
viz = ASTVisualizer(parser)
content = viz.gendot()
print(content)


if __name__ == '__main__':
main()
23 changes: 23 additions & 0 deletions part12/python/part12.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
PROGRAM Part12;
VAR
a : INTEGER;

PROCEDURE P1;
VAR
a : REAL;
k : INTEGER;

PROCEDURE P2;
VAR
a, z : INTEGER;
BEGIN {P2}
z := 777;
END; {P2}

BEGIN {P1}

END; {P1}

BEGIN {Part12}
a := 10;
END. {Part12}
Loading

0 comments on commit 106e401

Please sign in to comment.