Forth: POSTPONE --- how does it work?

John Heyes ANS Forth test suite contains the following definition:

: IFFLOORED [ -3 2 / -2 = INVERT ] LITERAL IF POSTPONE \ THEN ;

This is then used to conditionally define different words depending on whether we use gender or symmetric division:

IFFLOORED : T/MOD  >R S>D R> FM/MOD ;

So it IFFLOOREDacts like noop or \depending on the result of the expression. Good. This is easily implemented in my stream interpreter by doing this:

: POSTPONE ' , ; IMMEDIATE

... and now it IFFLOOREDworks; the definition is equivalent : IFFLOORED -1 IF ['] \ EXECUTE THEN ;.

Unfortunately, the following code is located further in the test suite:

: GT1 123 ;
: GT4 POSTPONE GT1 ; IMMEDIATE
: GT5 GT4 ;
\ assertion here that the stack is empty

. POSTPONE , GT4 : GT4 123 ;... GT4 . , GT5 , 123 , GT5 noop. ; GT5, 123 . , POSTPONE , :

: POSTPONE  ' LITERAL  ['] , LITERAL ;

, , gForth, , POSTPONE :

: GT1 123 ;
: GT4 POSTPONE GT1 ; IMMEDIATE
SEE GT4

<long number> compile, ;

. , ( IFFLOORED \, ). , ( GT4 , push).

... gForth.

, ?

+4
2

, . , , :)

: GT4 POSTPONE GT1 ; IMMEDIATE

, :

  • : , GT4
  • POSTPONE , GT1 - GForth.
  • ; ,
  • IMMEDIATE , .

POSTPONE GT4, . , GT5, POSTPONE .

, , POSTPONE , undefined.

. POSTPONE GForth.

EDIT :

: TEST1 ." interpretation" ;  => ok
: TEST2 ." compilation" ; IMMEDIATE  => ok
: TEST3 TEST1 TEST2 ;  => compilation ok
TEST3  => interpretation ok
: TEST4 POSTPONE TEST1 ; IMMEDIATE  => ok
: TEST5 TEST4 ;  => ok
TEST5  => interpretation ok
: TEST6 POSTPONE TEST2 ; IMMEDIATE  => ok
TEST6  => compilation ok

, .

+3

, :

  • -3/2 ( ) , -2.
  • , 0 (false), -1 (true) IFFLOORED, , , . ( LITERAL.)
  • IFFLOORED IF - THEN. , , , , , \.

, - \ IMMEDIATE, .. , . , , , POSTPONE.

0

Source: https://habr.com/ru/post/1599722/


All Articles