[comp.lang.forth] 0= IF or more simply 0 = IF

rob@idacom.uucp (Rob Chapman) (09/28/90)

: Jan Hofland  ( ? -- a )
   I have been looking through the internals of a forth implemented on the
   Motorola MC68HC11.  There is a primitive in the code that performs a
   branch if the top of stack is non-zero.  Is there a common word for this
   kind of branch?  I am familiar with the conditional branch primitive
   ?BRANCH which branches if top of stack is zero.  I would prefer not to
   make up a name for this conditional out of my imagination if there is a
   name in common usage. ;

 The phrase  0= IF  would be an equivelant in Forth.  Let the optimizers, 0=
 and IF , figure out that they are in sequence and have them lay down the
 6811 code.  If you are using an interpretive (inner) Forth, create a
 headless body (sounds grousome :o,-: ) containing the true-branch code.
 This can then be compiled by the optimizer:

    : IF  ( n -- )  LAST  ' 0=  =
       IF  BACKUP DROP  { true-branch code }  ELSE  { zero-branch code }  ENDIF 
       ,  HERE 0 , ; IMMEDIATE ( -- a )

 The {}s are vaper code which could have the stack comment ( -- body )

 I suppose we might factor this further and write  0 = IF  where = would
 compile 0=:

    : =  ( n \ m -- f )  LAST  ' LIT  =
       IF  BACKUP  DUP  0 =  IF  DROP  ' 0= ,  EXIT  ENDIF  \ LITERAL  ENDIF
       { = CODE } , ; IMMEDIATE ( -- )

 Where:
   BACKUP  ( -- n )  backup dp to the last compile operation and return the
     last value ,ed in the dictionary.  In the case of a literal, dp would
     be moved back 2 cells and the value on the stack would be the literal.
   LAST  ( -- n )  read the previously compiled word.
   {  ( -- )  mark a body of code.  
   }  ( -- body )  demark a body of code and return its execution vector.
   \  ( -- )  compile following word.  Same as [COMPILE] in figForth.

 Possible definitions:
   : BYPASS  ( -- a )  R>  @+ SWAP  >R ;
   : {  ( -- )  ' BYPASS ,  HERE 0 , ; IMMEDIATE ( -- a )
   : }  ( -- a )  ' EXIT ,  HERE SWAP ! ; IMMEDIATE ( a -- )

   : LAST  ( -- n )  old-dp @ @ ;
   : BACKUP  ( -- n )  HERE CELL - @  old-dp @ dp ! ;

   ( : @+  ( a -- n \ a+ )  DUP @  SWAP CELL + ;)

 Just a few ideas.

Rob