As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center 指导。




8年前关闭。




为什么 Forth 使用 IF 语句 THEN ... 而不是 ENDIF?

我正在实现一个(不符合标准的)Forth 编译器。基本上,关于 IF 语句,Forth 的语法对我来说似乎非常违反直觉。
IF ."Statement is true"
ELSE ."Statement is not true"
THEN ."Printed no matter what;

为什么结束语句是 THEN ?这让我觉得这门语言读起来很奇怪。对于我的编译器,我正在考虑将其更改为 ENDIF 之类的内容,这样读起来更自然。但是,首先向后的 IF-THEN 语句背后的基本原理是什么?

最佳答案

把它想成,“IF 就是这样,做这个,ELSE 做那个……然后 THEN 继续……”

或者更好的是,使用引号(如 FactorRetroForth ,...)在这种情况下,它完全是后缀,没有特殊的编译时词;只是从堆栈中获取地址的常规单词: [ do this ] [ do that ] if[ do this ] when[ do that ] unless 。我个人更喜欢这个。

旁白:引用

这是 how quotations are compiled in RetroForth 。在我自己的 Forth(编译为我自己的 VM)中,我只是添加了一个 QUOTE 指令,将下一个地址插入堆栈并跳过 n 字节。 n 字节预计由 RETURN 指令终止,ifwhenunless 字使用谓词以及前面引用留下的地址;酌情调用。确实非常简单,引用通常为 all kinds of beautiful abstractions away from thinking about the stack 敞开大门。

关于history - 为什么 Forth 使用 IF 语句 THEN ... 而不是 ENDIF?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12539634/

10-10 18:29