语言是否真的依赖于图书馆? [英] Are languages really dependent on libraries?

查看:121
本文介绍了语言是否真的依赖于图书馆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想知道依赖关系是如何从编程语言到库的管理的。以C#为例。当我开始学习计算时,我会假设(错误地说明)语言本身是独立于最终可用于的类库设计的。也就是说,一组语言关键字(例如, class code>)加上语法和语义是先定义的,可以从语言中使用的库是单独开发的。这些图书馆中的具体课程,我以前认为,不应该对语言的设计有任何影响。



但是这不行,时间。考虑 throw 。 C#编译器确保 throw 之后的表达式解析为异常类型。 异常是图书馆中的一个类,因此它不应该是特别的。它将是一个类作为任何其他,除了C#编译器分配它的特殊语义。这是非常好的,但我的结论是,语言的设计确实取决于类库中特定元素的存在和行为。



此外,我想知道如何这种依赖关系被管理。如果我要设计一个新的编程语言,我将使用什么技术将 throw 的语义映射到特定的类,这个类是 Exception



所以我的问题是两个:




  • 我认为语言设计与基类库的语言设计紧密耦合是正确的?

  • 这些依赖关系是如何从编译器和运行时管理的?使用什么技术?



谢谢。



编辑。感谢那些指出我的第二个问题很模糊的人。我同意。我想要学习的是编译器关于它需要的类型的什么样的参考。例如,它是否通过某种唯一的ID找到类型?发布新版本的编译器或类库时会发生什么?我知道这仍然很模糊,我不期待一个精确的单段答案;相反,对文学或博客文章的指引是非常受欢迎的。

解决方案


我正在尝试学习是编译器关于它需要的类型的什么样的参考。例如,它是否通过某种唯一的ID找到类型?


显然,C#编译器在源代码和元数据中都维护了可用的所有类型的内部数据库;这就是为什么一个编译器被称为编译器 - 它编译了一个关于源和库的数据集。



当C#编译器需要说明的是,检查抛出的表达式是否衍生自System.Exception或与System.Exception相同,它假定在 System 上执行全局命名空间查找,然后它查找异常,找到该类,然后将生成的类信息与表达式推导的类型进行比较。



编译器团队使用这种技术,因为它的工作原理,无论我们是否编译您的源代码,并且 System.Exception 在元数据中,或者如果我们正在编译mscorlib本身和 System.Exception 是源代码。



当然,作为性能优化,编译器实际上有一个列表的已知类型,并提早列出该列表,以便每次都不必花费进行查找。您可以想像,您必须查找内置类型的次数是非常大的。一旦列表被填充,那么可以从列表中读出 System.Exception 的类型信息,而无需执行查找。


当新版本的编译器或类库被释放时会发生什么?


发生的是:一大堆开发人员,测试人员,经理,设计师,作家和教育工作者聚在一起,花了几百万人 - 小时,确保编译器和类库都可以在它们被释放之前工作。



这个问题再次是不可思议的。新的编译器发布会发生什么? 很多工作,这就是发生了什么。


我知道这还是很模糊,我不期待一个精确的单段答案;相反,对文学或博客文章的指点是最受欢迎的。


我写了一篇博客,其中包括C#语言及其编译器。这是 http://ericlippert.com


I've always wondered how the dependencies are managed from a programming language to its libraries. Take for example C#. When I was beginning to learn about computing, I would assume (wrongly as it turns out) that the language itself is designed independently of the class libraries that would eventually become available for it. That is, the set of language keywords (such as for, class or throw) plus the syntax and semantics are defined first, and libraries that can be used from the language are developed separately. The specific classes in those libraries, I used to think, should not have any impact on the design of the language.

But that doesn't work, or not all the time. Consider throw. The C# compiler makes sure that the expression following throw resolves to an exception type. Exception is a class in a library, and as such it should not be special at all. It would be a class as any other, except that the C# compiler assigns it that special semantics. That is very good, but my conclusion is that the design of the language does depend on the existence and behaviour of specific elements in the class libraries.

Additionally, I wonder how this dependency is managed. If I were to design a new programming language, what techniques would I use to map the semantics of throw to the very particular class that is Exception?

So my questions are two:

  • Am I correct in thinking that language design is tightly coupled to that of its base class libraries?
  • How are these dependencies managed from within the compiler and run-time? What techniques are used?

Thank you.

EDIT. Thanks to those who pointed out that my second question is very vague. I agree. What I am trying to learn is what kind of references the compiler stores about the types it needs. For example, does it find the types by some kind of unique id? What happens when a new version of the compiler or the class libraries is released? I am aware that this is still pretty vague, and I don't expect a precise, single-paragraph answer; rather, pointers to literature or blog posts are most welcome.

解决方案

What I am trying to learn is what kind of references the compiler stores about the types it needs. For example, does it find the types by some kind of unique id?

Obviously the C# compiler maintains an internal database of all the types available to it in both source code and metadata; this is why a compiler is called a "compiler" -- it compiles a collection of data about the sources and libraries.

When the C# compiler needs to, say, check whether an expression that is thrown is derived from or identical to System.Exception it pretends to do a global namespace lookup on System, and then it does a lookup on Exception, finds the class, and then compares the resulting class information to the type that was deduced for the expression.

The compiler team uses this technique because that way it works no matter whether we are compiling your source code and System.Exception is in metadata, or if we are compiling mscorlib itself and System.Exception is in source.

Of course as a performance optimization the compiler actually has a list of "known types" and populates that list early so that it does not have to undergo the expense of doing the lookup every time. As you can imagine, the number of times you'd have to look up the built-in types is extremely large. Once the list is populated then the type information for System.Exception can be just read out of the list without having to do the lookup.

What happens when a new version of the compiler or the class libraries is released?

What happens is: a whole bunch of developers, testers, managers, designers, writers and educators get together and spend a few million man-hours making sure that the compiler and the class libraries all work before they're released.

This question is, again, impossibly vague. What has to happen to make a new compiler release? A lot of work, that's what has to happen.

I am aware that this is still pretty vague, and I don't expect a precise, single-paragraph answer; rather, pointers to literature or blog posts are most welcome.

I write a blog about, among other things, the design of the C# language and its compiler. It's at http://ericlippert.com.

这篇关于语言是否真的依赖于图书馆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆