使用程序进行短路评估 [英] Short circuit evaluation using procedures

查看:123
本文介绍了使用程序进行短路评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一种面向对象语言的编译器。我想把所有的值当作对象,那些值的运算符将被实现为方法。编译器将程序转换为基于堆栈的虚拟机的汇编器。

I am currently developing a compiler for a very limited object oriented language. I want to treat all values as objects and operators on those values will be implemented as methods. The compiler transforms the program into assembler for a stack-based virtual machine.

在编译期间,我将整数文字转换为特殊整数类的对象。算术运算符被实现为该类的方法(使用内联汇编器)。因此 4 + 5 基本上等于 4.add(5)

During compilation I transform integer literals into objects of a special "Integer" class. Arithmetic operators are implemented as methods of that class (using inline assembler). So that 4 + 5 basically equals to 4.add(5).

我现在面临的问题是布尔值的特殊情况。如果存在如果语句:

The problem I am facing right now is the special case for boolean values. If there is an if statement:

if(10 > 5 || 12 < 10)

这将被转换为: 10 .greaterThan(5).or(12.lessThan(10))

显然,这些整数文字也可以调用函数副作用。将这些二进制运算符实现为方法调用在这种情况下产生问题,因为短路评估变得不可能。

Now obviously those integer literals can also be calls to a function with side-effects. Implementing those binary operators as method calls yields a problem in this case as short-circuit evaluation gets impossible.

所以我的问题是:


  1. 短路评估,但仍然把每个值作为一个对象?

  1. How do other languages achieve short-circuit evaluation but still treating every value as an object?

根据维基百科,ALGOL 68使用程序来实现用户定义的短路操作员和程序。 -

According to Wikipedia "ALGOL 68 used "proceduring" to achieve user defined short-circuit operators & procedures." - How does this work?


推荐答案

涉及按名称调用通过需求调用。想法是,的参数不是比较的结果,而是比较本身转换为 thunk ,每当(按名称调用)或第一次(根据需要调用)它被转换为结果值

The usual technique, I believe, involves either call by name or call by need. The idea is that the parameter of or is not the result of the comparison, instead it is the comparison itself converted in to a thunk, which is converted into the result value whenever (in call by name) or the first time (in call by need) it is needed.

将表达式转换为thunk时,基本上是创建一个匿名函数,你可以这样处理编译问题。它涉及将表达式编译成将评估表达式的代码。它还涉及创建一个对象(thunk本身),该对象引用表达式使用的那些局部变量(或其副本),以及表达式的编译代码的指针。对象需要与您的布尔类的接口兼容,以便使用它的代码不必关心它是否有一个真正的布尔或thunk冒充这样。每当有人需要布尔值时,thunk将执行编译的表达式代码并提供结果值。

When converting an expression into a thunk, you are basically creating an anonymous function, and you can treat the compilation problem as such. It involves compiling the expression into code that will evaluate the expression. It also involves creating an object (the thunk itself) which refers to (or contains copies of) those local variables that the expression uses, along with a pointer to the compiled code of the expression. The object needs to be interface compatible with your boolean class so that the code using it does not have to care whether it has a genuine boolean or a thunk posing as such. Whenever someone needs the boolean value, the thunk would execute the compiled expression code and provide the resulting value.

如果通过名称调用足够了,那就是它。对于需要的调用,从缓存评估的结果增加了复杂性。

If call by name is sufficient to you, that's all there is to it. For call by need, there's added complexity from caching the result of the evaluation.

这篇关于使用程序进行短路评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆