Replacing `import` with `accio` in cpython

740 views

Published on

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.

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
740
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Replacing `import` with `accio` in cpython

  1. 1. replacing import with accio in CPython
  2. 2. >>> import sys! >>> accio sys! SyntaxError
  3. 3. >>> import sys! >>> accio sys! SyntaxError
  4. 4. >>> import sys! >>> accio sys! SyntaxError
  5. 5. >>> accio sys! >>> import sys! SyntaxError
  6. 6. >>> accio sys! >>> import sys! SyntaxError
  7. 7. >>> accio sys! >>> import sys! SyntaxError
  8. 8. why?
  9. 9. we’ll learn:! what is a statement
  10. 10. we’ll learn:! what is a SyntaxError
  11. 11. we’ll learn:! how does Python know the difference?
  12. 12. we’ll learn:! bootstrapping !
  13. 13. replacing import with accio
  14. 14. import is a statement
  15. 15. statements are defined in the Grammar! file
  16. 16. import_stmt: ! import_name | ! import_from
  17. 17. import_name: ! ‘import’ ! dotted_as_names
  18. 18. first attempt
  19. 19. import_name: ! ‘accio’ ! dotted_as_names
  20. 20. $ make
  21. 21. import sys! ^! SyntaxError: invalid syntax
  22. 22. oops
  23. 23. second attempt
  24. 24. $ sed ’s/ import/accio’
  25. 25. $ make
  26. 26. accio sys! ^! SyntaxError: invalid syntax
  27. 27. bootstrapping !
  28. 28. third attempt
  29. 29. import_name: ! ‘import’ ! dotted_as_names! | ‘accio’ ! dotted_as_names
  30. 30. $ make
  31. 31. $PATH
  32. 32. import_name: ! ‘accio’ ! dotted_as_names
  33. 33. $ sed ’s/ import/accio’
  34. 34. $ make
  35. 35. >>> accio sys! >>> import sys! SyntaxError
  36. 36. >>> accio sys! >>> import sys! SyntaxError
  37. 37. >>> accio sys! >>> import sys! SyntaxError!
  38. 38. 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/
  39. 39. @amygdalama! mathamy.com

×