Our evaluator for Lisp will be implemented as a Lisp program. It may seem
circular to think about evaluating Lisp programs using an evaluator that is
itself implemented in Lisp. However, evaluation is a process, so it is
appropriate to describe the evaluation process using Lisp, which, after all, is
our tool for describing processes.
An evaluator that is written in the same language that it evaluates is said to
be
metacircular.
我们对 Lisp 的求值器将被实现为一个 Lisp 程序。用一个本身以 Lisp 实现的求值器来对 Lisp 程序求值,看起来似乎是循环论证。然而,求值是一个计算过程,因此用 Lisp 来描述求值过程是恰当的——毕竟,Lisp 正是我们用来描述计算过程的工具。用与所求值的语言相同的语言写成的求值器,被称为元循环 (metacircular) 求值器。
The metacircular evaluator is essentially a Scheme formulation of the
environment model of evaluation described in 3.2. Recall that
the model has two basic parts:
元循环求值器本质上是 3.2 节所描述的求值环境模型的 Scheme 表述。回顾一下,该模型由两个基本部分构成:
To evaluate a combination (a compound expression other than a special form),
evaluate the subexpressions and then apply the value of the operator
subexpression to the values of the operand subexpressions.
对组合式(除特殊形式之外的复合表达式)求值时,先对各子表达式求值,再将运算符子表达式的值施用于各运算对象子表达式的值。
To apply a compound procedure to a set of arguments, evaluate the body of the
procedure in a new environment. To construct this environment, extend the
environment part of the procedure object by a frame in which the formal
parameters of the procedure are bound to the arguments to which the procedure
is applied.
将一个复合过程施用于一组实参时,在一个新环境中对该过程的体求值。构造这个新环境的方法是:在过程对象的环境部分上扩展一个框架,该框架将过程的形式参数绑定到过程所施用的实参上。
These two rules describe the essence of the evaluation process, a basic cycle
in which expressions to be evaluated in environments are reduced to procedures
to be applied to arguments, which in turn are reduced to new expressions to be
evaluated in new environments, and so on, until we get down to symbols, whose
values are looked up in the environment, and to primitive procedures, which are
applied directly (see Figure 4.1).
This evaluation cycle will be embodied by the interplay between the two
critical procedures in the evaluator, eval and apply, which are
described in 4.1.1 (see Figure 4.1).
Figure 4.1: The eval-apply cycle exposes the essence of a computer language.
这两条规则描述了求值过程的本质:一个基本的循环——在环境中待求值的表达式被归约为要施用于实参的过程,而这些过程又被归约为要在新环境中求值的新表达式,如此往复,直到归约到符号(其值在环境中查找)和基本过程(直接施用)为止(见图 4.1)。这一求值循环通过求值器中两个核心过程 eval 与 apply 的相互作用得以体现,详见 4.1.1(见图 4.1)。图 4.1:eval-apply 循环揭示了计算机语言的本质。
The implementation of the evaluator will depend upon procedures that define the
求值器的实现将依赖于一组定义待求值表达式之
syntax of the expressions to be evaluated. We will use data
abstraction to make the evaluator independent of the representation of the
language. For example, rather than committing to a choice that an assignment
is to be represented by a list beginning with the symbol set! we use an
abstract predicate assignment? to test for an assignment, and we use
abstract selectors assignment-variable and assignment-value to
access the parts of an assignment. Implementation of expressions will be
described in detail in 4.1.2. There are also operations,
described in 4.1.3, that specify the representation of procedures
and environments. For example, make-procedure constructs compound
procedures, lookup-variable-value accesses the values of variables, and
apply-primitive-procedure applies a primitive procedure to a given list
of arguments.
语法的过程。我们将使用数据抽象,使求值器独立于语言的具体表示。例如,与其规定赋值必须表示为以符号 set! 开头的表,不如使用抽象谓词 assignment? 来检测赋值,并使用抽象选择函数 assignment-variable 和 assignment-value 来访问赋值的各个组成部分。表达式的实现细节将在 4.1.2 中详述。此外,4.1.3 描述了用于指定过程和环境表示的操作——例如,make-procedure 构造复合过程,lookup-variable-value 访问变量的值,apply-primitive-procedure 将一个基本过程施用于给定的实参表。