David Given
David Given

Reputation: 13701

In Forth, what happens if I call DOES> twice?

John Hayes' ANS Forth test suite contains tests that look like this:

: WEIRD: CREATE DOES> 1 + DOES> 2 + ; 
WEIRD: W1
W1

I'm rather at a loss as to exactly what this is supposed to do. The ANS Forth Specification on DOES> is largely impenetrable.

From reading the test suite, it looks like it's expecting the first call to DOES> to modify W1, but then that calling W1 activates the second DOES>. I assume the second one operates on the word defined by the most recent call to CREATE, but that's already been DOES>ified, so I'm not sure what that's supposed to do.

gforth passes the test suite, so the tests do seem to be valid; but my pet Forth interpreter doesn't, and I need to figure out how to make it work...

Upvotes: 7

Views: 352

Answers (1)

Lars Brinkhoff
Lars Brinkhoff

Reputation: 14325

The second call to DOES> also modifies W1.

WEIRD: creates W1 with a runtime action of 1 + DOES> 2 +. The first call to W1 sets the runtime to 2 +.

This is more apparent if you change the code to print something, e.g.

: weird:   create  does> drop ." ONE"  does> drop ." TWO" ; ok
weird: w1 ok
w1 ONE ok
w1 TWO ok
w1 TWO ok
w1 TWO ok

The explanation for this is that DOES> always operates on the latest defined word.

Upvotes: 8

Related Questions