My UNLOOP is a primitive. It's only nine bytes.
Code: Select all
CODE UNLOOP ( S: -- )
( R: ADR N1 N2 -- )
PLA, PLA,
PLA, PLA,
PLA, PLA,
NEXT JMP,
END-CODE
Following a suggestion from Garth Wilson, my do loops have three loop parameters. ADR is the address that LEAVE or ?LEAVE ( if present) and LOOP or +LOOP branch to.
UNLOOP is from the Ansi Forth standard. Note that it doesn't actually exit the word the loop is in nor does it leave the loop. It just discards the loop parameters. This is so a word can be exited from within a do loop or within a nested do loop. There needs to be an UNLOOP for each level of loop nesting.
Here are two hypothetical examples:
Code: Select all
: SOMEWORD ( -- N1 )
10 0 DO
I DO_SOMETHING_WITH_I
I SOMETEST
IF I UNLOOP EXIT THEN
LOOP
TRUE ;
: ANOTHERWORD ( -- N1 N2 )
10 0 DO
25 0 DO
I J DO_SOMETHING
I J SOME_OTHER_TEST
IF I UNLOOP I UNLOOP EXIT THEN
LOOP
LOOP
TRUE TRUE ;