Re: LOOP

Liste des GroupesRevenir à cl forth 
Sujet : Re: LOOP
De : minforth (at) *nospam* gmx.net (minforth)
Groupes : comp.lang.forth
Date : 03. Jul 2025, 20:33:30
Autres entêtes
Organisation : novaBBS
Message-ID : <f404de58a716f2f63cf31477da3014ae@www.novabbs.com>
References : 1 2 3 4 5 6 7 8 9 10
User-Agent : Rocksolid Light
On Sat, 28 Jun 2025 21:01:48 +0000, Anton Ertl wrote:

sean@conman.org writes:
 What is the difference between FOR/NEXT and DO/LOOP?  Don't they do the
same thing?
>
FOR ... NEXT on one system does not do the same thing as FOR ... NEXT
on some other systems, and they all behave different from DO ... LOOP.
>
Correct. Here are variants with iterators that even run on gforth 0.7.9:
\ ====== <n> FOR# .. #TIMES
==================================================
\ original: machine code
\ demo variant: slow Forth
: _ITERATE \ end xt
swap
BEGIN dup 0>
WHILE over execute 1-
REPEAT 2drop ;
: FOR# postpone [: ; IMMEDIATE
: #TIMES postpone ;] postpone _iterate ; IMMEDIATE
\ ====== <n> FOR .. N M .. NEXT
==============================================
\ original: machine code with circular iteration control stack
\   advantage: avoids UNLOOP and doesn't clutter rstack
\ demo variant: slow Forth, indices on rstack like DO..LOOP, no
advantage
\ for gforth:
: rpick rp@ swap 1+ cells + @ ;
: N 2 rpick ; ( -- inner-index )
: M 6 rpick ; ( -- outer-index )
: _N-ITERATE \ end xt --
swap >r 0 >r
BEGIN 2r@ u> \ end n f
WHILE dup execute r> 1+ >r
REPEAT 2r> 2drop drop ;
: FOR postpone [: ; IMMEDIATE
: NEXT postpone ;] postpone _n-iterate ; IMMEDIATE
\ ====== <array> <#el> <elsize> FOR{ .. N M .. }NEXT
=========================
\ original: machine code with circular iteration control stack
\   advantage: avoids UNLOOP and doesn't clutter rstack
\ demo variant: slow Forth, indices on rstack like DO..LOOP, no
advantage
: _ARR-ITERATE \ adr els step xt --
>r dup >r * over + swap 2r> 2swap 2>r \ xt size -- s | r: end arr
BEGIN 2r@ u>
WHILE over execute dup r> + >r
REPEAT 2r> 2drop 2drop ;
: FOR{ postpone [: ; IMMEDIATE
: }NEXT postpone ;] postpone _arr-iterate ; IMMEDIATE
\ --- Tests
: STARS
20 FOR# '*' emit #TIMES ;
: NUMBER1
10 FOR n . NEXT ;
: NUMBER2
3 FOR cr 4 FOR n m 1+ * . NEXT NEXT ;
: TYPE-CHARARRAY \ a u --
dup >r pad swap move
pad r> 1 FOR{ n c@ emit }NEXT ;
CR STARS
CR NUMBER1
CR NUMBER2
CR S" Fortune" TYPE-CHARARRAY
--

Date Sujet#  Auteur
23 Jun 25 * OOS approach revisited23LIT
24 Jun 25 `* Re: OOS approach revisited22dxf
26 Jun 25  `* Re: OOS approach revisited21LIT
27 Jun 25   +* Re: OOS approach revisited19minforth
27 Jun 25   i+* Re: OOS approach revisited14dxf
27 Jun 25   ii`* Re: OOS approach revisited13minforth
27 Jun 25   ii +* Re: OOS approach revisited3LIT
27 Jun 25   ii i`* Re: OOS approach revisited2minforth
28 Jun 25   ii i `- Re: OOS approach revisited1Stephen Pelc
28 Jun 25   ii `* LOOP (was: OOS approach revisited)9Anton Ertl
28 Jun 25   ii  +* Re: LOOP7dxf
28 Jun 25   ii  i`* Re: LOOP6sean
28 Jun 25   ii  i +* Re: LOOP4Anton Ertl
3 Jul 25   ii  i i`* Re: LOOP3minforth
7 Jul 25   ii  i i `* Re: LOOP2Gerry Jackson
7 Jul 25   ii  i i  `- Re: LOOP1minforth
29 Jun 25   ii  i `- Re: LOOP1dxf
28 Jun 25   ii  `- Re: LOOP (was: OOS approach revisited)1Anton Ertl
28 Jun 25   i+* DO..LOOP and stack shuffling (was: OOS approach revisited)3Anton Ertl
3 Jul 25   ii+- Re: DO..LOOP and stack shuffling1dxf
3 Jul 25   ii`- Re: DO..LOOP and stack shuffling1Anton Ertl
30 Jun 25   i`- Re: OOS approach revisited1Hans Bezemer
27 Jun 25   `- Re: OOS approach revisited1dxf

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal