[英]Is there a word for a conditional exit in Forth?
在 Forth 中,如果堆棧頂部為零,是否有條件退出過程(返回)的常用詞? 我正在考慮在遞歸過程中使用它而不是 IF。
有一個常用的詞叫做“?exit”,如果不為零,它將退出。 你將不得不做:
0= ?exit
得到你想要的。 如果你的 Forth 缺少這個,你可以自己定義它,但嚴格來說它需要了解 Forth 的實現細節才能正確實現。 然而,在大多數 Forths 上,下面的代碼都可以工作:
: ?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 實現只有一個值用於每個 function 調用,所以這對它們中的大多數都有效。
還有一個更便攜的版本:
: ?exit postpone if postpone exit postpone then ; immediate
: -?exit postpone 0= postpone if postpone exit postpone then ; immediate
雖然我注意到並非所有 Forth 實現都實現了“延遲”,但可能會使用“[compile]”之類的詞。
可移植的實現:
: ?exit ( x -- ) postpone if postpone exit postpone then ; immediate
: 0?exit ( x -- ) postpone 0= postpone ?exit ; immediate
這個實現適用於任何標准的 Forth 系統。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.