5151
5252\section {Introduction\label {Introduction } }
5353
54- XXX Need basic intro
55-
56- XXX what are the major advantages... the abstract syntax is much
57- closer to the python source...
58-
59-
60- \section {The basic interface }
54+ The \module {compiler} package is a Python source to bytecode
55+ translator written in Python. It uses the builtin parser and standard
56+ \ulink {\module {parser}}
57+ {http://www.python.org/doc/current/lib/module-parser.html} to
58+ generated a concrete syntax tree. This tree is used to generate an
59+ abstract syntax tree (AST) and then Python bytecode.
60+
61+ The full functionality of the package duplicates the builtin compiler
62+ provided with the Python interpreter. It is intended to match its
63+ behavior almost exactly. Why implement another compiler that does the
64+ same thing? The package is useful for a variety of purposes. It can
65+ be modified more easily than the builtin compiler. The AST it
66+ generates is useful for analyzing Python source code.
67+
68+ This manual explains how the various components of the
69+ \module {compiler} package work. It blends reference material with a
70+ tutorial. (At least it will when the document is done.)
71+
72+ \subsection {The basic interface }
6173
6274\declaremodule {}{compiler}
6375
64- The top-level of the package defines four functions.
76+ The top-level of the package defines four functions. If you import
77+ \module {compiler}, you will get these functions and a collection of
78+ modules contained in the package.
6579
6680\begin {funcdesc }{parse}{buf}
6781Returns an abstract syntax tree for the Python source code in \var {buf}.
@@ -92,8 +106,7 @@ \section{The basic interface}
92106\module {misc}, \module {pyassem}, \module {pycodegen}, \module {symbols},
93107\module {transformer}, and \refmodule [compiler.visitor]{visitor}.
94108
95-
96- \section {Limitations }
109+ \subsection {Limitations }
97110
98111There are some problems with the error checking of the compiler
99112package. The interpreter detects syntax errors in two distinct
@@ -105,6 +118,7 @@ \section{Limitations}
105118if a name appears more than once in an argument list:
106119\code {def f(x, x): ...}
107120
121+ A future version of the compiler should fix these problems.
108122
109123\section {Python Abstract Syntax }
110124
@@ -132,8 +146,7 @@ \section{Python Abstract Syntax}
132146number of modifications and improvements, but the basic form of the
133147abstract syntax and of the transformer are due to Stein and Tutt.
134148
135-
136- \section {AST Nodes }
149+ \subsection {AST Nodes }
137150
138151\declaremodule {}{compiler.ast}
139152
@@ -221,7 +234,7 @@ \section{AST Nodes}
221234\input {asttable }
222235
223236
224- \section {Assignment nodes }
237+ \subsection {Assignment nodes }
225238
226239There is a collection of nodes used to represent assignments. Each
227240assignment statement in the source code becomes a single
@@ -232,9 +245,74 @@ \section{Assignment nodes}
232245\class {AssAttr}, \class {AssList}, \class {AssName}, or
233246\class {AssTuple}.
234247
235- XXX Explain what the AssXXX nodes are for. Mention \code {a.b.c = 2}
236- as an example. Explain what the flags are for.
248+ Each target assignment node will describe the kind of object being
249+ assigned to: \class {AssName} for a simple name, e.g. \code {a = 1}.
250+ \class {AssAttr} for an attribute assigned, e.g. \code {a.x = 1}.
251+ \class {AssList} and \class {AssTuple} for list and tuple expansion
252+ respectively, e.g. \code {a, b, c = a_tuple}.
253+
254+ The target assignment nodes also have a \member {flags} attribute that
255+ indicates whether the node is being used for assignment or in a delete
256+ statement. The \class {AssName} is also used to represent a delete
257+ statement, e.g. \class {del x}.
258+
259+ When an expression contains several attribute references, an
260+ assignment or delete statement will contain only one \class {AssAttr}
261+ node -- for the final attribute reference. The other attribute
262+ references will be represented as \class {Getattr} nodes in the
263+ \member {expr} attribute of the \class {AssAttr} instance.
264+
265+ \subsection {Examples }
266+
267+ This section shows several simple examples of ASTs for Python source
268+ code. The examples demonstrate how to use the \function {parse()}
269+ function, what the repr of an AST looks like, and how to access
270+ attributes of an AST node.
237271
272+ The first module defines a single function. Assume it is stored in
273+ \file {/tmp/doublelib.py}.
274+
275+ \begin {verbatim }
276+ """This is an example module.
277+
278+ This is the docstring.
279+ """
280+
281+ def double(x):
282+ "Return twice the argument"
283+ return x * 2
284+ \end {verbatim }
285+
286+ In the interactive interpreter session below, I have reformatted the
287+ long AST reprs for readability. The AST reprs use unqualified class
288+ names. If you want to create an instance from a repr, you must import
289+ the class names from the \module {compiler.ast} module.
290+
291+ \begin {verbatim }
292+ >>> import compiler
293+ >>> mod = compiler.parseFile("/tmp/doublelib.py")
294+ >>> mod
295+ Module('This is an example module.\n\nThis is the docstring.\n',
296+ Stmt([Function('double', ['x'], [], 0, 'Return twice the argument',
297+ Stmt([Return(Mul((Name('x'), Const(2))))]))]))
298+ >>> from compiler.ast import *
299+ >>> Module('This is an example module.\n\nThis is the docstring.\n',
300+ ... Stmt([Function('double', ['x'], [], 0, 'Return twice the argument',
301+ ... Stmt([Return(Mul((Name('x'), Const(2))))]))]))
302+ Module('This is an example module.\n\nThis is the docstring.\n',
303+ Stmt([Function('double', ['x'], [], 0, 'Return twice the argument',
304+ Stmt([Return(Mul((Name('x'), Const(2))))]))]))
305+ >>> mod.doc
306+ 'This is an example module.\n\nThis is the docstring.\n'
307+ >>> for node in mod.node.nodes:
308+ ... print node
309+ ...
310+ Function('double', ['x'], [], 0, 'Return twice the argument',
311+ Stmt([Return(Mul((Name('x'), Const(2))))]))
312+ >>> func = mod.node.nodes[0]
313+ >>> func.code
314+ Stmt([Return(Mul((Name('x'), Const(2))))])
315+ \end {verbatim }
238316
239317\section {Using Visitors to Walk ASTs }
240318
0 commit comments