Skip to content

Commit 29515fa

Browse files
committed
Background translated.
1 parent fbe8f83 commit 29515fa

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

src-zh/appendix/background.md

+95
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,51 @@
1+
<!--
12
# Appendix B: Background topics
3+
-->
24

5+
# 附录 B:背景话题
6+
7+
<!--
38
This section covers a numbers of common compiler terms that arise in
49
this guide. We try to give the general definition while providing some
510
Rust-specific context.
11+
-->
12+
13+
本节涵盖了本指南中出现的常见编译器术语。我们会在某些特定于 Rust
14+
的上下文中给出这些术语的一般定义。
615

716
<a name="cfg"></a>
817

18+
<!--
919
## What is a control-flow graph?
20+
-->
21+
22+
## 什么是控制流图?
1023

24+
<!--
1125
A control-flow graph is a common term from compilers. If you've ever
1226
used a flow-chart, then the concept of a control-flow graph will be
1327
pretty familiar to you. It's a representation of your program that
1428
exposes the underlying control flow in a very clear way.
29+
-->
1530

31+
控制流图(control-flow graph)是编译器中常见的术语。如果你曾使用过流程图,
32+
那么控制流图的概念对你来说会很熟悉。它是程序的一种表示方式,
33+
能够以非常清晰的方式展现底层控制流。
34+
35+
<!--
1636
A control-flow graph is structured as a set of **basic blocks**
1737
connected by edges. The key idea of a basic block is that it is a set
1838
of statements that execute "together" – that is, whenever you branch
1939
to a basic block, you start at the first statement and then execute
2040
all the remainder. Only at the end of the block is there the
2141
possibility of branching to more than one place (in MIR, we call that
2242
final statement the **terminator**):
43+
-->
44+
45+
控制流图由以边相连的一组**基本块(basic block)**构成。基本块的主要概念是
46+
一组「一起」执行的语句,也就是说,只要你的分支跳到了基本块,
47+
它就会从头到尾依次执行所有的语句。只有到基本块的最后才有可能分支到更多地方
48+
(在 MIR 中,我们将最后一条语句称作**终止句(terminator)**):
2349

2450
```mir
2551
bb0: {
@@ -31,8 +57,12 @@ bb0: {
3157
}
3258
```
3359

60+
<!--
3461
Many expressions that you are used to in Rust compile down to multiple
3562
basic blocks. For example, consider an if statement:
63+
-->
64+
65+
你在 Rust 中使用的很多表达式都会编译成多个基本块。例如,考虑以下 if 语句:
3666

3767
```rust,ignore
3868
a = 1;
@@ -44,7 +74,11 @@ if some_variable {
4474
d = 1;
4575
```
4676

77+
<!--
4778
This would compile into four basic blocks:
79+
-->
80+
81+
它会被编译成四个基本块
4882

4983
```mir
5084
BB0: {
@@ -68,39 +102,76 @@ BB3: {
68102
}
69103
```
70104

105+
<!--
71106
When using a control-flow graph, a loop simply appears as a cycle in
72107
the graph, and the `break` keyword translates into a path out of that
73108
cycle.
109+
-->
110+
111+
在使用控制流图时,循环会简单地作为一个图中的环路出现,而 `break`
112+
关键字则会被翻译成跳出此环路的一条路径。
74113

75114
<a name="dataflow"></a>
76115

116+
<!--
77117
## What is a dataflow analysis?
118+
-->
78119

120+
## 什么是数据流分析?
121+
122+
<!--
79123
[*Static Program Analysis*](https://cs.au.dk/~amoeller/spa/) by Anders Møller
80124
and Michael I. Schwartzbach is an incredible resource!
125+
-->
126+
127+
[**静态程序分析(Static Program Analysis)**](https://cs.au.dk/~amoeller/spa/)
128+
作者 Anders Møller 和 Michael I. Schwartzbach,它是一个绝佳的资源。
81129

82130
*to be written*
83131

84132
<a name="quantified"></a>
85133

134+
<!--
86135
## What is "universally quantified"? What about "existentially quantified"?
136+
-->
137+
138+
## 什么是「全称量化」?「存在量化」呢?
87139

88140
*to be written*
89141

90142
<a name="variance"></a>
91143

144+
<!--
92145
## What is co- and contra-variance?
146+
-->
147+
148+
## 什么是协变和逆变?
93149

150+
<!--
94151
Check out the subtyping chapter from the
95152
[Rust Nomicon](https://doc.rust-lang.org/nomicon/subtyping.html).
153+
-->
154+
155+
详见 [Rust 秘典](https://doc.rust-lang.org/nomicon/subtyping.html)
156+
中的子定型(Subtyping)一章。
96157

158+
<!--
97159
See the [variance](../variance.html) chapter of this guide for more info on how
98160
the type checker handles variance.
161+
-->
162+
163+
关于类型检查器如何处理型变的更多信息见本指南的
164+
[型变(variance)](../variance.html)一章。
99165

100166
<a name="free-vs-bound"></a>
101167

168+
<!--
102169
## What is a "free region" or a "free variable"? What about "bound region"?
170+
-->
171+
172+
## 什么是「自由生存域」和「自由变量」?「约束生存域」呢?
103173

174+
<!--
104175
Let's describe the concepts of free vs bound in terms of program
105176
variables, since that's the thing we're most familiar with.
106177
@@ -115,14 +186,38 @@ variables, since that's the thing we're most familiar with.
115186
refer to local variables that are defined *outside* of the
116187
expression. We say that those variables **appear free** in the
117188
expression (i.e., they are **free**, not **bound** (tied up)).
189+
-->
118190

191+
我们来描述一下程序变量的自由和约束的概念,因为它们是我们最熟悉的概念。
192+
193+
- 考虑此表达式,它创建了一个闭包:`|a, b| a + b`。在这里,`a + b` 中的 `a`
194+
`b` 指代该闭包被调用会时传入的参数。我们称 `a``b` 在该闭包中是
195+
**被约束(bound)**的,而闭包签名 `|a, b|` 是名字 `a``b`**约束位(binder)**
196+
(因为对 `a``b` 的任何引用都是指代它引入的变量)。
197+
198+
- 考虑此表达式 `a + b`。在该表达式中,`a``b` 均指代定义在该表达式**之外**
199+
的局部变量。我们称这些变量在该表达式中**自由出现(appear free)**
200+
(即它们是**自由(free)**的,而非**被约束(bound)**的(被束缚的))。
201+
202+
<!--
119203
So there you have it: a variable "appears free" in some
120204
expression/statement/whatever if it refers to something defined
121205
outside of that expressions/statement/whatever. Equivalently, we can
122206
then refer to the "free variables" of an expression – which is just
123207
the set of variables that "appear free".
208+
-->
124209

210+
所以现在你理解了:在某些「表达式、语句、还是别的什么」中的变量,
211+
如果指代的是定义在该「表达式、语句、还是别的什么」之外的东西,那么它们就是
212+
「自由出现」的。我们可以等价地称之为表达式中的「自由变量」,
213+
毕竟它们就是一组「自由出现」的变量而已。
214+
215+
<!--
125216
So what does this have to do with regions? Well, we can apply the
126217
analogous concept to type and regions. For example, in the type `&'a
127218
u32`, `'a` appears free. But in the type `for<'a> fn(&'a u32)`, it
128219
does not.
220+
-->
221+
222+
那么,它们与生存域(region)有什么关系呢?我们可以将类似的概念应用到类型和生存域上来。
223+
例如,在类型 `&'a u32` 中,`'a` 是自由出现的。但在类型 `for<'a> fn(&'a u32)` 中则不是。

0 commit comments

Comments
 (0)