One of our goals in this chapter is to isolate issues about thinking
procedurally. As a case in point, let us consider that, in evaluating
operator
combinations, the interpreter is itself following a procedure.
To evaluate
an operator combination,
do the following:
Evaluate the
operand expressions
of the combination.
Apply the function that is denoted by
the operator to the arguments that are the values of
the operands.
演算子の組み合わせ
を評価するには、次のようにします:
組み合わせの
オペランド式
を評価する。
演算子が示す関数を、オペランドの値である引数に適用する。
Even this simple rule illustrates some important points about
processes in general. First, observe that the first step dictates
that in order to accomplish the evaluation process for a
combination we must first perform the evaluation process on each
operand of the combination. Thus, the evaluation rule is
recursive in nature;
that is, it includes, as one of its steps, the need to invoke the rule
itself.
Notice how succinctly the idea of recursion can be used to express
what, in the case of a deeply nested combination, would otherwise be
viewed as a rather complicated process. For example, evaluating
requires that the evaluation rule be applied to four different
combinations. We can obtain a picture of this process by
representing the combination in the form of a
tree, as shown in
figure 1.2.
Each combination is represented by a
node with
branches corresponding to the operator and the
operands of the combination stemming from it.
The
terminal nodes (that is, nodes with
no branches stemming from them) represent either operators or numbers.
Viewing evaluation in terms of the tree, we can imagine that the
values of the operands percolate upward, starting from the terminal
nodes and then combining at higher and higher levels. In general, we
shall see that recursion is a very powerful technique for dealing with
hierarchical, treelike objects. In fact, the percolate values
upward form of the evaluation rule is an example of a general kind
of process known as
tree accumulation.
Figure 1.2 Tree representation, showing the value of each subexpression.
Next, observe that the repeated application of the first step brings
us to the point where we need to evaluate, not combinations, but
primitive expressions such as
numerals or names.
We take care of the primitive cases
by stipulating that
the values of numerals are the numbers that they name,
and
the values of
names are the objects associated
with those names in the environment.
数字の値は、それが表す数である。
そして、
名前の値は、環境においてその名前に関連付けられたオブジェクトである。
The key point to
notice is the role of the
environment in determining the meaning of
the
names
in expressions. In an interactive language such as
JavaScript,
it is meaningless to speak of the value of an expression such as
x + 1
without specifying any information about the environment
that would provide a meaning for the
name x.
As we shall see in chapter 3, the general notion of
the environment as providing a context in which evaluation takes place
will play an important role in our understanding of program execution.
注目すべき重要な点は、式の中の
名前
の意味を決定する際の
環境の役割です。
JavaScript
のような対話的言語では、
名前 x
に意味を与える環境に関する情報を指定せずに、
x + 1
のような式の値について語ることは無意味です。
第3章で見るように、評価が行われる文脈を提供するものとしての環境の一般的な概念は、
プログラムの実行を理解する上で重要な役割を果たします。
Notice that the
evaluation rule given above does not handle
declarations.
For instance, evaluating
const x = 3;
does not apply
an equality operator =
to two arguments, one
of which is the value of the
name
x and the other of which is
3, since the purpose of the
declaration
is precisely to associate
x with a value.
(That is,
const x = 3;
is not
a combination.)
上で述べた評価規則は
宣言
を扱わないことに注意してください。
例えば、
const x = 3;
の評価は、
等号演算子 =
を2つの引数に適用するのではありません。一方の引数は
名前
x の値で、もう一方は 3 です。
なぜなら、
宣言
の目的はまさに
x を値に関連付けることだからです。
(つまり、
const x = 3;
は組み合わせではありません。)
The word const
is a
keyword in JavaScript. Keywords carry a
particular meaning, and thus cannot be used as names. A keyword or a
combination of keywords in a statement instructs the JavaScript
interpreter to treat the statement in a special way. Each such
syntactic form has its own evaluation rule. The
various kinds of statements and expressions (each with its associated
evaluation rule) constitute the
syntax of the programming language.