Types SICP Ch 5:在显式控制求值器中,类型上的分派如何更有效?
我正在通过SICP,我在第5章——实现显式控制求值器 他们首先编写Types SICP Ch 5:在显式控制求值器中,类型上的分派如何更有效?,types,scheme,dispatch,sicp,language-implementation,Types,Scheme,Dispatch,Sicp,Language Implementation,我正在通过SICP,我在第5章——实现显式控制求值器 他们首先编写eval dispatch eval-dispatch (test (op self-evaluating?) (reg exp)) (branch (label ev-self-eval)) (test (op variable?) (reg exp)) (branch (label ev-variable)) (test (op quoted?) (reg exp)) (branch (label ev
eval dispatch
eval-dispatch
(test (op self-evaluating?) (reg exp))
(branch (label ev-self-eval))
(test (op variable?) (reg exp))
(branch (label ev-variable))
(test (op quoted?) (reg exp))
(branch (label ev-quoted))
(test (op assignment?) (reg exp))
(branch (label ev-assignment))
(test (op definition?) (reg exp))
(branch (label ev-definition))
(test (op if?) (reg exp))
(branch (label ev-if))
(test (op lambda?) (reg exp))
(branch (label ev-lambda))
(test (op begin?) (reg exp))
(branch (label ev-begin))
(test (op application?) (reg exp))
(branch (label ev-application))
(goto (label unknown-expression-type))
但是,这也可以以数据导向的方式进行:
在我们的控制器中,调度被写成一系列测试和分支指令。或者,它可以以数据导向的方式编写(在实际系统中可能是这样),以避免需要执行顺序测试,并便于定义新的表达式类型。设计用于运行Lisp的机器可能会包含一条类型分派指令,该指令将有效地执行此类数据定向分派 我不太确定我是否理解他们在这里的含义:
指令是什么意思?我是否正确理解这将是一个op
(assign type (op dispatch-on-type) (reg exp))
(goto type)
我不太清楚为什么以这种方式编写dispatch-on-type
必然会使添加新表达式类型变得更容易
分派
我不太确定我是否理解这一点。有没有什么方法能比
eval dispatch
已经在做的事情更有效?我猜dispatch on type
必须运行测试来确定它是什么类型的好吧,我注意到了一个效率上的差异:
在此版本中,分支
指令仍由机器检查。
如果我们使用数据导向方法,我们本可以避免这些检查。“为了避免执行顺序测试的需要”建议使用一个存储直接跳转指令地址的表,因此参考中的表,并直接跳转到适当的指令,根据问题中引用的代码判断,“指令”可能是通过调用
label
返回的。现在就是这样使用的。因此,现在要执行顺序测试,以确定要计算的是哪种表达式。假设在数据驱动的实现中,每个表达式都将表示为一个标记列表,例如,其类型由标记(带引号的符号、int、enum等)表示;在表中查找该标记,并从中检索跳转地址。