craftinginterpreters学习,持续更新

craftingin 精心制作

interpreters 编译器 ,解释器,翻译器,代码解释程序

Fairy tales are more than true: not because they tell us that dragons exist, but because they tell us that dragons can be beaten.
G.K. Chesterton by way of Neil Gaiman, Coraline

神话故事,比现实更现实, 不是因为巨龙存在,而是巨龙可以被战胜。

I’m really excited we’re going on this journey together. This is a book on implementing interpreters for programming languages. It’s also a book on how to design a language worth implementing. It’s the book I wish I’d had when I first started getting into languages, and it’s the book I’ve been writing in my head for nearly a decade.

我很兴奋,就要踏上我们的旅途了。这是一本关于实现程序预言编译器的书。也是一本关于如何设计一个值得实现的语言的书。一本在我刚接触程序开发的时候希望能拥有的书,也是一本在我脑子里写了十年的书。

In these pages, we will walk step-by-step through two complete interpreters for a full-featured language. I assume this is your first foray into languages, so I’ll cover each concept and line of code you need to build a complete, usable, fast language implementation.

在这些书页里,我们会一步步通过两个完备,完整的解释器实现一个功能齐全的语言。我假设你第一次涉足程序语言学,所以,我会cover包含所有概念,每一行代码去创建一个完整,稳定,快速的语言实现。

In order to cram two full implementations inside one book without it turning into a doorstop, this text is lighter on theory than others. As we build each piece of the system, I will introduce the history and concepts behind it. I’ll try to get you familiar with the lingo so that if you ever find yourself at a cocktail party full of PL (programming language) researchers, you’ll fit in.

为了能在一本书里搞定两个完整实现而不变成门槛,这本书会对于理论讲解轻量些,相比其他的书。对于创建的系统的每一部分,我都将讲解下历史和背后的理念。我会让你去熟悉行话,这样你如果参加一个PL(语言沙龙)一类的Party的时候能也融入其中。

作者吐槽参加过很多类似的party,这些写代码的很能喝。

But we’re mostly going to spend our brain juice getting the language up and running. This is not to say theory isn’t important. Being able to reason precisely and formally about syntax and semantics is a vital skill when working on a language. But, personally, I learn best by doing. It’s hard for me to wade through paragraphs full of abstract concepts and really absorb them. But if I’ve coded something, run it, and debugged it, then I get it.

不过,我们主要还是绞尽脑汁去让这些语言能运行上线。不是说理论不重要。能够精确且正式的推理语法和语义是极其重要的技能,当从事语言工作。
不过,个人而言,我学习最快的方法是做。我很难阅读满是抽象概念的段落还能完全理解吸收。不过如果我写代码,运行,调试,我就能学会它。(俺也一样)

Static type systems in particular require rigorous formal reasoning. Hacking on a type system has the same feel as proving a theorem in mathematics.

静态类型系统通常要求严格的公式推理。黑一个类型系统会有种数学定理证明的感觉。

That’s my goal for you. I want you to come away with a solid intuition of how a real language lives and breathes. My hope is that when you read other, more theoretical books later, the concepts there will firmly stick in your mind, adhered to this tangible substrate.

上述就是你的目标。我希望你能对真实语言的运作有一种坚实的直觉。我希望未来你阅读其他理论书籍的时候,这些概念可以牢牢的在你脑海,依附于这些实质的底层内容。

1 . 1Why Learn This Stuff?
为何而学?
Every introduction to every compiler book seems to have this section. I don’t know what it is about programming languages that causes such existential doubt. I don’t think ornithology books worry about justifying their existence. They assume the reader loves birds and start teaching.

似乎所有讲编译器的书都会有这个部分。我不知道为什么会有对学这个有必要性的质疑。我想不会有鸟类学的书会有人质疑其存在价值。书本身假设读者喜欢鸟
就直接教学。

But programming languages are a little different. I suppose it is true that the odds of any of us creating a broadly successful, general-purpose programming language are slim. The designers of the world’s widely used languages could fit in a Volkswagen bus, even without putting the pop-top camper up. If joining that elite group was the only reason to learn languages, it would be hard to justify. Fortunately, it isn’t.

不过程序语言还是有些不同。我猜多数人都创造不出来很成功泛用的程序语言。能设计出世界级别泛用语言的作者估计只能装进一辆大众bus里,甚至不用敞篷。如果只是为了加入这群人去研究语言,那就很难评价学习其必要的合理性。幸运的是,并不是。

Little languages are everywhere
小而美语言很多

For every successful general-purpose language, there are a thousand successful niche ones. We used to call them “little languages”, but inflation in the jargon economy led to the name “domain-specific languages”. These are pidgins tailor-built to a specific task. Think application scripting languages, template engines, markup formats, and configuration files.

每个成功流行的语言,背后都有上千个成功的小语言。我们一般叫他们“小型语言,玩具语言,方言”,但是,行话早就通货膨胀了,我们叫他们领域语言。

太慢了,能力不足,不翻译了,直接看。Git上有翻译好的。