Search code examples
kotlinprogramming-languages

How is the Kotlin language written in Kotlin?


I was looking at the Kotlin Github page and I noticed that the Kotlin language itself is mostly written in Kotlin:enter image description here I am just wondering, how is it possible for a language to be mostly written in it's own language? Wouldn't the compiler need to be written (in a different language) before you can even use the language being created?


Solution

  • The process of writing a compiler in its source language is called bootstrapping.

    In fact, at its earliest stage, it involves writing the compiler in another (often a lower-level) programming language, supporting a reasonable subset of the features designed for the compiler's source language.

    Then, using the subset of the features that were implemented in the first step, one can rewrite the compiler's code in the language it compiles. It gives you a compiler of a subset of the language that is written in the same language.

    Afterwards, one can add new features (not using them in the code at first) and build a more powerful compiler each time, and so on iteratively.

    Kotlin used the Java programming language for its initial implementation, then most of the Kotlin compiler's source code got rewritten to Kotlin. Now, most of new code that is added to the Kotlin compiler codebase is written in Kotlin.