Exercise 5.42: Using find-variable from
Exercise 5.41, rewrite compile-variable and
compile-assignment to output lexical-address instructions. In cases
where find-variable returns not-found (that is, where the
variable is not in the compile-time environment), you should have the code
generators use the evaluator operations, as before, to search for the binding.
(The only place a variable that is not found at compile time can be is in the
global environment, which is part of the run-time environment but is not part
of the compile-time environment. Thus, if you wish, you may have the evaluator operations look
directly in the global environment, which can be obtained with the operation
(op get-global-environment), instead of having them search the whole
run-time environment found in env.) Test the modified compiler on a few
simple cases, such as the nested lambda combination at the beginning of
this section.
练习 5.42:利用练习 5.41 中的 find-variable,改写 compile-variable 和 compile-assignment,使其输出词法地址指令。当 find-variable 返回 not-found(即变量不在编译时环境中)时,代码生成器应与以前一样使用求值器操作来查找绑定。(在编译时找不到的变量只可能出现在全局环境中,全局环境是运行时环境的一部分,但不是编译时环境的一部分。因此,如果愿意,可以让求值器操作直接在全局环境中查找,全局环境可通过操作 (op get-global-environment) 获得,而不必在 env 中保存的整个运行时环境里搜索。)在几个简单的例子上测试修改后的编译器,例如本节开头的嵌套 lambda 组合式。