Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrapping an interpreter?

We know that a compiler can be written in its own language using a trick known as bootstrapping. My question is whether this trick can be applied to interpreters as well?

In theory the answer is certainly yes, but there is one worry that the interpretation of source code will become more and more inefficient as we go through the iterations. Would that be a serious problem?

I'm bootstrapping a very dynamical system where the programs will be constantly changing, so it rules out a compiler.

Let me spell it out this way:

Let the i's be interpreters.

Let the L's be programming languages.

  • We can write i1 in machine code (lowest level), to interpret L1.
  • We then write i2 in L1, interpreting L2 -- a new language.
  • We then write i3 in L2, interpreting L3 -- another new language.
  • and so on...

We don't need any compiler above, just interpreters. Right?

It could be inefficient. That is my question, and how to overcome it if it is indeed inefficient.

like image 513
Yan King Yin Avatar asked May 05 '26 12:05

Yan King Yin


1 Answers

That doesn't make sense. An interpreter doesn't produce a binary, so can't create something that can run itself standalone. Somewhere, ultimately, you need to have a binary that is the interpreter.

Example of a compiler bootstrapping. Let's say we have two languages A(ssembler) and C. We want to bootstrap a C compiler written in C. But we only have an assembler to start with.

  1. Write basic C compiler in A
  2. Write C compiler in C and compile with earlier compiler written in A
  3. You now have a C compiler which can compile itself, you don't need A or the original compiler any more.

Later runs become just

  1. Compile C program using compiler written in C

Now let's say you have an interpreted language instead, I'll call it Y. The first version can be called Y1, the next Y2 and so on. Let's try to "bootstrap" it.

First off we don't have anything that can interpret Y programs, we need to write a basic interpreter. Let's say we have a C compiler and write a Y1 interpreter in C.

  1. Write Y1 interpreter in C, compile it
  2. Write Y2 interpreter in Y1, run it on Y1 interpreter written in C
  3. Write Y3 interpreter in Y2, run it on Y2 interpreter running on Y1 interpreter... Written in C.

The problem is that you can never escape the stack of interpreters as you never compile a higher level interpreter. So you're always going to need to compile and run that first version interpreter written in C. You can never escape it, which I think is the fundamental point of the compiler bootstrapping process. This is why I say your question does not make sense.

like image 117
blueshift Avatar answered May 07 '26 14:05

blueshift