8 ms·
500 Lines or Less – A Python Interpreter Written in Python
- sndean 10y agoPrevious discussion about the book: https://news.ycombinator.com/item?id=12170182 https://news.ycombinator.com/item?id=12170182
- makmanalp 10y agoThis is awesome. Also see her talk at Pycon here: https://www.youtube.com/watch?v=HVUTjQzESeo https://www.youtube.com/watch?v=HVUTjQzESeo
- kazinator 10y agoIn the early 2000's, Teemu Kalvas created "Lisp 500": 500 lines of C (plus a .lisp file that provides a library and goes as far as featuring a compiler, which works by emitting C). I can't find the sources for this. Incomplete, no .lisp file: http://www.s2.org/~chery/projects/lisp500/ http://www.s2.org/~chery/projects/lisp500/ Doesn't connect: http://modeemi.cs.tut.fi/~chery/lisp500/ http://modeemi.cs.tut.fi/~chery/lisp500/ The most accessible source for this currently seems to be a "lisp5000" project in GitHub: https://github.com/jackpal/lisp5000 https://github.com/jackpal/lisp5000 This is a derivative work whose C code has been blown up to 1600+ lines.
- thomasahle 10y agoList is inherently easy to parse though.
- kazinator 10y agoThat is neither here nor there because parsing is a small sub-problem of the overall problem of providing a complete programming language run-time. Lisp500 isn't a "meta-circular" interpreter; it cannot rely on a host Lisp's implementation of things like memory management, I/O streams, hashing, compilation, ...
- morenoh149 10y agonot sure if troll
- lisper 10y agoThis is similar, but in C++: https://github.com/rongarret/Ciel https://github.com/rongarret/Ciel
- kazinator 10y agoIf you implement Arc in it as a subdialect, you get Arc en Ciel. :)
- chj 10y agoNot easy to read.
- erdifr 10y agoThe Wayback Machine seems to have a copy online. [1] 1: https://web.archive.org/web/20040305005602/http://modeemi.cs.tut.fi/~chery/lisp500/ https://web.archive.org/web/20040305005602/http://modeemi.cs...
- nickpsecurity 10y agoLook up femtolisp. It's a fast interpreter that was around 1kloc at one point. Julia is built on it. It was LISP internally last I checked.
- padator 10y agoI count more than 30 000 LOC at https://github.com/JeffBezanson/femtolisp https://github.com/JeffBezanson/femtolisp
- nickpsecurity 10y agoI was going by the original description. No longer true it seems. I'll retract it then. Thanks. :)
- notsorandomname 10y agoShouldn't the title of the article be "500 lines or less - ceval.c written in Python" or "500 lines or less - a python bytecode interpreter written in python" The original one is just misleading.
- alehander42 10y agoIt is an interpreter and that's absolutely correct. They could've even said "A vm in Python" because that is a VM, just without JIT etc. The compiler includes a parser usually, but interpreters in the purest sense of the word interpret the semantics of some structure corresponding to the code.
- the_af 10y agoIt's not an interpreter of the programming language Python, however. It's a VM for Python bytecode, which is not what most people understand as Python, right?
- astrobe_ 10y agoIt's as correct as writing a Kernel and call it an OS. If you use that kind of deception too often, people won't take you seriously any more very quickly.
- pgbovine 10y agobest "well-actually" of the day
- deleted 10y ago[deleted]
- richardwhiuk 10y agoSurely something like, while True: input() would be closer to the mark!
- Ivoah 10y agoWhy not make it work in python 2 and 3? import sys if sys.version_info.major >= 3: raw_input = input eval(raw_input())
- witty_username 10y agoI would write it as input = raw_input. Or, better, just use the six package.
- qwertyuiop924 10y agoThis is really impressive, until you look at, say, SICP's metacircular evaluator, or McCarthy's original implementation of Lisp. Python by no means has the monopoly in elegant implementations of itself.
- dalke 10y agoI'm a bit put off by your combination of praise and dismissiveness when I think neither are warranted. This is a pretty standard stack-based virtual machine. It is not particularly elegant, nor is the underlying language designed to be particularly elegant, much less have a monopoly on such. It's designed more for clarity and practicality. For something on the same lines which is meant to be an elegant stack-based VM, see Forth. The description at http://yosefk.com/blog/my-history-with-forth-stack-machines.html http://yosefk.com/blog/my-history-with-forth-stack-machines.... is frequently cited on HN.
- qwertyuiop924 10y agoI meant it was impressive that it was so simple to implement the python VM in itself, but becomes less impressive when compared to the simplicity and elegance with which you can do similar feats in other languages. I find that point valid.
- dalke 10y agoWhile I don't see what it is that you, who has looked into how other languages are implemented, finds impressive. It looks like a bog-standard stack VM to me. Why do you think it would be harder than this?
- qwertyuiop924 10y agoIt's impressive that it could fit into 500 lines of Python.
- dalke 10y ago
- divs1210 10y agoHere's a metacircular bytecode interpreter in Clojure: https://github.com/divs1210/Impala https://github.com/divs1210/Impala Wrote this for fun a while back.
- _RPM 10y agoThe Python VM is elegant and it's kind of well documented. It really helped me understand stack machines.
- boznz 10y agoBrownie points for writing another Python interpreter in this Python Interpreter :-)
- grondilu 10y agoI'm slightly confused about how it's possible for a language to be written in itself. Say for instance you want to implement "printf". What's stopping you from just using printf?
- chewxy 10y agoWell, for one, you will have to implement the memory/system model for the language. It may or may not have access to the host language's "printf" function. Initially of course, a shortcut way is to leverage as much of the host language's features as possible, then eventually, as you move further into the bootstrap process, you will want to remove reliance on the host language.