A new DO..LOOP?

22 views
Skip to first unread message

The Beez

unread,
Mar 13, 2026, 9:31:28 AM (7 days ago) Mar 13
to 4tH-compiler
Hi 4tH-ers!

You know we've been struggling with DO..LOOP since eternity. Chuck thought he's rather replace the whole shebang with FOR..NEXT.

For years I've largely avoided DO..LOOP where possible, using BEGIN..REPEAT instead.

But now I've encoded it in a few macros - and it works fine (at first sight)!

Like DO..LOOP it keeps its parameters on the Return stack, but it allows WHILE of EXCEPT to add additional conditions. With the parameters on the Return stack, it still requires UNLOOP to exit it, of course.

Also - you feed the parameters like a BASIC FOR..NEXT loop - that is: inclusive. It also does NOT enter the loop when parameters are out of reach. And you can define a STEP very naturally. In 4tH, you can still use I and J.

:macro countup   1+ begin over over >r >r < while ;
:macro countdown 1- begin over over >r >r > while ;
:macro step      r> + r> repeat rdrop rdrop ;
 
10 30 countup   i . 1 step cr
30 10 countdown i . -1 step cr
10 30 countup   i . i 15 < while 1 step cr

This renders:

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10
10 11 12 13 14 15

Let me know what you think. I'm adding this to the 4pp repository.

Hans Bezemer

p da

unread,
Mar 14, 2026, 9:45:03 AM (6 days ago) Mar 14
to 4th-co...@googlegroups.com
I find It a very interesting alternative to gforth's ?do and -do

I'm confused about the use of while inside the countup loop in your third example, using it shouldn't be unbalanced with the repeat in the step macro?

--
You received this message because you are subscribed to the Google Groups "4tH-compiler" group.
To unsubscribe from this group and stop receiving emails from it, send an email to 4th-compiler...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/4th-compiler/42ea2bae-5c73-4efb-8279-f623aae431e8n%40googlegroups.com.

The Beez

unread,
Mar 14, 2026, 10:08:42 AM (6 days ago) Mar 14
to 4tH-compiler
Posted to comp.lang.forth:

Okay, quick overview of developments - I found the names horrible. Second - the syntax itself didn't translate nicely to ANS-Forth for reasons I explain later.

So - I thought:

[A] It resembles the BASIC syntax - like: "FOR x = 10 TO 30 STEP 1 : NEXT x" So why not go all the way? I mean - it's not like FOR..NEXT is standard;
[B] It also fixed the point that you cannot terminate an additional WHILE the ANS-94 way.

In short - the entire routine has the following advantages:

- It is easier to parse mentally;
- It is consistent for both ascending and descending loops;
- It has all the advantages of ?DO..LOOP + it won't enter faux loops;
- You can add additional WHILE's - no more need for LEAVE;
- Portable, you don't need to meddle with control stacks;
- Lots of Forths allow the continued use of "I" and "J".

So - what does it look like? Just four easy definitions:

: for
  postpone 1+ postpone begin postpone over postpone over
  postpone >r postpone >r postpone <  postpone while ; immediate

: -for
  postpone 1- postpone begin postpone over postpone over
  postpone >r postpone >r postpone >  postpone while ; immediate

: step  postpone r> postpone + postpone r> postpone repeat ; immediate
: next  postpone rdrop postpone rdrop ; immediate


Examples:

: ex1 10 30  for i .  1 step next cr ;  ok
ex1 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 ok
: ex2 30 10 -for i . -1 step next cr ;  ok
ex2 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10
 ok
: ex3 10 30  for i . i 15 < while 1 step then next cr ;  ok
ex3 10 11 12 13 14 15
 ok
: ex4 30 10 for i .  1 step next cr ;   ok
ex4
 ok


This is the ?DO..LOOP equivalent of "ex4" (*). Try it.

: ex5 10 30 ?do i . loop cr ;

Hans Bezemer

(*) About. I can never remember the proper syntax of descending loops. Not even for my own compiler. It's all very confusing.

The Beez

unread,
Mar 14, 2026, 10:16:31 AM (6 days ago) Mar 14
to 4tH-compiler
In 4tH, an extra WHILE (or EXCEPT) doesn't make a BEGIN..REPEAT unbalanced. Between BEGIN and REPEAT (or UNTIL) you can add as many WHILE's as you want - REPEAT will resolve them all. However, in the other article I posted I changed the syntax a bit to accommodate this ANS-Forth quirk - between STEP and NEXT you can add your THEN's.

4tH's REPEAT resolves WHILE's like ENDCASE resolves OF's. The latter generates as many THEN's as needed. I picked up on this on some Forth Dimensions article. I thought it made for a much cleaner syntax, so I adopted it (long ago).

Hans Bezemer
Reply all
Reply to author
Forward
0 new messages