Bootstrapping a compiler: why?

前端 未结 11 1425
花落未央
花落未央 2020-12-01 02:55

I understand how a language can bootstrap itself, but I haven\'t been able to find much reference on why you should consider bootstrapping.

The int

相关标签:
11条回答
  • 2020-12-01 03:30

    There are two main advantages to bootstrapped language implementations: first, as you suggest, to take advantages of the high-level features of said language in the implementation. However, a less-obvious but no less important advantage is that it lets you customize and extend the language without dropping into a lower layer written in C (or Java, or whatever sits below the new language runtime).

    Metaprogramming may not be useful for most day-to-day tasks, but there are times where it can save you a lot of duplicated or boilerplate code. Being able to hook into the compiler and core runtime for a language at a high level can make advanced metaprogramming tasks much easier.

    0 讨论(0)
  • 2020-12-01 03:32

    It can be considered the bar that separates "toy" languages from "real" languages. If the language isn't rich enough to implement itself, it's still a toy. But this is probably an attitude from a largely bygone era, given the number of popular languages today that are implemented in C.

    0 讨论(0)
  • 2020-12-01 03:35

    Bootstrapping has also another advantage: If your language is nice, you might save time by writing your compiler in <insert language here> than in let's say C. For instance, the C# compiler was written in C++, but now they are rewriting it in C#, which allows them (among other things) to use the threading framework from the CLR instead of rolling their own in C++ (and to follow the lead of the Mono guys as well, marketing wise, Mono was in a better position by being able to say our C# compiler is actually written in C#).

    0 讨论(0)
  • 2020-12-01 03:36

    There's a principle called "eating your own dogfood". By using a tool, you demonstrate the usefulness of the tool.

    It is often asked, "if the compiler for language X isn't written in language X, why should I risk using it?"

    This of course only applies to languages suitable for the domain of compiler writing.

    0 讨论(0)
  • 2020-12-01 03:40

    Low-level languages are often bootstrapped because in order to put code on the new system, you need a low-level compiler there. Get a C compiler over and now you have tons of code available to use. Having a bootstrapped compiler makes this easier, you only require the presence of your own code in order to compile and improve your own code.

    There are other ways to accomplish this, like making a cross-compiler, on most systems you never need to be able to compile static languages on the device itself in ordinary use (in fact, systems like Windows ship with no compiler).

    Another reason compilers often bootstrap is so that they don't have to worry about bugs in the compiler they are compiled with. Ensure your compiler can be compiled with itself and you limit the combinations of bugs that might otherwise appear if you compile with another compiler.

    I think bootstrapping high-level languages is mostly done to show off one's hairy-chested programming skills.

    0 讨论(0)
提交回复
热议问题