55%
66% http://www.python.org/doc/current/doc/doc.html
77
8- \documentclass {manual }
8+ \documentclass {howto }
99
1010\title {Python compiler package}
1111
1515% the rest is at your discretion.
1616\authoraddress {
1717 PythonLabs \\
18- Zope Corp. \\
18+ Zope Corporation \\
19192020}
2121
3636
3737\maketitle
3838
39- % This makes the contents more accessible from the front page of the HTML.
40- \ifhtml
41- \chapter* {Front Matter\label {front } }
42- \fi
43-
44- % \input{copyright}
45-
4639\begin {abstract }
4740
4841\noindent
@@ -55,15 +48,19 @@ \chapter*{Front Matter\label{front}}
5548
5649\tableofcontents
5750
58- \chapter {Introduction\label {Introduction } }
51+
52+ \section {Introduction\label {Introduction } }
5953
6054XXX Need basic intro
6155
6256XXX what are the major advantages... the abstract syntax is much
6357closer to the python source...
6458
59+
6560\section {The basic interface }
6661
62+ \declaremodule {}{compiler}
63+
6764The top-level of the package defines four functions.
6865
6966\begin {funcdesc }{parse}{buf}
@@ -79,20 +76,22 @@ \section{The basic interface}
7976\code {parse(open(\var {path}).read())}.
8077\end {funcdesc }
8178
82- \begin {funcdesc }{walk}{ast, visitor, \optional {verbose=None }}
79+ \begin {funcdesc }{walk}{ast, visitor\optional {, verbose}}
8380Do a pre-order walk over the abstract syntax tree \var {ast}. Call the
8481appropriate method on the \var {visitor} instance for each node
85- encountered.
82+ encountered.
8683\end {funcdesc }
8784
88- \begin {funcdesc }{compile}{filename}
89- Compile the file \var {filename} and generated \var {filename}.pyc.
85+ \begin {funcdesc }{compile}{path}
86+ Compile the file \var {path} and generate the corresponding \file {.pyc}
87+ file.
9088\end {funcdesc }
9189
9290The \module {compiler} package contains the following modules:
93- \module {ast}, \module {consts}, \module {future}, \module {misc},
94- \module {pyassem}, \module {pycodegen}, \module {symbols},
95- \module {transformer}, and \module {visitor}.
91+ \refmodule [compiler.ast]{ast}, \module {consts}, \module {future},
92+ \module {misc}, \module {pyassem}, \module {pycodegen}, \module {symbols},
93+ \module {transformer}, and \refmodule [compiler.visitor]{visitor}.
94+
9695
9796\section {Limitations }
9897
@@ -106,38 +105,43 @@ \section{Limitations}
106105if a name appears more than once in an argument list:
107106\code {def f(x, x): ...}
108107
109- \chapter {Python Abstract Syntax }
110108
111- \section {Introduction }
109+ \section {Python Abstract Syntax }
112110
113111The \module {compiler.ast} module defines an abstract syntax for
114112Python. In the abstract syntax tree, each node represents a syntactic
115113construct. The root of the tree is \class {Module} object.
116114
117115The abstract syntax offers a higher level interface to parsed Python
118- source code. The \module {parser} module and the compiler written in C
119- for the Python interpreter use a concrete syntax tree. The concrete
120- syntax is tied closely to the grammar description used for the Python
121- parser. Instead of a single node for a construct, there are often
122- several levels of nested nodes that are introduced by Python's
123- precedence rules.
116+ source code. The \ulink {\module {parser}}
117+ {http://www.python.org/doc/current/lib/module-parser.html}
118+ module and the compiler written in C for the Python interpreter use a
119+ concrete syntax tree. The concrete syntax is tied closely to the
120+ grammar description used for the Python parser. Instead of a single
121+ node for a construct, there are often several levels of nested nodes
122+ that are introduced by Python's precedence rules.
124123
125124The abstract syntax tree is created by the
126125\module {compiler.transformer} module. The transformer relies on the
127126builtin Python parser to generate a concrete syntax tree. It
128127generates an abstract syntax tree from the concrete tree.
129128
130- The \module {transformer} module was created by Greg Stein and Bill
131- Tutt for the Python-to-C compiler. The current version contains a
129+ The \module {transformer} module was created by Greg
130+ Stein\index {Stein, Greg} and Bill Tutt\index {Tutt, Bill} for an
131+ experimental Python-to-C compiler. The current version contains a
132132number of modifications and improvements, but the basic form of the
133133abstract syntax and of the transformer are due to Stein and Tutt.
134134
135+
135136\section {AST Nodes }
136137
137- The \module {ast} module is generated from a text file that describes
138- each node type and its elements. Each node type is represented as a
139- class that inherits from the abstract base class \class {ast.Node} and
140- defines a set of named attributes for child nodes.
138+ \declaremodule {}{compiler.ast}
139+
140+ The \module {compiler.ast} module is generated from a text file that
141+ describes each node type and its elements. Each node type is
142+ represented as a class that inherits from the abstract base class
143+ \class {compiler.ast.Node} and defines a set of named attributes for
144+ child nodes.
141145
142146\begin {classdesc }{Node}{}
143147
@@ -153,26 +157,27 @@ \section{AST Nodes}
153157 Each \class {Node} instance has a \member {lineno} attribute which may
154158 be \code {None}. XXX Not sure what the rules are for which nodes
155159 will have a useful lineno.
160+ \end {classdesc }
156161
157- \begin {methoddesc }{getChildren}{}
158- Returns a flattened list of the child nodes and objects in the
159- order they occur. Specifically, the order of the nodes is the
160- order in which they appear in the Python grammar. Not all of the
161- children are \class {Node} instances. The names of functions and
162- classes, for example, are plain strings.
163- \end {methoddesc }
162+ All \class {Node} objects offer the following methods:
164163
165- \begin {methoddesc }{getChildNodes}{}
166- Returns a flattened list of the child nodes in the order they
167- occur. This method is like \method {getChildNodes()}, except that it
168- only returns those children that are \class {Node} instances.
169- \end {methoddesc }
164+ \begin {methoddesc }{getChildren}{}
165+ Returns a flattened list of the child nodes and objects in the
166+ order they occur. Specifically, the order of the nodes is the
167+ order in which they appear in the Python grammar. Not all of the
168+ children are \class {Node} instances. The names of functions and
169+ classes, for example, are plain strings.
170+ \end {methoddesc }
170171
171- \end {classdesc }
172+ \begin {methoddesc }{getChildNodes}{}
173+ Returns a flattened list of the child nodes in the order they
174+ occur. This method is like \method {getChildren()}, except that it
175+ only returns those children that are \class {Node} instances.
176+ \end {methoddesc }
172177
173178Two examples illustrate the general structure of \class {Node}
174- classes. The while statement is defined by the following grammar
175- production:
179+ classes. The \keyword { while} statement is defined by the following
180+ grammar production:
176181
177182\begin {verbatim }
178183while_stmt: "while" expression ":" suite
@@ -182,28 +187,28 @@ \section{AST Nodes}
182187The \class {While} node has three attributes: \member {test},
183188\member {body}, and \member {else_}. (If the natural name for an
184189attribute is also a Python reserved word, it can't be used as an
185- attribute name. An underscore is appended to the word to make it
186- legal, hence \code {else_} instead of \code {else}.)
190+ attribute name. An underscore is appended to the word to make it a
191+ legal identifier , hence \member {else_} instead of \keyword {else}.)
187192
188- The if statement is more complicated because it can include several
189- tests.
193+ The \keyword {if} statement is more complicated because it can include
194+ several tests.
190195
191196\begin {verbatim }
192197if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
193198\end {verbatim }
194199
195200The \class {If} node only defines two attributes: \member {tests} and
196201\member {else_}. The \member {tests} attribute is a sequence of test
197- expression, consequent body pairs. There is one pair of each if/elif
198- clause. The first element of the pair is the test expression. The
199- second elements is a \class {Stmt} node that contains the code to
200- execute if the test is true.
202+ expression, consequent body pairs. There is one pair for each
203+ \keyword {if}/ \keyword {elif} clause. The first element of the pair is
204+ the test expression. The second elements is a \class {Stmt} node that
205+ contains the code to execute if the test is true.
201206
202207The \method {getChildren()} method of \class {If} returns a flat list of
203- child nodes. If there are three if/ elif clauses and no else clause,
204- then \method {getChildren()} will return a list of six elements: the
205- first test expression, the first \class {Stmt} , the second text
206- expression, etc.
208+ child nodes. If there are three \keyword {if}/ \keyword { elif} clauses
209+ and no \keyword {else} clause, then \method {getChildren()} will return
210+ a list of six elements: the first test expression , the first
211+ \class {Stmt}, the second text expression, etc.
207212
208213The following table lists each of the \class {Node} subclasses defined
209214in \module {compiler.ast} and each of the public attributes available
@@ -215,6 +220,7 @@ \section{AST Nodes}
215220
216221\input {asttable }
217222
223+
218224\section {Assignment nodes }
219225
220226There is a collection of nodes used to represent assignments. Each
@@ -229,9 +235,12 @@ \section{Assignment nodes}
229235XXX Explain what the AssXXX nodes are for. Mention \code {a.b.c = 2}
230236as an example. Explain what the flags are for.
231237
232- \chapter {Using Visitors to Walk ASTs }
233238
234- The visitor pattern is ... The \module {compiler} package uses a
239+ \section {Using Visitors to Walk ASTs }
240+
241+ \declaremodule {}{compiler.visitor}
242+
243+ The visitor pattern is ... The \refmodule {compiler} package uses a
235244variant on the visitor pattern that takes advantage of Python's
236245introspection features to elminiate the need for much of the visitor's
237246infrastructure.
@@ -243,6 +252,9 @@ \chapter{Using Visitors to Walk ASTs}
243252
244253XXX The magic \method {visit()} method for visitors.
245254
255+ \begin {funcdesc }{walk}{tree, visitor\optional {, verbose}}
256+ \end {funcdesc }
257+
246258\begin {classdesc }{ASTVisitor}{}
247259
248260The \class {ASTVisitor} is responsible for walking over the tree in the
@@ -259,48 +271,32 @@ \chapter{Using Visitors to Walk ASTs}
259271method can be used to visit a particular child node. If no visitor is
260272found for a particular node type, the \method {default()} method is
261273called.
274+ \end {classdesc }
275+
276+ \class {ASTVisitor} objects have the following methods:
262277
263278XXX describe extra arguments
264279
265- \begin {methoddesc }{default}{node\optional {, *args }}
280+ \begin {methoddesc }{default}{node\optional {, \moreargs }}
266281\end {methoddesc }
267282
268- \begin {methoddesc }{dispatch}{node\optional {, *args }}
283+ \begin {methoddesc }{dispatch}{node\optional {, \moreargs }}
269284\end {methoddesc }
270285
271286\begin {methoddesc }{preorder}{tree, visitor}
272287\end {methoddesc }
273288
274- \end {classdesc }
275289
276- \begin {funcdesc }{walk}{tree, visitor\optional {, verbose=None}}
277- \end {funcdesc }
278-
279- \chapter {Bytecode Generation }
290+ \section {Bytecode Generation }
280291
281- The code generator is a visit that emits bytecodes. Each visit method
292+ The code generator is a visitor that emits bytecodes. Each visit method
282293can call the \method {emit()} method to emit a new bytecode. The basic
283294code generator is specialized for modules, classes, and functions. An
284295assembler converts that emitted instructions to the low-level bytecode
285296format. It handles things like generator of constant lists of code
286297objects and calculation of jump offsets.
287298
288- %
289- % The ugly "%begin{latexonly}" pseudo-environments are really just to
290- % keep LaTeX2HTML quiet during the \renewcommand{} macros; they're
291- % not really valuable.
292- %
293- % If you don't want the Module Index, you can remove all of this up
294- % until the second \input line.
295- %
296- % begin{latexonly}
297- \renewcommand {\indexname }{Module Index}
298- % end{latexonly}
299- \input {mod\jobname .ind } % Module Index
300-
301- % begin{latexonly}
302- \renewcommand {\indexname }{Index}
303- % end{latexonly}
304- \input {\jobname .ind } % Index
299+
300+ \input {compiler.ind } % Index
305301
306302\end {document }
0 commit comments