home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.forth      Forth programmers eat a lot of Bratwurst      117,951 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 117,691 of 117,951   
   Gerry Jackson to Gerry Jackson   
   Re: Iterator not using [ELSE]!   
   06 Nov 25 21:05:54   
   
   From: do-not-use@swldwa.uk   
      
   On 04/11/2025 23:15, Gerry Jackson wrote:   
   > On 04/11/2025 21:36, Gerry Jackson wrote:   
   >> : iterator:  ( "name" -- xt ) \ xt of quotation   
   >>     : ]] true begin [: [[   
   >>     save-input ]] scan-words [[ restore-input   
   >>     abort" Error: RESTORE-INPUT failed"   
   >> ;   
   >   
   >   I just realised to my embarassment that this is unduly complicated. Of   
   > course the scanning is not required. All that is needed for iterator: is   
   > simply:   
   >   
      
   The full code for the iterator without the redundant [IF] [ELSE] etc but   
   including the ]] ... [[ postpone definition   
      
   : ]]        \ Single line postponer, avoids POSTPONE clutter   
       begin   
          >in @ parse-name s" [[" compare   
       while   
          >in ! postpone postpone   
       repeat drop   
    immediate   
      
   : iterator:  ( "name" -- )  : ]] true begin [: [[  ;   
      
   : ;iterator  ( -- ) ]] ;] swap while execute repeat drop ; [[  ;  immediate   
      
   \ An example   
   iterator: downcount dup 0< if drop 0 exit then dup . 1- true  ;iterator   
      
   compiled as   
   : downcount true begin [: dup 0< if drop 0 exit then dup . 1- true ;]   
                     swap while execute repeat drop ;   
      
   cr 9 downcount  \ displays 9 8 7 6 5 4 3 2 1 0   
      
   A better example of use is a definition of a multi-line postponer.   
      
   \ ---[ Multi-line postpone ]--------------------------   
      
   : next  postpone true  postpone exit  ; immediate  \ could be continue   
   : done  postpone false postpone exit  ; immediate  \ could be break   
      
   iterator: ]]   
       >in @ parse-name ?dup   
       if s" [[" compare   
          if >in ! postpone postpone next then   
          drop done   
       then   
       2drop refill 0= -39 and throw next   
   iterator immediate   
      
   \ Test   
   : foo   
       ]] over over + [[   
       ]] >r * .   
          r>   
      
          2*   
          .   
       [[   
    immediate   
      
   : bar foo ;   
      
   111 234 bar   \ displays 25974 690   
      
   \ ---[ Compare the gforth compatibility version ]------------   
      
   : refilling-parse-name ( -- old->in c-addr u )   
        begin   
       >in @ parse-name dup 0= while   
           2drop drop refill 0= -39 and throw   
        repeat ;   
      
   : ]] ( -- )   
        \ switch into postpone state   
        begin   
       refilling-parse-name s" [[" compare while   
           >in ! POSTPONE postpone   
        repeat   
        drop ; immediate   
      
   \ -------------------------   
      
   --   
   Gerry   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca