5 ms·
This is an interpreter for a super restricted subset of C and it looks well written from a pedagogical standpoint (keeps thing pretty simple, fairly easy to rea
by WoodenChair 4y ago
This is an interpreter for a super restricted subset of C and it looks well written from a pedagogical standpoint (keeps thing pretty simple, fairly easy to read). But it's slightly awk to strip-down a language (what features do you keep, what do you lose?). I think it's more fun to build an interpreter for an actual tiny language. In my next book I have interpreters for Brainfuck [0], an obfuscated kind of joke of a language, and Tiny BASIC[1] a real tiny language that was used on early personal computers. These are pretty common first projects for folks interested in doing an interpreter.
Here's why real languages are better than stripped down languages:
Anyone with programming knowledge can implement a Brainfuck interpreter in a few hours and run any Brainfuck program. Anyone with a tiny bit of CS knowledge can implement a Tiny BASIC interpreter in just a day and then you can run any real Tiny BASIC program from the late 70s. It's cool to run real programs people actually used. With this stripped down C, there are no pre-made real programs...
0:https://en.wikipedia.org/wiki/Brainfuck https://en.wikipedia.org/wiki/Brainfuck
1:https://en.wikipedia.org/wiki/Tiny_BASIC https://en.wikipedia.org/wiki/Tiny_BASIC
- doodlesdev 4y agoAnother language that's more modern and currently useful but which is very tiny to write an interpreter for is Lua [0][1]. Currently the official Lua interpreter has around 30k LOC which I find pretty amusing for a language used so widely in games and for scripting purposes [2]. Of course it's still at least an order of magnitude larger than a small Tiny BASIC interpreter but the fact it's a current language used in so many places makes it even more interesting to make your for-fun implementation. Also related to small language implementations I find notable PicoC [3] which is a C interpreter written in around 3k LOC of C. Past discussion about it here 13 years ago [4]. [0]: https://www.lua.org/about.html https://www.lua.org/about.html [1]: https://www.lua.org/spe.html https://www.lua.org/spe.html [2]: https://en.wikipedia.org/wiki/Lua_(programming_language) https://en.wikipedia.org/wiki/Lua_(programming_language) [3]: https://gitlab.com/zsaleeba/picoc https://gitlab.com/zsaleeba/picoc [4]: https://news.ycombinator.com/item?id=1658890 https://news.ycombinator.com/item?id=1658890
- benj111 4y agoWhile I appreciate your point. 1. You use the example of a tiny basic of a 'real language' and I don't see how tiny basic is a 'real language', but tiny C is a stripped down language. 2. You can build on this to make a full c implementation. A minimal c implementation that can potentially bootstrap a full c environment is more useful than a brainfuck interpreter.
- WoodenChair 4y ago1. I think I explained why. You can Google real programs people used to do their day to day computing tasks and games and run them unmodified. You can’t do that with this dialect of C. It’s cool to run real programs. 2. Good point, but it’s several orders of magnitude more work to go from this to C, so it depends if you’re doing a one off project or have much bigger ambitions. I think you get more bang for the buck out of Tiny BASIC if it’s a one-off project is my point.
- Gordonjcp 4y agoFORTH is another language that's quick and easy to write from scratch, where you need a couple of dozen words written in assembler and then the rest of FORTH can be written in FORTH.