2023-10-04 00:59:18 +00:00
|
|
|
( iteration control stacks
|
2023-10-02 01:54:58 +00:00
|
|
|
We create two new stacks - a small stack to hold the "current" value
|
|
|
|
of the loop, or the "i" stack, and a larger stack to hold any extra
|
2023-11-05 03:39:30 +00:00
|
|
|
state, which we call the "next" stack. Typically the top of the next
|
|
|
|
stack would contain an "iterator" that knows how to advance to the next
|
|
|
|
value and how to be cancelled, depending on the needs of the calling code.
|
|
|
|
|
2023-10-02 01:54:58 +00:00
|
|
|
With these two new stacks, we can create a generic loop construct for
|
|
|
|
iterating over streaming values. Not only that, but those values can be
|
|
|
|
arbitrarily filtered and transformed simply by pushing a new value onto
|
|
|
|
the iter-next stack which calls out to the previous one. )
|
|
|
|
|
|
|
|
uservar itop
|
2023-10-04 00:59:18 +00:00
|
|
|
8 cells userallot
|
2023-10-02 01:54:58 +00:00
|
|
|
{ userhere @ } const itop-init
|
|
|
|
uservar nexttop
|
2023-10-04 00:59:18 +00:00
|
|
|
24 cells userallot
|
2023-10-02 01:54:58 +00:00
|
|
|
{ userhere @ } const nexttop-init
|
|
|
|
|
|
|
|
' task-init :chain
|
|
|
|
>r itop-init itop r@ !far
|
|
|
|
nexttop-init nexttop r@ !far <r ;
|
|
|
|
|
|
|
|
taskseg task-init drop
|
|
|
|
|
|
|
|
{ : :peek
|
|
|
|
:ASM ( pixp -- ) >r
|
|
|
|
( i -- v )
|
|
|
|
POP AX
|
|
|
|
SHL AX 1 #
|
|
|
|
MOV BX @[ SS: <r @]
|
|
|
|
ADD BX AX
|
|
|
|
PUSH @[ SS: BX]
|
|
|
|
NEXT ; }
|
|
|
|
|
|
|
|
itop :peek ipeek
|
|
|
|
nexttop :peek nextpeek
|
|
|
|
|
|
|
|
{ : :drop ( pixp -- )
|
|
|
|
:ASM >r
|
|
|
|
MOV BX @[ SS: r@ @]
|
|
|
|
INC BX INC BX
|
|
|
|
MOV @[ SS: <r @] BX
|
2023-10-18 23:39:36 +00:00
|
|
|
NEXT ;
|
|
|
|
: :ndrop ( pixp -- )
|
|
|
|
:ASM >r ( c -- )
|
|
|
|
MOV BX @[ SS: r@ @]
|
|
|
|
POP CX
|
2023-11-05 03:39:30 +00:00
|
|
|
SHL CX 1 #
|
2023-10-18 23:39:36 +00:00
|
|
|
ADD BX CX
|
|
|
|
MOV @[ SS: <r @] BX
|
2023-10-02 01:54:58 +00:00
|
|
|
NEXT ; }
|
|
|
|
|
|
|
|
itop :drop idrop
|
2023-10-18 23:39:36 +00:00
|
|
|
itop :ndrop n-idrop
|
2023-10-02 01:54:58 +00:00
|
|
|
nexttop :drop nextdrop
|
2023-10-18 23:39:36 +00:00
|
|
|
nexttop :ndrop n-nextdrop
|
|
|
|
|
2023-11-05 03:39:30 +00:00
|
|
|
: iterdrop ( n-next -- ) n-nextdrop idrop ;
|
2023-11-05 04:30:34 +00:00
|
|
|
: 1-iterdrop nextdrop idrop ;
|
2023-10-02 01:54:58 +00:00
|
|
|
|
|
|
|
{ : :push ( pixp -- )
|
|
|
|
:ASM >r
|
|
|
|
( v -- )
|
|
|
|
POP AX
|
|
|
|
MOV BX @[ SS: r@ @]
|
|
|
|
DEC BX DEC BX
|
|
|
|
MOV @[ SS: <r @] BX
|
|
|
|
MOV @[ SS: BX] AX
|
|
|
|
NEXT ; }
|
|
|
|
|
|
|
|
itop :push >i
|
|
|
|
nexttop :push >next
|
|
|
|
|
|
|
|
: <i 0 ipeek idrop ;
|
|
|
|
: <next 0 nextpeek nextdrop ;
|
|
|
|
: i 0 ipeek ; : j 1 ipeek ;
|
|
|
|
|
2023-10-18 23:39:36 +00:00
|
|
|
:asm n-<next ( n |n| args... -- args... |n| )
|
2023-11-05 03:39:30 +00:00
|
|
|
MOV AX SS
|
|
|
|
MOV ES AX
|
2023-10-18 23:39:36 +00:00
|
|
|
POP CX
|
|
|
|
JCXZ 2 @>
|
|
|
|
MOV DI @[ SS: nexttop @]
|
|
|
|
( make SP affect the nextstack and DI affect the data stack. )
|
|
|
|
STD ( data stack grows down )
|
|
|
|
XCHG DI SP
|
|
|
|
( PUSH decrements and then stores; STOSW stores then decrements. )
|
|
|
|
SCASW ( pre-decrement )
|
|
|
|
1 :>
|
|
|
|
POP AX
|
|
|
|
STOSW
|
|
|
|
LOOP 1 <@
|
|
|
|
( fix SP - DI is one word past the end of the stack )
|
|
|
|
CLD SCASW XCHG SP DI
|
|
|
|
( update nexttop )
|
|
|
|
MOV @[ SS: nexttop @] DI
|
|
|
|
2 <:
|
|
|
|
NEXT
|
2023-10-02 01:54:58 +00:00
|
|
|
|
2023-10-18 23:39:36 +00:00
|
|
|
:asm n->next ( args... n |n| -- |n| args... )
|
2023-11-05 03:39:30 +00:00
|
|
|
MOV AX SS
|
|
|
|
MOV ES AX
|
2023-10-18 23:39:36 +00:00
|
|
|
POP CX
|
|
|
|
JCXZ 1 @>
|
|
|
|
MOV DI @[ SS: nexttop @]
|
|
|
|
STD ( next-stack grows down )
|
|
|
|
SCASW ( pre-decrement )
|
|
|
|
0 :>
|
|
|
|
POP AX
|
|
|
|
STOSW
|
|
|
|
LOOP 0 <@
|
|
|
|
CLD SCASW ( correct DI - off by one word )
|
|
|
|
MOV @[ SS: nexttop @] DI
|
|
|
|
1 <:
|
|
|
|
NEXT
|
2023-10-02 01:54:58 +00:00
|
|
|
|
2023-11-05 03:39:30 +00:00
|
|
|
( iterators are pointers to an array containing two function pointers:
|
|
|
|
xt-iter xt-cancel
|
|
|
|
The xt-iter word must take care of updating the stacks directly. If
|
2023-10-18 23:39:36 +00:00
|
|
|
there are no more values, it must remove the values from the i-stack,
|
|
|
|
drop itself from the next-stack, and return 0. "finished" and "finish?"
|
|
|
|
are useful words to help with this.
|
|
|
|
|
|
|
|
The xt-cancel word should remove all of the iterator's state from the
|
2023-11-05 03:39:30 +00:00
|
|
|
iteration stacks and return nothing. "n-nextdrop" and "iterdrop" are useful
|
|
|
|
words to help with this. If the iterator is itself making use of an
|
|
|
|
iterator below it on the stack, the xt-cancel word should call "cancel" to
|
|
|
|
recursively clean that up once it's done.
|
2023-10-02 01:54:58 +00:00
|
|
|
|
|
|
|
Note that all "next" words _must_ be defined in the target Forth!
|
|
|
|
This means that any iterator that dereferences near memory, such as "links",
|
|
|
|
WILL NOT WORK on the host Forth! )
|
|
|
|
|
2023-11-05 03:39:30 +00:00
|
|
|
: iterate 0 nextpeek @ execute ;
|
|
|
|
: cancel 0 nextpeek cell + @ execute ;
|
2023-10-18 23:39:36 +00:00
|
|
|
|
2023-11-05 04:30:34 +00:00
|
|
|
: finished ( -- 0 ) cancel 0 ;
|
|
|
|
: finish? ( f -- f ) if 1 else finished then ;
|
|
|
|
|
2023-10-11 02:11:56 +00:00
|
|
|
: EACH_ <r iterate if cell + else @ then >r ;
|
2023-10-02 01:54:58 +00:00
|
|
|
|
|
|
|
{ ( Because we dereference pointers on the return stack, we must run this
|
|
|
|
from the caller's segment. Copy the definition into the host segment. )
|
2023-10-11 02:11:56 +00:00
|
|
|
: EACH_ <r iterate if cell + else @ then >r ;
|
2023-10-09 04:09:40 +00:00
|
|
|
: each ' EACH_ , here >i 0 , ; immediate
|
2023-10-06 15:07:26 +00:00
|
|
|
: continue ' GOTO_ , i cell - , ; immediate
|
|
|
|
: next ['] continue here <i ! ; immediate
|
|
|
|
:timm each t, EACH_ patchpt >i ;
|
|
|
|
: CONTINUE t, GOTO_ i cell - w>t ;
|
2023-10-02 01:54:58 +00:00
|
|
|
:timm continue CONTINUE ;
|
2023-11-05 04:30:34 +00:00
|
|
|
:timm next CONTINUE <i patch!t ;
|
|
|
|
: blankiter, t' finished w>t t' 1-iterdrop w>t ; }
|
2023-11-05 03:39:30 +00:00
|
|
|
|
2023-11-05 04:30:34 +00:00
|
|
|
{ : defiter CREATE blankiter, DOES} >next ;
|
|
|
|
{ : :iter CREATE blankiter, startcolon DOES}
|
|
|
|
>r r@ [ 2 cells lit ] + execute <r >next ;
|
|
|
|
|
|
|
|
{ : iter! ( val off -- ) cells latest entry>tcp + !t ;
|
|
|
|
: next! 2 iter! ; : cancel! 3 iter! ;
|
|
|
|
: :next target next! startcolon ; : :cancel target cancel! startcolon ; }
|
2023-10-02 01:54:58 +00:00
|
|
|
|
2023-11-05 04:30:34 +00:00
|
|
|
defiter >cancel
|
|
|
|
:cancel nextdrop cancel ;
|
2023-11-05 03:39:30 +00:00
|
|
|
|
|
|
|
{ : break ' >cancel , ['] continue ; immediate
|
2023-10-06 15:07:26 +00:00
|
|
|
:timm break t, >cancel CONTINUE ; }
|
2023-10-02 01:54:58 +00:00
|
|
|
|
2023-11-05 04:30:34 +00:00
|
|
|
defiter nothing
|
|
|
|
' nextdrop cancel!
|
2023-11-05 03:39:30 +00:00
|
|
|
|
2023-11-05 04:30:34 +00:00
|
|
|
defiter >single-done
|
|
|
|
:iter single >i ;
|
|
|
|
:next nextdrop 1 >single-done ;
|
2023-11-05 03:39:30 +00:00
|
|
|
|
2023-11-05 04:30:34 +00:00
|
|
|
:iter times ( n -- ) >i ;
|
|
|
|
:next <i dup 1- >i finish? ;
|
2023-11-05 03:39:30 +00:00
|
|
|
|
2023-11-05 04:30:34 +00:00
|
|
|
defiter >links
|
|
|
|
:next <i @ dup >i finish? ;
|
|
|
|
: links ( p -- ) dup if >i >links else nothing then ;
|
2023-11-05 03:39:30 +00:00
|
|
|
|
|
|
|
: +for? ( n -- f ) <i + dup >i 1 nextpeek != finish? ;
|
|
|
|
|
2023-11-05 04:30:34 +00:00
|
|
|
:iter for ( start lim -- ) >next 1- >i ;
|
|
|
|
:next 1 +for? ;
|
|
|
|
:cancel 2 iterdrop ;
|
2023-11-05 03:39:30 +00:00
|
|
|
|
2023-11-05 04:30:34 +00:00
|
|
|
:iter for+ ( start lim inc -- ) >next >next 1 nextpeek - >i ;
|
|
|
|
:next 2 nextpeek +for? ;
|
|
|
|
:cancel 3 iterdrop ;
|
2023-11-05 03:39:30 +00:00
|
|
|
|
2023-11-05 04:30:34 +00:00
|
|
|
:iter pchars ( st -- ) 1- >i ;
|
|
|
|
:next <i 1+ dup >i b@ finish? ;
|
2023-10-02 01:54:58 +00:00
|
|
|
|
2023-10-15 20:54:40 +00:00
|
|
|
: nth ( i -- v ) 0 each 2dup = if drop i break then 1+ next swap drop ;
|
|
|
|
: count 0 each 1+ next ;
|
|
|
|
|
2023-11-05 03:39:30 +00:00
|
|
|
:asm _suspend>args
|
|
|
|
( iter |r| yieldpoint -- argcount |r| yieldpoint iter )
|
|
|
|
MOV DI @[ -2 @+ BP]
|
|
|
|
POP @[ BP]
|
|
|
|
INC BP INC BP
|
2023-10-18 23:39:36 +00:00
|
|
|
XOR AH AH
|
2023-11-05 03:39:30 +00:00
|
|
|
MOV AL @[ DS: DI]
|
2023-10-18 23:39:36 +00:00
|
|
|
PUSH AX
|
2023-11-05 03:39:30 +00:00
|
|
|
NEXT
|
|
|
|
|
|
|
|
:asm 2r>next
|
2023-10-18 23:39:36 +00:00
|
|
|
MOV BX @[ SS: nexttop @]
|
2023-11-05 03:39:30 +00:00
|
|
|
( bp grows up. top: BP-2, second: BP-4
|
|
|
|
nexttop grows down. top: nexttop, second: nexttop+2 )
|
|
|
|
SUB BX 4 # ( make room for 2 more items on next stack )
|
|
|
|
MOV AX @[ -4 @+ BP]
|
|
|
|
MOV @[ SS: 2 @+ BX] AX
|
|
|
|
MOV AX @[ -2 @+ BP]
|
|
|
|
MOV @[ SS: BX] AX
|
|
|
|
SUB BP 4 # ( pop two off return stack )
|
2023-10-18 23:39:36 +00:00
|
|
|
MOV @[ SS: nexttop @] BX
|
|
|
|
NEXT
|
2023-10-12 02:21:56 +00:00
|
|
|
|
2023-10-18 23:39:36 +00:00
|
|
|
:asm _resume>args ( |n| yieldpoint xt-next -- argcount |n| |r| resumepoint )
|
|
|
|
MOV BX @[ SS: nexttop @]
|
|
|
|
MOV DI @[ SS: 2 @+ BX]
|
|
|
|
ADD BX 4 #
|
|
|
|
MOV @[ SS: nexttop @] BX
|
|
|
|
XOR AH AH
|
|
|
|
MOV AL @[ DS: DI]
|
|
|
|
INC DI
|
|
|
|
PUSH AX ( argcount )
|
2023-11-05 03:39:30 +00:00
|
|
|
MOV @[ BP] DI
|
2023-10-11 02:11:56 +00:00
|
|
|
INC BP INC BP
|
|
|
|
NEXT
|
2023-10-12 02:21:56 +00:00
|
|
|
|
2023-10-18 23:39:36 +00:00
|
|
|
: _resume _resume>args n-<next rswap ;
|
2023-11-05 03:39:30 +00:00
|
|
|
: _suspend
|
|
|
|
( args... iter |r| yieldpoint ret -- 1 |r| ret |n| args... yieldpoint iter )
|
|
|
|
rswap _suspend>args n->next 2r>next 1 ;
|
2023-10-18 23:39:36 +00:00
|
|
|
: _cancel _resume>args n-nextdrop rdrop ;
|
2023-10-11 02:11:56 +00:00
|
|
|
|
2023-11-05 04:30:34 +00:00
|
|
|
defiter >genstart
|
|
|
|
:next _resume>args n-<next ;
|
|
|
|
' _cancel cancel!
|
|
|
|
: GENSTART_ <r >next >genstart ;
|
2023-10-04 00:59:18 +00:00
|
|
|
|
2023-10-18 23:39:36 +00:00
|
|
|
( yielding from a generator has three moving parts:
|
2023-11-05 03:39:30 +00:00
|
|
|
suspend: run immediately when the generator yields. takes care of updating
|
|
|
|
the i-stack appropriately, as well as potentially saving any
|
|
|
|
yield-specific state to the next-stack. After this runs, the
|
|
|
|
generator's extra parameters are pushed to the next-stack, along
|
|
|
|
with the generator's "resume" pointer and the yielder's iterator.
|
|
|
|
resume: run when the generator is resumed by iterating. Before this runs,
|
|
|
|
the yielder will take care of placing the "resume" pointer back
|
|
|
|
on the stack and restoring the generator's arguments, so the
|
|
|
|
stack environments should match what they were when the suspension
|
|
|
|
happened. This deals with cleaning up after the yielder so the
|
|
|
|
rest of the generator can run.
|
|
|
|
cancel: run when the generator is aborted. Before this runs, the next-stack
|
|
|
|
is cleared of the "resume" pointer and all of the generator's
|
|
|
|
arguments. This should remove any yield-specific data from the
|
|
|
|
iteration stacks, and potentially call "cancel" to clean up the
|
|
|
|
iterator underneath if the yielder is intended to map or filter
|
|
|
|
another iterator. )
|
|
|
|
|
2023-10-04 00:59:18 +00:00
|
|
|
{ var gen-arg-count
|
|
|
|
:timm (( t:| t, GENSTART_ gen-arg-count @ >t ;
|
2023-11-05 03:39:30 +00:00
|
|
|
:timm )) t, 0 t|; t, execute 0 gen-arg-count ! ;
|
2023-10-04 00:59:18 +00:00
|
|
|
: +arg 1 gen-arg-count !+ ; :timm +arg +arg ;
|
|
|
|
: -arg -1 gen-arg-count !+ ; :timm -arg -arg ;
|
|
|
|
:timm >arg t, >next +arg ;
|
2023-11-05 03:39:30 +00:00
|
|
|
: colonpair ( xt-first xt-second -- xt )
|
|
|
|
target <rot gencolon w>t swap w>t t, return ;
|
|
|
|
: :yield ( xt-suspend xt-resume xt-cancel -- )
|
|
|
|
} t' _cancel swap colonpair swap t' _resume swap colonpair
|
|
|
|
target >r w>t w>t gencolon w>t r@ compilenum t, _suspend t, return
|
|
|
|
create immediate <r 2 cells + , does> @ w>t gen-arg-count @ >t ; }
|
2023-10-04 00:59:18 +00:00
|
|
|
|
2023-11-05 03:39:30 +00:00
|
|
|
: shadow-i <i >next >i ; : 2>i >i >i ; : 2idrop idrop idrop ;
|
2023-10-18 23:39:36 +00:00
|
|
|
: unmap idrop <next >i ; : mapcancel unmap cancel ;
|
2023-11-05 03:39:30 +00:00
|
|
|
: unsuspend rdrop rdrop <r 1+ >r ; ( don't yield at all, skip past the yielder )
|
2023-10-18 23:39:36 +00:00
|
|
|
: suspend? not if unsuspend then ;
|
|
|
|
|
|
|
|
( suspend resume cancel )
|
2023-11-05 03:39:30 +00:00
|
|
|
' noop ' noop ' noop :yield yield0 [
|
2023-10-18 23:39:36 +00:00
|
|
|
' >i ' idrop ' idrop :yield yield
|
|
|
|
' >i ' <i ' idrop :yield yield>
|
|
|
|
' 2>i ' 2idrop dup :yield yield2
|
2023-11-05 03:39:30 +00:00
|
|
|
' shadow-i ' unmap ' mapcancel :yield map
|
2023-10-18 23:39:36 +00:00
|
|
|
' noop ' noop ' cancel :yield pass
|
|
|
|
' suspend? ' noop ' cancel :yield filter
|
2023-10-02 01:54:58 +00:00
|
|
|
|
2023-10-11 02:11:56 +00:00
|
|
|
: take ( n -- ) >arg (( each dup if pass else break then 1- next drop )) ;
|
|
|
|
: readbytes ( -- ) (( each i b@ map next )) ;
|
|
|
|
: chars ( p -- ) pchars readbytes ;
|