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.