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

标签 forth gforth

在 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 - Forth 中有条件退出的说法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66266092/

相关文章:

forth - 如何实现向量或动态数组?

string - Gforth - 如何获取字符串的代码点?

programming-languages - Forth 还在使用吗?如果是这样,如何以及在哪里?

compilation - 如何为 J1 CPU 编译 Forth 代码?

forth - 与 "parse-word"类似的单词,但用于将整数压入堆栈

forth - 在 Forth 中打印函数的定义

forth - Forth 中大排序数组的快速排序问题

computer-science - 如何在 Forth 中实现 Y-combinator?

macos - 将 JonesForth 移植到 macOS v10.15 (Catalina)