replacing `import` with `accio` in cpython

39
replacing import with accio in CPython

Upload: amy-hanlon

Post on 03-Jul-2015

296 views

Category:

Technology


0 download

DESCRIPTION

Replacing `import` with `accio`: Compiling Pythons with Custom Grammar for the sake of a joke! These are the slides from my 03/25/14 talk at NYC Hack and Tell.

TRANSCRIPT

Page 1: Replacing `import` with `accio` in cpython

replacing import with accio in CPython

Page 2: Replacing `import` with `accio` in cpython

>>> import sys!>>> accio sys!SyntaxError

Page 3: Replacing `import` with `accio` in cpython

>>> import sys!>>> accio sys!SyntaxError

Page 4: Replacing `import` with `accio` in cpython

>>> import sys!>>> accio sys!SyntaxError

Page 5: Replacing `import` with `accio` in cpython

>>> accio sys!>>> import sys!SyntaxError

Page 6: Replacing `import` with `accio` in cpython

>>> accio sys!>>> import sys!SyntaxError

Page 7: Replacing `import` with `accio` in cpython

>>> accio sys!>>> import sys!SyntaxError

Page 8: Replacing `import` with `accio` in cpython

why?

Page 9: Replacing `import` with `accio` in cpython

we’ll learn:!what is a statement

Page 10: Replacing `import` with `accio` in cpython

we’ll learn:!what is a

SyntaxError

Page 11: Replacing `import` with `accio` in cpython

we’ll learn:!how does

Python know the difference?

Page 12: Replacing `import` with `accio` in cpython

we’ll learn:!bootstrapping

!

Page 13: Replacing `import` with `accio` in cpython

replacing import with

accio

Page 14: Replacing `import` with `accio` in cpython

import is a statement

Page 15: Replacing `import` with `accio` in cpython

statements are defined in the Grammar!

file

Page 16: Replacing `import` with `accio` in cpython

import_stmt: ! import_name | ! import_from

Page 17: Replacing `import` with `accio` in cpython

import_name: ! ‘import’ ! dotted_as_names

Page 18: Replacing `import` with `accio` in cpython

first attempt

Page 19: Replacing `import` with `accio` in cpython

import_name: ! ‘accio’ ! dotted_as_names

Page 20: Replacing `import` with `accio` in cpython

$ make

Page 21: Replacing `import` with `accio` in cpython

import sys! ^!SyntaxError: invalid syntax

Page 22: Replacing `import` with `accio` in cpython

oops

Page 23: Replacing `import` with `accio` in cpython

second attempt

Page 24: Replacing `import` with `accio` in cpython

$ sed ’s/import/accio’

Page 25: Replacing `import` with `accio` in cpython

$ make

Page 26: Replacing `import` with `accio` in cpython

accio sys! ^!SyntaxError: invalid syntax

Page 27: Replacing `import` with `accio` in cpython

bootstrapping!

Page 28: Replacing `import` with `accio` in cpython

third attempt

Page 29: Replacing `import` with `accio` in cpython

import_name: ! ‘import’ ! dotted_as_names! | ‘accio’ ! dotted_as_names

Page 30: Replacing `import` with `accio` in cpython

$ make

Page 31: Replacing `import` with `accio` in cpython

$PATH

Page 32: Replacing `import` with `accio` in cpython

import_name: ! ‘accio’ ! dotted_as_names

Page 33: Replacing `import` with `accio` in cpython

$ sed ’s/import/accio’

Page 34: Replacing `import` with `accio` in cpython

$ make

Page 35: Replacing `import` with `accio` in cpython

>>> accio sys!>>> import sys!SyntaxError

Page 36: Replacing `import` with `accio` in cpython

>>> accio sys!>>> import sys!SyntaxError

Page 37: Replacing `import` with `accio` in cpython

>>> accio sys!>>> import sys!SyntaxError!

Page 38: Replacing `import` with `accio` in cpython

links• Grammar - https://docs.python.org/2/reference/

grammar.html

• EBNF - http://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_Form

• Adding a statement - http://eli.thegreenplace.net/2010/06/30/python-internals-adding-a-new-statement-to-python/

Page 39: Replacing `import` with `accio` in cpython

@amygdalama!mathamy.com