9

Possible Duplicate:
How could the first C++ compiler be written in C++?

You probably heard that Microsoft released a new language called TypeScript which is a the typed superset of JavaScript.

The most interesting thing that makes me wonder is the fact that its compiler writen in TypeScript itself.

Call me ignorant but I really couldn't figure out in my head how that is possible. This is just like chicken and egg problem in my head because there is no compiler to compile TypeScript's compiler in the first place. How is it possible to write a compiler of the compiler of a programming language with that language?

tugberk
  • 994
  • 9
  • 19
  • 2
    http://stackoverflow.com/questions/193560/implementing-a-compiler-in-itself – jk. Oct 03 '12 at 15:56
  • 2
    http://stackoverflow.com/questions/13537/bootstrapping-a-language – jk. Oct 03 '12 at 15:57
  • 2
    @jk. Sadly we can't close it as duplicate of those, so I propose we move it to SO and close it as duplicate there. –  Oct 03 '12 at 16:02
  • Sometimes you keep both the compiler source and the compiler object in the repository. E.g. Ocaml keeps its `boot/ocamlc` and [MELT](http://gcc-melt.org/) keeps it `melt/generated/*.cc` files in their svn repository. – Basile Starynkevitch May 01 '15 at 07:56
  • see also: [Why are self-hosting compilers considered a rite of passage for new languages?](http://programmers.stackexchange.com/questions/263651/why-are-self-hosting-compilers-considered-a-rite-of-passage-for-new-languages) – gnat Jan 04 '16 at 10:08

2 Answers2

13

It's called Bootstrapping. You're right that the first version has to be written in another language. But once that's done, you can write successive versions with the previously finished compiler.

Tesserex
  • 3,015
  • 4
  • 26
  • 27
  • Not necessarily - sometimes the compiler is written in the same language on a different platform, and except for the first version of a language anywhere, that's the usual these days. – ddyer Oct 03 '12 at 16:57
  • 4
    @ddyer I took "the first version" as "the first version anywhere". Of course you can trivially use a cross compiler to port an existing interpreter to a platform the language hasn't been implemented for yet. –  Oct 03 '12 at 17:22
  • 8
    It's not true that the very first version hast be written in another language. You have to somehow compile the very first compiler, but you don't have to do that by machine. You can do it by hand. Prof. Wirth, for example, simply chopped up the Oberon compiler into equal sized chunks and handed them out as homework assignments to be translated (into a variant of Fortran, I believe) by his students. So, in the end he *did* end up with a version written in another language, but it wasn't the first version, it was the second. And it wasn't "written" per se, it was translated. – Jörg W Mittag Oct 04 '12 at 01:06
  • That's an awesome story! – Noein Sep 12 '15 at 18:33
  • @JackG I think you can forgive him based on the fact that this was in the mid-80s. The skills you needed as a student and the tools you had at your hands were completely different than today. The kids were probably just happy that they weren't making punchcards. – Brandon Barkley Oct 12 '21 at 14:02
1

A slight variation is where the language itself is interpreted, so the compiler is written in the language itself, runs in the interpreter and compiles itself. These languages (classically, Lisp) have very different runtime representations for compiled and interpreted code, but the compiled form is required to preserve the same semantics.

ddyer
  • 4,060
  • 15
  • 18
  • 3
    Of course, this requires having an interpreter in the first place. While this gets around the letter of the title (it's a pity so many people are so confused about the relationships of languages, compilers and interpreters), it's exactly equivalent once we shed the misconception that a compiler is somehow more of a language implementation than an interpreter. IOW bootstrapping is relevant regardless of whether the language implementations in question are interpreters or compilers. –  Oct 03 '12 at 17:21
  • you could build that interpreter in hardware :) – jwenting Oct 04 '12 at 03:54
  • you could also build that interpreter in software if you're not a masochist :) – Jack G Dec 30 '17 at 21:47