为什么所有流行脚本语言的解释器都是用 C 编写的(如果不是用 C 至少不是用 C++)?
我最近问了一个关于从 C++ 切换到 C 来编写解释器以提高速度的问题,我收到了有人的评论,问我到底为什么要切换到 C。
所以我发现我实际上不知道为什么 - 除了 C++ 面向对象系统具有更高的抽象,因此速度较慢。
- 为什么所有流行脚本语言的解释器都是用 C 编写的,而不是用 C++ 编写的?
如果您想告诉我有关解释器不是 C 语言的其他语言,请用 Ruby、Python、Perl 和 PHP 替换此问题中所有出现的流行脚本语言
。
I recently asked a question on switching from C++ to C for writing an interpreter for speed and I got a comment from someone asking why on earth I would switch to C for that.
So I found out that I actually don't know why - except that C++ object oriented system has a much higher abstraction and therefore is slower.
- Why are the interpreters of all popular scripting languages written in C and not in C++?
If you want to tell me about some other language where the interpreter for it isn't in C, please replace all occurences of popular scripting languages
in this question with Ruby, Python, Perl and PHP
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
C 是一种非常古老的语言,因此几乎所有可用的系统都支持 C。因此,对于任何需要到处移植的项目来说,它都是一个不错的选择。
C is a very old language, and is thus supported on pretty much every system available. It is therefore a good choice for any project that needs to be ported everywhere.
Ruby 的历史可以追溯到 1995 年。如果您在 1995 年编写解释器,您有什么选择? Java于同年发布。 (在 v1.0 中速度非常慢,而且在很多方面都非常慢,不值得使用)
C++ 尚未标准化,编译器对它的支持非常。 (它还没有过渡到我们今天使用的“现代 C++”。我认为 STL 也是在这个时候被提议用于标准化的。它实际上并没有被添加到直到几年后才成为标准。即使在添加它之后,1)编译器才迎头赶上,2)人们也花了几年时间才习惯这种通用编程风格。当时,C++ 首先是一种 OOP 语言,在许多情况下,C++ 的这种风格比 C 慢很多。(在现代 C++ 代码中,这种性能差异几乎被消除了,部分是通过更好的编译器,部分是通过更好的编码风格,减少对 OOP 结构的依赖,更多地依赖模板和泛型编程)
Python 于 1991 年开始。Perl 甚至更老(1987 年)
PHP 也是从 1995 年开始,但另外,重要的是,是由一个几乎对编程一无所知的人创建的。 (是的,当然这在许多重要方面塑造了该语言)
您提到的语言是从 C 开始的,因为 C 是当时便携式、面向未来的平台的最佳选择。
虽然我还没有查过这一点,但我愿意打赌,除了 PHP 的情况(它是由无能造成的)之外,其他语言的语言设计者都选择了 C,因为他们*已经知道它了。所以也许教训不是“C 是最好的”,而是“你已经知道的语言是最好的”。
经常选择 C 的还有其他原因:
这些原因并不意味着 C 实际上是编写解释器(或其他任何东西)的优越语言,它们只是解释了导致其他人使用 C 编写的一些动机。
Ruby dates back to 1995. If you were writing an interpreter in 1995, what were your options? Java was released in the same year. (And was painfully slow in v1.0 and in many ways, not really worth using)
C++ was not yet standardized, and compiler support for it was very sketchy. (it had also not yet made the transition to the "modern C++" that we use today. I think the STL was proposed for standardization around this time as well. It didn't actually get added to the standard until years later. And even after it was added, it took several more years for 1) compilers to catch up, and 2) people to get used to this generic programming style. Back then, C++ was an OOP language first and foremost, and in many cases, that style of C++ was quite a bit slower than C. (In modern C++ code, that performance difference is pretty much eliminated, partly through better compilers, and partly through better coding styles, less reliance on OOP constructs and more on templates and generic programming)
Python was started in 1991. Perl is even older (1987)
PHP is from 1995 as well, but additionally, and importantly, was created by a guy who knew virtually nothing of programming. (and yes, of course this has shaped the language in many important ways)
The languages you mention were started in C because C was the best bet for a portable, future-proof platform back then.
And while I haven't looked this up, I'm willing to bet that apart from the PHP case, which is shaped by incompetence more than anything, the language designers of the other languages chose C because they *already knew it. So perhaps the lesson is not "C is best", but "the language you already know is best"
There are other reasons why C is often chosen:
These reasons don't mean that C is in fact a superior language for writing interpreters (or for anything else), they simply explain some of the motivations that have caused others to write in C.
我猜这是因为 C 几乎是唯一一种为几乎所有现有平台提供合理标准编译器的语言。
I'd guess it's because C is pretty much the only language that has a reasonably standard compiler for almost every platform in existence.
我大胆猜测,部分原因是 1998 年 C++ 没有标准化直到 1998 年,实现可移植性变得更加困难。
您列出的所有这些语言都是在标准化之前开发的。
I would hazard a guess that it's in part due to 1998 C++ not being standardized until 1998, making achieving portability that much harder.
All those languages you list were developed before that standardization.
是什么让您认为它们是用 C 编写的?根据我的经验,大多数脚本语言的大部分实现都是用 C 以外的语言编写的。
下面是几个示例:
Ruby
Python
PHP
Perl6
JavaScript
HotSpot JVM 用 C++ 编写,Animorphic Smalltalk VM(HotSpot 和 V8 的衍生版本)用 C++ 编写,Self VM(Animorphic Smalltalk VM 的基础)用 C++ 编写。
有趣的是,在上述许多情况下,不是用 C 编写的实现实际上比用 C 编写的更快。
作为两个实现的示例, 是用 C 编写的,采用 Lua 和 CPython。在这两种情况下,它们实际上都是用非常旧的 C 版本的一个小子集编写的。这样做的原因是它们希望具有高度可移植性。例如,CPython 运行的平台甚至不存在 C++ 编译器。此外,Perl 编写于 1989 年,CPython 编写于 1990 年,Lua 编写于 1993 年,SpiderMonkey 编写于 1995 年。C++ 直到 1998 年才标准化。
What makes you think that they are written in C? In my experience, the majority of implementations for the majority of scripting languages are written in languages other than C.
Here's a couple of examples:
Ruby
Python
PHP
Perl6
JavaScript
The HotSpot JVM is written in C++, the Animorphic Smalltalk VM (from which HotSpot and V8 are derived) is written in C++, the Self VM (on which the Animorphic Smalltalk VM is based) is written in C++.
Interestingly enough, in many of the above cases, the implementations that are not written in C, are actually faster than the ones written in C.
As an example of two implementations that are written in C, take Lua and CPython. In both cases, they are actually written in a small subset of a very old version of C. The reason for this is that they want to be highly portable. CPython, for example, runs on platform for which a C++ compiler doesn't even exist. Also, Perl was written in 1989, CPython in 1990, Lua in 1993, SpiderMonkey in 1995. C++ wasn't standardized until 1998.
与 C 相比,C++ 的复杂性非常高 - 许多人认为它是现有的最复杂且最容易出错的语言之一。
C++ 的许多功能也存在问题——STL 在很多年前就已标准化,但仍然缺乏一种出色的实现。
OOP固然很棒,但在很多场景下它并不能弥补C++的不足。
The complexity of C++ is great compared to that of C - many people consider it one of the most complex and error prone languages in existance.
Many of the features of C++ are problematic as well - the STL was standardized many years ago and it still lacks one great implementation.
OOP is certainly great, but it does not outweigh C++'s deficiencies in many scenarios.
大多数著名的编译器书籍都是用 C 编写的示例。
另外两个主要工具 lexx(构建词法分析器)和 yacc(将语法翻译为 C)也支持 C。
Most known compiler books are written with examples in C.
Also two of the major tools lexx (builds a lexer) and yacc (Translates a grammar to C) have support for C.
如果问题是为什么是 C 而不是 C++,那么答案可以归结为这样一个事实:当您实现脚本语言时,C++ 对象模型就会出现。它的限制如此之大,以至于您将无法将其用于您自己的对象。
因此,您只能将其用于内部结构,并且与更简单的 C 语言相比,您通常无法从 C++ 中获得足够的好处,而 C 语言更容易移植和分发。
在 C 中实现脚本语言时,唯一的问题是缺少协程支持(您必须以某种方式切换堆栈指针),最重要的是,没有办法在没有大量开销的情况下进行异常处理(与 C++ 相比)。
If the question is about why C and not C++ the answer comes down to the fact that when you implement a scripting language the C++ object model comes into your way. Its so restricted that you will not be able to use it for your own objects.
So you can only use this for the internals and they there you usually do not get enough benefits from C++ over the much simpler C language, which makes it easier to port and distribute.
The only problem when implementing a script language in C are missing coroutine support (you have to switch your stack pointer in some way) and most important there is no way to do exception handling without a lot of overhead (compared to C++).