为什么所有流行脚本语言的解释器都是用 C 编写的(如果不是用 C 至少不是用 C++)?

发布于 2024-08-28 13:37:25 字数 274 浏览 12 评论 0原文

我最近问了一个关于从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

物价感观 2024-09-04 13:37:25

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.

寂寞美少年 2024-09-04 13:37:25

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 程序员。这些语言变得流行的原因之一可能是很容易找到程序员来帮助开发解释器。 C++ 比较复杂,不好学和好用。今天,这可能不是什么大问题,但 10 或 15 年前呢?
  • 互操作性:大多数语言通过 C 接口进行通信。由于您喜欢的新语言将依赖于用其他语言编写的组件(特别是在早期版本中,语言本身受到限制并且几乎没有库),因此调用 C 函数总是很好且简单。不管怎样,如果你有一些 C 代码,那么你可能会很想一直用 C 编写整个代码。
  • 性能:C 不会对你造成太大的阻碍。它不会神奇地使您的代码变得更快,但它可以让您获得良好的性能。当然,C++ 或许多其他语言也是如此。但对于 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:

  • experience and accessibility: C is a simple language that is fairly easy to pick up, lowering the barrier of entry. It's also popular, and there are a lot of experienced C programmers around. One reason why these languages have become popular might just be that it was easy to find programmers to help developing the interpreters. C++ is more complex to learn and use well. Today, that might not be so much of a problem, but 10 or 15 years ago?
  • interoperability: Most languages communicate through C interfaces. Since your fancy new language is going to rely on components written in other languages (especially in early versions when the language itself is limited and has few libraries), it's always nice and simple to call a C function.So since we're going to have some C code anyway, it might be tempting to go all the way and just write the whole thing in C.
  • performance: C doesn't get in your way much. It doesn't magically make your code fast, but it allows you to achieve good performance. So does C++, of course, or many other languages. But it's true for C as well.
  • portability: Practically every platform has a C compiler. Until recently, C++ compilers were much more hit and miss.

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.

千柳 2024-09-04 13:37:25

我猜这是因为 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.

紙鸢 2024-09-04 13:37:25

我大胆猜测,部分原因是 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.

起风了 2024-09-04 13:37:25

为什么所有流行脚本语言的解释器都是用 C 编写的,而不是用 C++ 编写的?

是什么让您认为它们是用 C 编写的?根据我的经验,大多数脚本语言的大部分实现都是用 C 以外的语言编写的。

下面是几个示例:

Ruby

  • BlueRuby:用 ABAP 编写
  • HotRuby:JavaScript
  • Red Sun:ActionScript
  • SmallRuby: Smalltalk/X
  • MagLev:Ruby、GemStone Smalltalk
  • Smalltalk.rb:Smalltalk
  • Alumina:Smalltalk
  • Cardinal:PIR、NQP、PGE
  • RubyGoLightly:Go
  • YARI:Io
  • JRuby:Java
  • XRuby:Java
  • Microsoft IronRuby:C#
  • Wilco Bauwer 的原始 IronRuby:C#
  • Ruby。 NET:C#
  • NETRuby:C#
  • MacRuby:Objective-C
  • Rubinius:Ruby、C++
  • MetaRuby:Ruby
  • RubyVM:Ruby

Python

  • IronPython:C#
  • Jython:Java
  • Pynie:PIR、NQP、PGE
  • PyPy:Python、RPython

PHP

  • P8:Java
  • Quercus:Java
  • Phalanger: C#

Perl6

  • Rakudo:Perl6、PIR、NQP、PGE
  • Pugs:Haskell
  • Sprixel:JavaScript
  • v6.pm:Perl5
  • Elf:CommonLisp

JavaScript

  • Narcissus:JavaScript
  • Ejacs:ELisp
  • Jint:C#
  • IronJS:F#
  • Rhino:Java
  • Mascara(ECMAScript Harmony 参考实现):Python
  • ECMAScript 4 参考实现:标准 ML

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 年才标准化。

Why are the interpreters of all popular scripting languages written in C and not in C++?

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

  • BlueRuby: written in ABAP
  • HotRuby: JavaScript
  • Red Sun: ActionScript
  • SmallRuby: Smalltalk/X
  • MagLev: Ruby, GemStone Smalltalk
  • Smalltalk.rb: Smalltalk
  • Alumina: Smalltalk
  • Cardinal: PIR, NQP, PGE
  • RubyGoLightly: Go
  • YARI: Io
  • JRuby: Java
  • XRuby: Java
  • Microsoft IronRuby: C#
  • the original IronRuby by Wilco Bauwer: C#
  • Ruby.NET: C#
  • NETRuby: C#
  • MacRuby: Objective-C
  • Rubinius: Ruby, C++
  • MetaRuby: Ruby
  • RubyVM: Ruby

Python

  • IronPython: C#
  • Jython: Java
  • Pynie: PIR, NQP, PGE
  • PyPy: Python, RPython

PHP

  • P8: Java
  • Quercus: Java
  • Phalanger: C#

Perl6

  • Rakudo: Perl6, PIR, NQP, PGE
  • Pugs: Haskell
  • Sprixel: JavaScript
  • v6.pm: Perl5
  • Elf: CommonLisp

JavaScript

  • Narcissus: JavaScript
  • Ejacs: ELisp
  • Jint: C#
  • IronJS: F#
  • Rhino: Java
  • Mascara (ECMAScript Harmony Reference Implementation): Python
  • ECMAScript 4 Reference Implementation: Standard ML

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.

白龙吟 2024-09-04 13:37:25

与 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.

逆夏时光 2024-09-04 13:37:25

大多数著名的编译器书籍都是用 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.

坏尐絯℡ 2024-09-04 13:37:25

如果问题是为什么是 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++).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文