Self-hosting interpreters

I wonder... are self-hosted interpreters a thing? Should they be... what is your opinion?

Not typical but certainly technically possible and it has been done a few times.

I would generally advise against it, because unless you build a highly optimizing JIT, performance will basically be unacceptable for anything practical.

1 Like

Why did Perl end up doing this? Like you said, doesn't it cost performance? Why didn't other interpreted languages (Python, pretty much all ECMAScript implementations, etc.) end up doing this?

Stare not into that abyss lest ye go mad.

I suspect it's because it's really, really hard. First, consider that you can't run an interpreter written in an interpreted language. Your CPU can only run native instructions, and (for example) a Python program isn't native. So if your Python interpreter is written in Python, you need a Python interpreter written in native code to run the Python interpreter to run your Python program... so why don't you just run the Python program on the native interpreter in the first place?

The only way to break this cycle is to have an interpreter that is actually a compiler that can spit out native code from the interpreted interpreter code (whether that is ahead-of-time or just-in-time). Except now, arguably, what you have is a compiled language, not an interpreted one.

This is a lot of work to do, and in order to be really worth it, you'd need to generate native code that is better than, say, a C compiler could. And that's even harder.

About the only advantage to doing this is the same as self-hosting for compiled languages: the people who use the language are more likely to be able to contribute to the language itself. But again, all the above is super hard.

The only other example I'm aware of is PyPy, which last I checked was still a bit of a niche thing. The main implementation of Python still remains CPython (written in C).

1 Like

I'm not sure what the actual reasons were, but the only things that come to mind are 1. It ensures a that anyone who understands Perl could, in principle, contribute to the implementation and 2. It can provide an immediate testing ground for new language features.

For all of these languages, performance is a primary concern, and thus self-hosting is simply too expensive.

1 Like

If I made an interpreter, would it generally be a bad idea to do this? I mean... could it harm my performance at some point? Also... what about the Haskell interpreter? Is it written in the Haskell compiler?

PyPy could be considered a self-hosted interpreter for Python, as a JIT compiler written in a subset of Python. But it typically lags behind CPython and is more experimental.

1 Like

Yes, but it's an unnoficial implementation that wouldn't be used in production...

Sorry, when I said Python I was referring to CPython.

LISP did this early on. Some CS courses had students write Scheme interpreters in Scheme.

Would this ever be a good idea (not as a learning thing)?

I doubt it if you care about performance.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.