Skip to content

Implementation details

bjpop edited this page Apr 17, 2013 · 10 revisions

Some notes about Blip's implementation

Compilation algorithm for the whole Python module

  1. Parse the Python source into an AST.
  2. Compute variable scope for the entire module (requires one full pass over the AST).
  3. Recursively compile the module AST into a (possibly nested) code object (requires one full pass over the AST).
  4. Write the code object to a .pyc file with the necessary headers.

Compilation algorithm for a code object (module, class or function)

  1. Lookup the local scope for the entity.
  2. 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.
  3. Convert all jump targets from labels to actual indices.
  4. Compute the maximum stack usage of the bytecode sequence.
  5. Generate a code object for the entity containing the bytecode sequence, plus all the necessary fields, including variable sets (names, varnames, freevars, cellvars etcetera).

Desugaring

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.