4 ms·
Hint: IF, THEN, and ELSE are words that are partially evaluated at compile time. IF remembers its own location and reserves space for a jump; THEN compiles a c
by bitwize 1y ago
Hint: IF, THEN, and ELSE are words that are partially evaluated at compile time.
IF remembers its own location and reserves space for a jump; THEN compiles a conditional jump if NOT true to its location, in the place where IF was, unless there was an intervening ELSE, in which case it will compile such a conditional jump to ELSE's location at IF, and an unconditional jump to then just before ELSE. So a full use might look like:
: TEST > IF ." greater" ELSE ." less or equal" THEN ;
Which is the equivalent of:
void test(x,y) {
if(x > y) {
printf("greater");
} else {
printf("less or equal to");
}
}
THEN works differently in Forth than in, like, BASIC. It marks the end of the whole conditional block, not the start of the consequent.