
Let's Build a Simple Interpreter, Part 1 (2015) - rspivak
https://ruslanspivak.com/lsbasi-part1/
======
euske
From the article: "If you don't know how compilers work, then you don't know
how computers work."

I still don't understand how JIT compilers work, which is probably the
majority of compilers that I'm using today. It seems that there aren't many
resources or established textbooks for this area. I feel we're still left in
the dark in this age.

~~~
chrisseaton
A basic JIT is no different from a normal compiler. Just write output code to
memory rather than to a file, and only compile each function as needed. It
gets more complicated if you want to compete with the performance of V8 or
something of course but the basics are the same.

~~~
exDM69
It's a bit more complex than that. First a bit of clarification on
terminology: it seems like everything that's compiling code at runtime is
called JIT, but I don't like that definition. For a language implementation to
qualify as JIT, it has to have an interpreter, a compiler and passing control
between compiled and interpreted code.

This is where the complexity is, managing when to JIT compile a function and
how to pass control back to the interpreted code. There are so many ways to do
this, from compiling individual functions to doing a tracing JIT like luajit
is.

So, yes, the compiler part of a JIT is "just" a compiler that outputs code to
memory but the complexity is doing it just-in-time. Start executing code in an
interpreter, find a hot path, then compile that and make the compiler emit
instructions that return the control back to the interpreter when executing
the hot path is done.

~~~
johncolanduoni
> For a language implementation to qualify as JIT, it has to have an
> interpreter, a compiler and passing control between compiled and interpreted
> code.

So you wouldn't consider the .NET framework a JIT? It never interprets any
code, just compiles things as they are used.

~~~
redpanda_ua
Wait a second, I thought .NET is interpreted? I mean take C#. Isn't it
compiled to IL and then run on CLR VM? And has JIT to speed it up?

EDIT: spelling

~~~
chrisseaton
The JIT always runs - there's no interpreter, so if your need to run a method
the first thing the VM does is to JIT compile it.

This has some disadvantages - you have to wait for the JIT to run and can't
keep running in the interpreter while it compiles, and some advantages - it's
simpler.

------
kwhitefoot
The problem I always have with this kind of article is that even though they,
like this one, are often well written and clear they skip all discussion of
the choice of language to implement as though this is somehow completely
irrelevant to the task of creating an interpreter.

But it is not. An interpreter for a curly bracket language or Algol descendant
(Algol, C, C++. C#, VB.Net, Pascal) looks quite different from one for a stack
based language (the canonical one being Forth I suppose) and different again
from on that uses S-expressions (Lisp, Scheme, etc.), and so on.

A simple interpreter for a stack based language can be a lot shorter than one
for a block structured language

------
ryanmccullagh
Yes, this is a simple expression parser, I transpiled this tutorial to C
earlier this year [0]. If by some chance you would like to build my C version,
you would need two other libraries that I built too. Compilers is the most
interesting CS topic for me. That and language runtimes. If I could go to grad
school, that's what I would want to research.

I also emulated Python's `raw_input` function to my own C version :)

It was also a chance for me to use `setjmp` to implement exception handling in
C. Take a look at this commit:
[https://github.com/rmccullagh/calculator/commit/6afac5e5924b...](https://github.com/rmccullagh/calculator/commit/6afac5e5924b769e7a9f7a402d4dd86c1baead34#diff-2045016cb90d1e65d71c2407a2570927R43)

[0]
[https://github.com/rmccullagh/calculator](https://github.com/rmccullagh/calculator)

------
nfrmatk
I've been following this tutorial recently and I've enjoyed it quite a bit. I
think I understand grammars a little bit more as a result. If this style is
interesting to you the author also has a series on building a web server from
scratch. [https://ruslanspivak.com/lsbaws-
part1/](https://ruslanspivak.com/lsbaws-part1/)

------
vesche
Neat series. Another book to add to the recommendation list is Game Scripting
Mastery which is a great text for learning to create scripting languages,
compilers, and virtual machines. Don't judge it by the title, it's a gem.
[https://www.amazon.com/Scripting-Mastery-Premier-Press-
Devel...](https://www.amazon.com/Scripting-Mastery-Premier-Press-
Development/dp/1931841578)

------
haches
Java version to play with:

[https://codeboard.io/projects/9288](https://codeboard.io/projects/9288)

It's interesting to see how much "overhead" static typing and language
verbosity generates. But maybe there are also ways to write it more compact in
Java, not sure.

------
kuharich
Previous discussion:
[https://news.ycombinator.com/item?id=9718472](https://news.ycombinator.com/item?id=9718472)

