I'm reading Starting Forth: 4. Decisions, Decisions... . I can run 42 42 = .
42 42 = ok
42 42 = . -1 ok
Predictably, I get -1
which is two's compliment for true
. However, if I push a 42
on the stack, and I run
42 .s
42 = IF ." foobar " THEN ;
I would expect foobar
to be outputted and it's not. 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. 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
In Forth you can use control structures only inside colon definitions. An if-structure looks like this:
Exactly structured words are compiled word usage only for loops / if / while...until & so on....
Use Gforth documentation only. 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.
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". Some implementations allow interactive use, but it is far from universal.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.