Hacker News new | past | comments | ask | show | jobs | submit login
Introduction to the Python Interpreter, Part 1: Function Objects (akaptur.github.io)
80 points by luu on Nov 16, 2013 | hide | past | favorite | 15 comments



This seems like it could be a great series, but maybe the author should wait a little next time until there is more content to post.

I am currently reading Engineering a Compiler, and I eventually want to build my own small, interpreted language. Does anyone know of any great resources to supplement that book that is particular to interpreted languages? Is a supplement really needed?


If your goal is to work with an interpreted language, I would leave reading about compilation until later. This is often how it is taught in university courses, the first being learning language theory and implementing interpreted languages, and the second on theory and implementation issues of compiled languages. Others have already suggested SICP and PLAI, and these are great. PLAI is in the manner of what the first course would cover, and SICP does a bit of both. Two more I know of are

Scheme from Scratch http://michaux.ca/articles/scheme-from-scratch-introduction

Write Yourself a Scheme in 48 Hours https://en.wikibooks.org/wiki/Write_Yourself_a_Scheme_in_48_...

There was a good video at PyCon US 2013 this year

So you want to write an interpreter? http://pyvideo.org/video/1694/so-you-want-to-write-an-interp...

It gives a forty minute, high level overview of what is done to implement an interpreted language.


Write yourself a Scheme is pretty magnificent.


I think SICP (http://mitpress.mit.edu/sicp/) is definitely worth a read as far as implementing small languages goes.



This wouldn't be very useful for building a small interpreter.



I you want to see some real Python internals (complete with low-level C gore), take a look at Yaniv Aknin's series starting here - http://tech.blog.aknin.name/2010/04/02/pythons-innards-intro...

... and my own series - http://eli.thegreenplace.net/category/programming/python/pyt..., in particular starting with http://eli.thegreenplace.net/2010/06/30/python-internals-add...


A friend bought this book and it seems pretty great and seems to be exactly what you want.

http://createyourproglang.com


First question is what type of language are you aiming for, and what your goal is.

If you're e.g. aiming for a LISP, or other language with a very small grammar, then you need only very rudimentary understanding of parsing, and so spending lots of time reading about it will be mostly wasted (for the purpose of the interpreter - you'll still get good uses for it).

If you're planning on a language with a bigger grammar, then most compiler texts covers that in excruciating detail, but often overly so for the needs of an interpreter. If you want to build and manipulate an AST in particular (but for the love of all that is good don't aim to interpret the AST - it's horribly slow; if you want to go there, at least generate a simple bytecode, in which case all that compiler reading will pay off).

But if you "just" want something simple, and the LISP or Scheme implementations are not for you, you might be happier with something like Niklaus Wirth's "Compiler Construction" (PDF: http://www.ethoberon.ethz.ch/WirthPubl/CBEAll.pdf ) - note that this covers building a full compiler for Oberon-0, a subset of the already minimal Oberon.

But Wirth's style is a good one to take a look at, because his compilers are extremely small and simple and easy to learn from, and very conservative about what features they use. They won't teach you how to compiler/interpret "fancy" features like closures etc. (Wirth is very much a minimalist) - check out LISP/Scheme and Smalltalk for the fancier stuff (you should) - but Wirth will give you a foundation that is solid for the simpler stuff, and be more applicable to many languages that are more mainstream than the above. And it's quite easy to follow.

The compiler described in the book generates code for a hypothetical RISC architecture, and a virtual machine for it is include in ca 1.5 pages of Oberon (easily transliterated to another language - Oberon is similar to Pascal or Modula, and readable for e.g. C programmers relatively easily). It's a simple way of getting started experimenting, for example by letting you modify the architecture and generated code to experiment with simpler instructions or "superinstructions" etc.. And the Oberon-0 compiler itself is "easily" adapted / extended.



Part 2 has been posted http://akaptur.github.io/blog/2013/11/15/introduction-to-the...

But I still feel like both part 1 and part 2 are far too short; even if read together they provide very little information to digest.


Summary: In Python, functions are objects.

Summary of part 2: Function objects have a code attribute.


What a cliffhanger.


Finish pls.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: