Forth中有条件退出的说法吗?

在 Forth 中,如果栈顶为零,是否有一个常用词来有条件地退出过程(返回)?我正在考虑在递归过程中使用它而不是 IF。

回答

有一个普遍实现的词叫做“?exit”,如果不是零,它就会退出。你将不得不做:

0= ?exit

去得到你想要的。如果你的 Forth 缺少这个,你可以自己定义,但是严格来说需要了解 Forth 的实现细节才能正确实现。然而,在大多数 Forth 上,以下代码将起作用:

   : ?exit if rdrop exit then ;
   : ?exit if r> drop exit then ; ( if "rdrop" is not available )
   : -?exit 0= if rdrop exit then ; ( what you want )

大多数 Forth 实现只有一个值用于每个函数调用,因此这将适用于其中的大多数。

还有一个更便携的版本:

: ?exit postpone if postpone exit postpone then ; immediate
: -?exit postpone 0= postpone if postpone exit postpone then ; immediate

尽管我注意到并非所有 Forth 实现都实现了“推迟”,而是可能使用“[编译]”之类的词。


以上是Forth中有条件退出的说法吗?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>