Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 1 | % Complete documentation on the extended LaTeX markup used for Python |
| 2 | % documentation is available in ``Documenting Python'', which is part |
| 3 | % of the standard documentation for Python. It may be found online |
| 4 | % at: |
| 5 | % |
| 6 | % http://www.python.org/doc/current/doc/doc.html |
| 7 | |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 8 | \documentclass{howto} |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 9 | |
| 10 | \title{Python compiler package} |
| 11 | |
| 12 | \author{Jeremy Hylton} |
| 13 | |
| 14 | % Please at least include a long-lived email address; |
| 15 | % the rest is at your discretion. |
| 16 | \authoraddress{ |
| 17 | PythonLabs \\ |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 18 | Zope Corporation \\ |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 19 | Email: \email{jeremy@zope.com} |
| 20 | } |
| 21 | |
| 22 | \date{August 15, 2001} % update before release! |
| 23 | % Use an explicit date so that reformatting |
| 24 | % doesn't cause a new date to be used. Setting |
| 25 | % the date to \today can be used during draft |
| 26 | % stages to make it easier to handle versions. |
| 27 | |
| 28 | \release{2.2} % release version; this is used to define the |
| 29 | % \version macro |
| 30 | |
| 31 | \makeindex % tell \index to actually write the .idx file |
| 32 | \makemodindex % If this contains a lot of module sections. |
| 33 | |
| 34 | |
| 35 | \begin{document} |
| 36 | |
| 37 | \maketitle |
| 38 | |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 39 | \begin{abstract} |
| 40 | |
| 41 | \noindent |
| 42 | The Python compiler package is a tool for analyzing Python source code |
| 43 | and generating Python bytecode. The compiler contains libraries to |
| 44 | generate an abstract syntax tree from Python source code and to |
| 45 | generate Python bytecode from the tree. |
| 46 | |
| 47 | \end{abstract} |
| 48 | |
| 49 | \tableofcontents |
| 50 | |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 51 | |
| 52 | \section{Introduction\label{Introduction}} |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 53 | |
Jeremy Hylton | 241d69c | 2001-08-18 00:24:46 +0000 | [diff] [blame] | 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. |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 60 | |
Jeremy Hylton | 241d69c | 2001-08-18 00:24:46 +0000 | [diff] [blame] | 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. |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 67 | |
Jeremy Hylton | 241d69c | 2001-08-18 00:24:46 +0000 | [diff] [blame] | 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.) |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 71 | |
Jeremy Hylton | 241d69c | 2001-08-18 00:24:46 +0000 | [diff] [blame] | 72 | \subsection{The basic interface} |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 73 | |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 74 | \declaremodule{}{compiler} |
| 75 | |
Jeremy Hylton | 241d69c | 2001-08-18 00:24:46 +0000 | [diff] [blame] | 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. |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 79 | |
| 80 | \begin{funcdesc}{parse}{buf} |
| 81 | Returns an abstract syntax tree for the Python source code in \var{buf}. |
| 82 | The function raises SyntaxError if there is an error in the source |
| 83 | code. The return value is a \class{compiler.ast.Module} instance that |
| 84 | contains the tree. |
| 85 | \end{funcdesc} |
| 86 | |
| 87 | \begin{funcdesc}{parseFile}{path} |
| 88 | Return an abstract syntax tree for the Python source code in the file |
Fred Drake | 42caf3f | 2001-08-15 14:35:13 +0000 | [diff] [blame] | 89 | specified by \var{path}. It is equivalent to |
| 90 | \code{parse(open(\var{path}).read())}. |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 91 | \end{funcdesc} |
| 92 | |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 93 | \begin{funcdesc}{walk}{ast, visitor\optional{, verbose}} |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 94 | Do a pre-order walk over the abstract syntax tree \var{ast}. Call the |
| 95 | appropriate method on the \var{visitor} instance for each node |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 96 | encountered. |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 97 | \end{funcdesc} |
| 98 | |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 99 | \begin{funcdesc}{compile}{path} |
| 100 | Compile the file \var{path} and generate the corresponding \file{.pyc} |
| 101 | file. |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 102 | \end{funcdesc} |
| 103 | |
| 104 | The \module{compiler} package contains the following modules: |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 105 | \refmodule[compiler.ast]{ast}, \module{consts}, \module{future}, |
| 106 | \module{misc}, \module{pyassem}, \module{pycodegen}, \module{symbols}, |
| 107 | \module{transformer}, and \refmodule[compiler.visitor]{visitor}. |
| 108 | |
Jeremy Hylton | 241d69c | 2001-08-18 00:24:46 +0000 | [diff] [blame] | 109 | \subsection{Limitations} |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 110 | |
| 111 | There are some problems with the error checking of the compiler |
| 112 | package. The interpreter detects syntax errors in two distinct |
| 113 | phases. One set of errors is detected by the interpreter's parser, |
| 114 | the other set by the compiler. The compiler package relies on the |
| 115 | interpreter's parser, so it get the first phases of error checking for |
| 116 | free. It implements the second phase itself, and that implement is |
| 117 | incomplete. For example, the compiler package does not raise an error |
| 118 | if a name appears more than once in an argument list: |
| 119 | \code{def f(x, x): ...} |
| 120 | |
Jeremy Hylton | 241d69c | 2001-08-18 00:24:46 +0000 | [diff] [blame] | 121 | A future version of the compiler should fix these problems. |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 122 | |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 123 | \section{Python Abstract Syntax} |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 124 | |
| 125 | The \module{compiler.ast} module defines an abstract syntax for |
| 126 | Python. In the abstract syntax tree, each node represents a syntactic |
| 127 | construct. The root of the tree is \class{Module} object. |
| 128 | |
| 129 | The abstract syntax offers a higher level interface to parsed Python |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 130 | source code. The \ulink{\module{parser}} |
| 131 | {http://www.python.org/doc/current/lib/module-parser.html} |
| 132 | module and the compiler written in C for the Python interpreter use a |
| 133 | concrete syntax tree. The concrete syntax is tied closely to the |
| 134 | grammar description used for the Python parser. Instead of a single |
| 135 | node for a construct, there are often several levels of nested nodes |
| 136 | that are introduced by Python's precedence rules. |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 137 | |
| 138 | The abstract syntax tree is created by the |
| 139 | \module{compiler.transformer} module. The transformer relies on the |
| 140 | builtin Python parser to generate a concrete syntax tree. It |
| 141 | generates an abstract syntax tree from the concrete tree. |
| 142 | |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 143 | The \module{transformer} module was created by Greg |
| 144 | Stein\index{Stein, Greg} and Bill Tutt\index{Tutt, Bill} for an |
| 145 | experimental Python-to-C compiler. The current version contains a |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 146 | number of modifications and improvements, but the basic form of the |
| 147 | abstract syntax and of the transformer are due to Stein and Tutt. |
| 148 | |
Jeremy Hylton | 241d69c | 2001-08-18 00:24:46 +0000 | [diff] [blame] | 149 | \subsection{AST Nodes} |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 150 | |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 151 | \declaremodule{}{compiler.ast} |
| 152 | |
| 153 | The \module{compiler.ast} module is generated from a text file that |
| 154 | describes each node type and its elements. Each node type is |
| 155 | represented as a class that inherits from the abstract base class |
| 156 | \class{compiler.ast.Node} and defines a set of named attributes for |
| 157 | child nodes. |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 158 | |
| 159 | \begin{classdesc}{Node}{} |
| 160 | |
| 161 | The \class{Node} instances are created automatically by the parser |
| 162 | generator. The recommended interface for specific \class{Node} |
| 163 | instances is to use the public attributes to access child nodes. A |
| 164 | public attribute may be bound to a single node or to a sequence of |
| 165 | nodes, depending on the \class{Node} type. For example, the |
| 166 | \member{bases} attribute of the \class{Class} node, is bound to a |
| 167 | list of base class nodes, and the \member{doc} attribute is bound to |
| 168 | a single node. |
| 169 | |
| 170 | Each \class{Node} instance has a \member{lineno} attribute which may |
| 171 | be \code{None}. XXX Not sure what the rules are for which nodes |
| 172 | will have a useful lineno. |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 173 | \end{classdesc} |
| 174 | |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 175 | All \class{Node} objects offer the following methods: |
| 176 | |
| 177 | \begin{methoddesc}{getChildren}{} |
| 178 | Returns a flattened list of the child nodes and objects in the |
| 179 | order they occur. Specifically, the order of the nodes is the |
| 180 | order in which they appear in the Python grammar. Not all of the |
| 181 | children are \class{Node} instances. The names of functions and |
| 182 | classes, for example, are plain strings. |
| 183 | \end{methoddesc} |
| 184 | |
| 185 | \begin{methoddesc}{getChildNodes}{} |
| 186 | Returns a flattened list of the child nodes in the order they |
| 187 | occur. This method is like \method{getChildren()}, except that it |
| 188 | only returns those children that are \class{Node} instances. |
| 189 | \end{methoddesc} |
| 190 | |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 191 | Two examples illustrate the general structure of \class{Node} |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 192 | classes. The \keyword{while} statement is defined by the following |
| 193 | grammar production: |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 194 | |
| 195 | \begin{verbatim} |
| 196 | while_stmt: "while" expression ":" suite |
| 197 | ["else" ":" suite] |
| 198 | \end{verbatim} |
| 199 | |
| 200 | The \class{While} node has three attributes: \member{test}, |
| 201 | \member{body}, and \member{else_}. (If the natural name for an |
| 202 | attribute is also a Python reserved word, it can't be used as an |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 203 | attribute name. An underscore is appended to the word to make it a |
| 204 | legal identifier, hence \member{else_} instead of \keyword{else}.) |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 205 | |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 206 | The \keyword{if} statement is more complicated because it can include |
| 207 | several tests. |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 208 | |
| 209 | \begin{verbatim} |
| 210 | if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] |
| 211 | \end{verbatim} |
| 212 | |
| 213 | The \class{If} node only defines two attributes: \member{tests} and |
| 214 | \member{else_}. The \member{tests} attribute is a sequence of test |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 215 | expression, consequent body pairs. There is one pair for each |
| 216 | \keyword{if}/\keyword{elif} clause. The first element of the pair is |
| 217 | the test expression. The second elements is a \class{Stmt} node that |
| 218 | contains the code to execute if the test is true. |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 219 | |
| 220 | The \method{getChildren()} method of \class{If} returns a flat list of |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 221 | child nodes. If there are three \keyword{if}/\keyword{elif} clauses |
| 222 | and no \keyword{else} clause, then \method{getChildren()} will return |
| 223 | a list of six elements: the first test expression, the first |
| 224 | \class{Stmt}, the second text expression, etc. |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 225 | |
| 226 | The following table lists each of the \class{Node} subclasses defined |
| 227 | in \module{compiler.ast} and each of the public attributes available |
| 228 | on their instances. The values of most of the attributes are |
| 229 | themselves \class{Node} instances or sequences of instances. When the |
| 230 | value is something other than an instance, the type is noted in the |
| 231 | comment. The attributes are listed in the order in which they are |
Fred Drake | 42caf3f | 2001-08-15 14:35:13 +0000 | [diff] [blame] | 232 | returned by \method{getChildren()} and \method{getChildNodes()}. |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 233 | |
| 234 | \input{asttable} |
| 235 | |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 236 | |
Jeremy Hylton | 241d69c | 2001-08-18 00:24:46 +0000 | [diff] [blame] | 237 | \subsection{Assignment nodes} |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 238 | |
| 239 | There is a collection of nodes used to represent assignments. Each |
| 240 | assignment statement in the source code becomes a single |
| 241 | \class{Assign} node in the AST. The \member{nodes} attribute is a |
| 242 | list that contains a node for each assignment target. This is |
| 243 | necessary because assignment can be chained, e.g. \code{a = b = 2}. |
| 244 | Each \class{Node} in the list will be one of the following classes: |
| 245 | \class{AssAttr}, \class{AssList}, \class{AssName}, or |
| 246 | \class{AssTuple}. |
| 247 | |
Jeremy Hylton | 241d69c | 2001-08-18 00:24:46 +0000 | [diff] [blame] | 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}. |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 253 | |
Jeremy Hylton | 241d69c | 2001-08-18 00:24:46 +0000 | [diff] [blame] | 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. |
| 271 | |
| 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} |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 316 | |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 317 | \section{Using Visitors to Walk ASTs} |
| 318 | |
| 319 | \declaremodule{}{compiler.visitor} |
| 320 | |
| 321 | The visitor pattern is ... The \refmodule{compiler} package uses a |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 322 | variant on the visitor pattern that takes advantage of Python's |
| 323 | introspection features to elminiate the need for much of the visitor's |
| 324 | infrastructure. |
| 325 | |
| 326 | The classes being visited do not need to be programmed to accept |
| 327 | visitors. The visitor need only define visit methods for classes it |
| 328 | is specifically interested in; a default visit method can handle the |
| 329 | rest. |
| 330 | |
| 331 | XXX The magic \method{visit()} method for visitors. |
| 332 | |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 333 | \begin{funcdesc}{walk}{tree, visitor\optional{, verbose}} |
| 334 | \end{funcdesc} |
| 335 | |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 336 | \begin{classdesc}{ASTVisitor}{} |
| 337 | |
| 338 | The \class{ASTVisitor} is responsible for walking over the tree in the |
| 339 | correct order. A walk begins with a call to \method{preorder()}. For |
Fred Drake | 42caf3f | 2001-08-15 14:35:13 +0000 | [diff] [blame] | 340 | each node, it checks the \var{visitor} argument to \method{preorder()} |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 341 | for a method named `visitNodeType,' where NodeType is the name of the |
Fred Drake | 42caf3f | 2001-08-15 14:35:13 +0000 | [diff] [blame] | 342 | node's class, e.g. for a \class{While} node a \method{visitWhile()} |
Fred Drake | 4e6a3fe | 2001-08-15 18:48:10 +0000 | [diff] [blame] | 343 | would be called. If the method exists, it is called with the node as |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 344 | its first argument. |
| 345 | |
| 346 | The visitor method for a particular node type can control how child |
| 347 | nodes are visited during the walk. The \class{ASTVisitor} modifies |
| 348 | the visitor argument by adding a visit method to the visitor; this |
| 349 | method can be used to visit a particular child node. If no visitor is |
Fred Drake | 42caf3f | 2001-08-15 14:35:13 +0000 | [diff] [blame] | 350 | found for a particular node type, the \method{default()} method is |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 351 | called. |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 352 | \end{classdesc} |
| 353 | |
| 354 | \class{ASTVisitor} objects have the following methods: |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 355 | |
| 356 | XXX describe extra arguments |
| 357 | |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 358 | \begin{methoddesc}{default}{node\optional{, \moreargs}} |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 359 | \end{methoddesc} |
| 360 | |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 361 | \begin{methoddesc}{dispatch}{node\optional{, \moreargs}} |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 362 | \end{methoddesc} |
| 363 | |
| 364 | \begin{methoddesc}{preorder}{tree, visitor} |
| 365 | \end{methoddesc} |
| 366 | |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 367 | |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 368 | \section{Bytecode Generation} |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 369 | |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 370 | The code generator is a visitor that emits bytecodes. Each visit method |
Fred Drake | 42caf3f | 2001-08-15 14:35:13 +0000 | [diff] [blame] | 371 | can call the \method{emit()} method to emit a new bytecode. The basic |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 372 | code generator is specialized for modules, classes, and functions. An |
| 373 | assembler converts that emitted instructions to the low-level bytecode |
| 374 | format. It handles things like generator of constant lists of code |
| 375 | objects and calculation of jump offsets. |
| 376 | |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 377 | |
Fred Drake | 834a85a | 2001-08-15 17:01:34 +0000 | [diff] [blame] | 378 | \input{compiler.ind} % Index |
Jeremy Hylton | 76f42ac | 2001-08-14 22:04:44 +0000 | [diff] [blame] | 379 | |
| 380 | \end{document} |