The following code is for ficl.
\ if you're nesting more than 6 for endfor you're crazy.
create for_next_stack 0 , 0 , 0 , 0 , 0 , 0 ,
\ the stack grows downwards
create for_next_offset for_next_stack 6 cells + ,
: for_next_push ( here-value )
for_next_offset @ 1 cells - for_next_offset !
for_next_offset @ ! ;
: for_next_get ( -- here-value )
for_next_offset @ @ ;
: for_next_drop ( -- )
for_next_offset @ 1 cells + for_next_offset ! ;
: for here for_next_push ; immediate compile-only
: next postpone (branch-final) postpone [ for_next_get here - 4 / , ] ; immediate compile-only
: endfor for_next_drop ; immediate compile-only
\ have some values like 1 2 3 4 5 input on the stack prior to this.
: test for . depth 0= if exit then next endfor ;
: test2
0
for
1 +
dup 1 AND
if
S" odd" type cr .s next
else
S" even" type cr .s next
then
endfor ;
: test3
for
S" BEGIN for 2" type cr
0
for
S" value:" type hex dup . decimal cr
1 + dup 5 <
if next then
endfor
drop
endfor ;