-
Notifications
You must be signed in to change notification settings - Fork 6
Implementation details
bjpop edited this page Apr 17, 2013
·
10 revisions
- Parse the Python source into an AST.
- Compute variable scope for the entire module (requires one full pass over the AST).
- Recursively compile the module AST into a (possibly nested) code object (requires one full pass over the AST).
- Write the code object to a
.pyc
file with the necessary headers.
- Lookup the local scope for the entity.
- Recursively compile the statements and expressions of the entity into an annotated bytecode sequence. In annotated bytecode (integer) labels are used to mark the targets of jump instructions.
- Convert all jump targets from labels to actual indices.
- Compute the maximum stack usage of the bytecode sequence.
- Generate a code object for the entity containing the bytecode sequence, plus all the necessary fields, including variable sets (names, varnames, freevars, cellvars etcetera).
Currently the only syntactic construct that is desugared is comprehensions (lists, sets, dictionaries and generators). These are turned into zero-arity functions with for loops in their bodies.
For example:
[x + y for x in [1,2,3] if x > 1 for y in [4,5,6]]
is desugared to:
$result = []
for x in [1,2,3]:
if x > 1:
for y in [4,5,6]:
$result.append(x + y)
return $result
This is compiled into a code object for a function, and then immediately called.