FOR..NEXT

12 views
Skip to first unread message

The Beez

unread,
Mar 2, 2023, 1:26:41 PM3/2/23
to 4tH-compiler
Hi 4tH-ers!

Obviously, you've stumbled on the occasional use of FOR..NEXT in Forth programs. So I was contemplating this a bit and decided to try my own. And then I found out it could just as easily be incorporated in a .4pp file instead of being hardcoded. And that proved to be a good plan.

Because - in my naivety I decided to make a solid FOR..NEXT.  One that did *not* loop when there is nothing to do - like "0 FOR SPACE NEXT". And one that actually does 10 iterations when I write: "10 FOR SPACE NEXT".

It proved - I was wrong. The most popular inceptions of the word do *at least* one iteration. And n+1 iterations on top of that. Who gets such crazy ideas?!

We know that one of the weaknesses of DO..LOOP is that it *does* at least one iteration - and ?DO is just an ugly fix for that. And who would have thought something as clear as "10 FOR SPACE NEXT" actually implies 11 spaces to be printed. It's not very intuitive..

So - back to the drawing table. I ended up making TWO implementations - a sane one and a compatible one. As a matter of fact, it's *exactly* the same code - it's just divided differently between FOR and NEXT:

>R BEGIN R@ 0> WHILE R> 1- >R REPEAT RDROP

And you can break it up like this:

>R BEGIN R@ 0> WHILE
R> 1- >R REPEAT RDROP

Or like this:

>R BEGIN
R@ 0> WHILE R> 1- >R REPEAT RDROP

Which (after expansion) results in either this:

>R BEGIN R@ 0> WHILE SPACE R> 1- >R REPEAT RDROP

Or this:

>R BEGIN SPACE R@ 0> WHILE R> 1- >R REPEAT RDROP

And yes, you can use "I" or "WHILE" or "EXCEPT" or "DONE" if you like. It's just a simple BEGIN..WHILE..REPEAT.

Anyways, the compliant version is "efornext.4pp" (after eForth) and the sane version is "fornext.4pp".

Available in SVN. All are fully compatible with the current stable version.

Hans Bezemer


Reply all
Reply to author
Forward
0 new messages