Python Compiler Internals Presentation Slides
-
Upload
tom-lee -
Category
Technology
-
view
4.133 -
download
2
Transcript of Python Compiler Internals Presentation Slides
My Contributions to Python
● try/except/finally syntax for 2.5● AST to bytecode compilation from Python● “optimization” at the AST level
Erm ... Before we start ...
A patch to fix a quirk in the build process.
This has been reported on the bug tracker!
An overview of the compiler
Tokenizer Parser
AST Translation
Tokens
Parse Tree
Bytecode Generation Bytecode Optimization
Execution
Symtable ConstructionAST
Bytecode+ Data
AST + Symtable
Semantics of “unless”
Works just like “if not” ...
if not would_consider_letting_you_down: print “never gonna let you down...”
Implementing “unless”
1. Modify the Grammar2. Change the AST definition3. Generate bytecode for “unless”
What does this change affect?
Tokenizer Parser
AST Translation
Tokens
Parse Tree
Bytecode Generation Bytecode Optimization
Execution
Symtable ConstructionAST
Bytecode+ Data
AST + Symtable
What does the parser do?
Organises tokens into the structure defined by the grammar.This is the “parse tree”.
What's affected by this change?
Tokenizer Parser
AST Translation
Tokens
Parse Tree
Bytecode Generation Bytecode Optimization
Execution
Symtable ConstructionAST
Bytecode+ Data
AST + Symtable
AST reuse
... so we could implement “unless” using “if” and “not” AST nodes.
But then I wouldn't be able to show you the code generator ...
Unless(test, body) = If(Not(test), body)
What's affected by this change?
Tokenizer Parser
AST Translation
Tokens
Parse Tree
Bytecode Generation Bytecode Optimization
Execution
Symtable ConstructionAST
Bytecode+ Data
AST + Symtable
Generating bytecode for “unless”
● Add a hook for our new AST node● Generate bytecode implementing “unless” logic● Use basicblocks as labels for jumps
What about the rest of the compiler?
Tokenizer Parser
AST Translation
Tokens
Parse Tree
Bytecode Generation Bytecode Optimization
Execution
Symtable ConstructionAST
Bytecode+ Data
AST + Symtable
Where to go from here?
● Use the Source.● Don't try to grok it all at once.● Don't be afraid to be wrong.● Annoy the python-dev mailing list, like I do!