[comp.lang.forth] Forth-83 coded do-loop built on for-next opcode

GEnie@willett.UUCP (ForthNet articles from GEnie) (12/22/89)

Message 15        Thu Dec 21, 1989
R.BERKEY [Robert]            at 05:02 PST
 
 
 To: Dan Miller
 Re: Forth-83 coded do-loop built on for-next opcode


Here's a Forth-83 version which should work on TFORTH.

 ( Block    1 )

 ( Forth-83 do-loop built on Harris for-next opcode )

 : (DO)   ( limit index -- for-index index-offset )
     OVER >R - 1-   R> 1- ;
 : DO   ( runtime:   limit index -- )
    COMPILE (DO)   COMPILE >R   [COMPILE] FOR   ;
    IMMEDIATE
 : LOOP   ( runtime:   --   ;R x1 x2 -- x1 x2 | x1 x2 -- )
    [COMPILE] NEXT   COMPILE R>   COMPILE DROP   ;
    IMMEDIATE

 : I   ( -- x )   COMPILE 2R@   COMPILE - ; IMMEDIATE
 \ An immediate I is non-compliant with the Forth-83 Standard

The immediacy here is due to my ignorance of the chip.  Any assembly-level RTX
programmer should be able fix this.  There's probably several ways to improve
on the speed of the I given here.


 ----------------------------------------------------------------
 ----------------------------------------------------------------


The TFORTH loop implementation helps explain things.  The problem is I needs
to have two different behaviours.  Since both are tied to the for-next opcode,
the options are more limited than on an 8086.

Implementors can either play compiler games, slow down the FOR-NEXT I , or do
what TFORTH did: slow down do-loop.  Or, the simple solution is to use another
name for the loop index with FOR-NEXT.  Who would object to say H , for
example?


----------------------------------



' R@ ALIAS H     \ The simple solution

   \ (Whether or not TFORTH has ALIAS isn't the point.)


----------------------------------

            \ A supports-existing-FOR-I-NEXT-source-code solution



 ( Block    2 )

 ( I compatible with both do-loop and for-next )
 ( For RTX chip with  for-next  opcode )

 VARIABLE I?   \ Contains the current I compile state

 : I
    I? @ IF   [COMPILE] I   ELSE   COMPILE R@   THEN ;
    IMMEDIATE
 \ An immediate I is non-compliant with the Forth-83 Standard
 \ Note that if the previous I is made non-immediate
 \ the [COMPILE] needs to be changed here to COMPILE

 : FOR    I? @   FALSE I? !   [COMPILE] FOR   ; IMMEDIATE
 : DO     I? @   TRUE I? !   [COMPILE] DO   ; IMMEDIATE

 : NEXT   [COMPILE] NEXT   I? ! ; IMMEDIATE
 : LOOP    [COMPILE] LOOP   I? ! ; IMMEDIATE
 : +LOOP    [COMPILE] +LOOP   I? ! ; IMMEDIATE


----------


 ------------
-----
This message came from GEnie via willett through a semi-automated program.
Report problems to: 'uunet!willett!dwp' or 'willett!dwp@gateway.sei.cmu.edu'