r/ProgrammingLanguages • • Jun 07 '26

Steps to a self-hosted compiler?

After having programmed in C for a while, I have finally decided to create my own language, very similar to it, in an attempt to fix it for my personal use.

It is my belief that, with some relatively minor tweaks and changes, C could be transformed into a language I am prepared to sink much time programming for. All of this, of course, is my own opinion; though if you are curious, here is a copy of the current draft for the standard: https://pastebin.com/eq1v7K9t

With the goal being to have a completely self-hosted transpiler (so, written in the language itself), I've been wondering how people usually go about this: do they write a sketchy, barely-working patchwork just to get the language "alive", and then code the true compiler? Or is it more common to do things well the first time, even if it means slower progress?

11 Upvotes

23 comments sorted by

View all comments

Show parent comment

3

u/someOfThisStuff Jun 08 '26

Huh, I didn't know python and swift weren't self-hosted, I imagined large languages like this all were. My language targets C (specifically GNU11) so any output is readily compilable to pretty much everything. As it is, the brunt of the work of making my compiler is all the error checking and type coherency, as there will be no optimization passes (that's GCC's problem).

I like to think that while the languages themselves are not translatable 1:1, the concepts expressed in them are. Because of this, I'm not afraid of performance loss during transcompilation, as any decent C optimizer should be able to produce the same (or equivalent) assembly.

1

u/alkalisun Jun 08 '26

It really depends on the language, usually you don't want to selfhost on a dynamic language or on one where you can't make the same performance optimizations.

There's a lot of tradeoffs here.

1

u/kaplotnikov Jun 08 '26

I would suggest bare minimum to run simple tests first, for example for expressions. You would need to test compiler parts anyway. If the language does not provide an easy way to run tests, this is also an usability issue.

1

u/Inconstant_Moo 🧿 Pipefish Jun 09 '26

I imagined large languages like this all were.

Not all languages are well-suited to writing a compiler. I've seen it argued, in fact, that self-hosting as a goal distorts the langdev project because it makes you design the language for that.

IIRC, Julia uses C++ to interface with LLVM, its runtime is written in C, its parser is written in Scheme, and only its type system is written in Julia.