简体   繁体   English

将IF语句与FORTH一起使用会产生“解释仅编译的单词”

[英]USING IF Statement with FORTH produces “Interpreting a compile-only word”

I'm reading Starting Forth: 4. Decisions, Decisions... . 我正在阅读《 起点》:4.决策,决策...。 I can run 42 42 = . 我可以运行42 42 = .

42 42 =  ok
42 42 = . -1  ok

Predictably, I get -1 which is two's compliment for true . 可以预测,我得到-1 ,这是对true 二的补充 However, if I push a 42 on the stack, and I run 但是,如果我将42压入堆栈,然后运行

42 .s
42 = IF ." foobar " THEN ; 

I would expect foobar to be outputted and it's not. 我希望可以输出foobar ,但事实并非如此。 Instead I get 相反,我得到了

    42 .s <1> 42  ok
    42 = IF ." foobar " THEN ;  
:2: Interpreting a compile-only word
    42 = >>>IF<<< ." foobar " THEN ; 
Backtrace:
$7F7539250B30 throw 

What's going on here? 这里发生了什么?

I believe these must be compiled into words, for whatever reasons expressions aren't primitives. 我相信无论出于何种原因表达式都不是原始语言,都必须将它们编译成单词。 I believe this is referenced in the book with, 我相信本书中会提到这一点,

Notice: an IF…THEN statement must be contained within a definition. 注意: IF…THEN语句必须包含在定义中。 You can't just enter these words in “calculator style.” 您不能仅以“计算器样式”输入这些字词。

So it would look like this, 所以看起来像这样

: mycond 42 = IF ." foobar " THEN ;   ok
42 .s <1> 42  ok
mycond foobar  ok
42 mycond foobar  ok

This is again in the gforth docs on Conditional execution 这再次在有关条件执行gforth文档中

In Forth you can use control structures only inside colon definitions. 在Forth中,只能在冒号定义内部使用控制结构。 An if-structure looks like this: if结构如下所示:

Exactly structured words are compiled word usage only for loops / if / while...until & so on.... 完全结构化的单词仅在循环/ if / while ...直到此类情况下才用作编译的单词。

Use Gforth documentation only. 仅使用Gforth文档。 This is the best one. 这是最好的。 I mean to use Gforth ... else you may have difficulties with bad examples or other Forth interpreters specific words not included in ANSI Forth nor gnuforth. 我的意思是使用Gforth ...否则您可能会遇到不好的示例或ANSI Forth或gnuforth中未包含的其他Forth解释器特定单词的问题。

The FORTH standard says that control structures (IF, ELSE, THEN; DO, LOOP; and so on) are for compiled use only, interactive use is an "ambiguous situation". FORTH标准表示控制结构(IF,ELSE,THEN,DO,LOOP等)仅用于编译使用 ,而交互使用则是“模棱两可的情况”。 Some implementations allow interactive use, but it is far from universal. 一些实现允许交互式使用,但是它远非通用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM