2019-02-04 02:04:02 +00:00
|
|
|
: stdout ' putc task-emit ! ;
|
2019-02-24 17:18:34 +00:00
|
|
|
: withfp ( xt fp -- ) fdeactivate r> factivate execute fdeactivate drop r< factivate ;
|
2019-02-04 02:04:02 +00:00
|
|
|
|
2019-02-24 17:18:34 +00:00
|
|
|
: >rot <rot <rot ;
|
2019-02-02 20:30:18 +00:00
|
|
|
: 2dup over over ;
|
2019-02-24 17:18:34 +00:00
|
|
|
: 3dup r> 2dup r@ >rot r< ;
|
|
|
|
: 4dup r> r> 2dup r@ >rot rswap r@ >rot r< r< swap ;
|
2019-02-24 15:14:56 +00:00
|
|
|
|
2019-03-01 02:46:04 +00:00
|
|
|
: 2= ( a b c d -- a=c&b=d )
|
|
|
|
r> <rot = swap r< = and ;
|
|
|
|
|
|
|
|
: ~ -1 ^ ;
|
|
|
|
: f! ( b v flag -- )
|
|
|
|
r> dup @ ( b v val r: flag )
|
|
|
|
<rot if r< | else r< ~ & then ( v newval )
|
|
|
|
swap ! ;
|
|
|
|
: f@ ( v flag -- ) swap @ & ;
|
2019-03-12 01:57:22 +00:00
|
|
|
: fnot! 2dup f@ not f! ;
|
2019-03-01 02:46:04 +00:00
|
|
|
|
2019-03-05 22:35:50 +00:00
|
|
|
: expile state if , else execute then ;
|
|
|
|
|
2019-02-26 03:19:08 +00:00
|
|
|
' cells @ const $DOCOLON ( get the colon execution token )
|
|
|
|
: :noname here $DOCOLON , ] ;
|
2019-02-24 15:14:56 +00:00
|
|
|
|
2019-02-26 03:19:08 +00:00
|
|
|
: :| inline| $DOCOLON , ; immediate
|
2019-02-24 15:14:56 +00:00
|
|
|
: |; ' ret , |inline ; immediate
|
2019-02-12 04:23:00 +00:00
|
|
|
|
2019-02-27 02:44:22 +00:00
|
|
|
: array word new-word $DOVAR , ;
|
2019-02-26 03:19:08 +00:00
|
|
|
: create word new-word $DOCREATE , 0 , ;
|
|
|
|
|
|
|
|
: finishcreate ( ipfirst -- )
|
|
|
|
( set cell after codepointer to first instruction of does> )
|
|
|
|
latest codepointer cell + ! ;
|
|
|
|
|
|
|
|
: does> here 4 cells + lit ' finishcreate , ' ret , ] ; immediate
|
|
|
|
|
2019-02-24 22:26:28 +00:00
|
|
|
: +towards ( from to -- from+-1 )
|
|
|
|
over > if 1 + else 1 - then ;
|
|
|
|
|
2019-03-05 22:35:50 +00:00
|
|
|
: for ( from to -- )
|
|
|
|
' r> , [ ' begin , ] ( from r: to )
|
|
|
|
' dup , ' r@ , ' != , [ ' while , ]
|
|
|
|
' r> , ; immediate ( r: to from )
|
2019-02-24 17:18:34 +00:00
|
|
|
: i ' r@ , ; immediate
|
2019-03-05 22:35:50 +00:00
|
|
|
: next
|
|
|
|
' r< , ' r@ , ' +towards , ( from+1 r: to )
|
|
|
|
[ ' repeat , ] ' drop , ' rdrop , ; immediate
|
2019-02-24 22:26:28 +00:00
|
|
|
|
2019-03-01 02:46:04 +00:00
|
|
|
: yield rswap ;
|
|
|
|
: each [ ' begin , ] ' dup , [ ' while , ] ; immediate
|
|
|
|
: more ' yield , [ ' repeat , ] ' drop , ] ; immediate
|
|
|
|
: dobreak yield 0 ;
|
|
|
|
: break ' rdrop , ' dobreak , ; immediate
|
|
|
|
|
2019-03-09 23:49:45 +00:00
|
|
|
: links begin yield @ dup not until ;
|
|
|
|
|
2019-03-12 01:57:22 +00:00
|
|
|
( usage:
|
|
|
|
: search-xy { x y -- b r: coroutine } begin 2dup search >rot drop drop ;
|
|
|
|
: test-xy { x y -- b }
|
|
|
|
search-xy 1 2 2= yield 3 4 2= yield drop drop 999 yield ;
|
|
|
|
test-xy will return 1 if x y is 1 2 or 3 4, otherwise it returns 999.
|
|
|
|
Note that it must always end with a yield, as search has no way to tell
|
|
|
|
the difference between an early termination and a final non-zero result.
|
|
|
|
)
|
|
|
|
: search
|
|
|
|
' yield , ' dup , ' not , [ ' while , ] ' drop , [ ' repeat , ]
|
|
|
|
' rdrop , ; immediate
|
|
|
|
|
2019-02-24 22:26:28 +00:00
|
|
|
: min ( x y -- x|y ) 2dup > if swap then drop ;
|
|
|
|
: max ( x y -- x|y ) 2dup < if swap then drop ;
|
2019-02-24 17:18:34 +00:00
|
|
|
|
2019-03-10 23:51:24 +00:00
|
|
|
: checkpoint ( cp -- )
|
|
|
|
create here 4 cells + , latest , tasks , ,
|
|
|
|
does> dup @ here!
|
|
|
|
dup cell + @ latest!
|
|
|
|
dup 2 cells + @ tasks!
|
|
|
|
3 cells + @ execute ;
|
|
|
|
|
|
|
|
: intern create latest wordname , does> @ ;
|
|
|
|
|
2019-02-02 20:30:18 +00:00
|
|
|
: decompile
|
2019-02-04 02:04:02 +00:00
|
|
|
word lookup if 1 begin ( cp i )
|
2019-02-02 20:30:18 +00:00
|
|
|
2dup cells + @ ( cp i @cp+i )
|
2019-02-04 02:04:02 +00:00
|
|
|
dup ' ret != ( cp i @cp+i bool )
|
2019-02-02 20:30:18 +00:00
|
|
|
while
|
2019-02-04 02:04:02 +00:00
|
|
|
dup ` dup if type drop else drop . then bl ( cp i )
|
2019-02-02 20:30:18 +00:00
|
|
|
1 + ( cp i+1 )
|
2019-02-04 02:04:02 +00:00
|
|
|
repeat drop drop then drop ;
|
|
|
|
|
2019-03-10 00:59:52 +00:00
|
|
|
: words latest links each dup wordname type bl more ;
|
|
|
|
|
2019-02-04 02:04:02 +00:00
|
|
|
( tasks )
|
|
|
|
: mailbox 2 cells + ;
|
|
|
|
: task-ip task-user-size cells + ;
|
|
|
|
: task-sp task-user-size 1 + cells + ;
|
|
|
|
: task-rsp task-user-size 2 + cells + ;
|
|
|
|
: task-stack task-user-size 3 + cells + ;
|
|
|
|
: task-rstack task-stack stacksize cells + ;
|
|
|
|
|
2019-03-09 23:49:45 +00:00
|
|
|
: .wordin ( ptr -- )
|
|
|
|
latest links each
|
|
|
|
2dup > if wordname type drop 0 break then
|
|
|
|
more dup if . else drop then ;
|
|
|
|
|
|
|
|
: tasks.s
|
|
|
|
tasks links each
|
|
|
|
dup .wordin s" : " type
|
|
|
|
dup task-sp @ over task-stack ( task stackLim stack )
|
|
|
|
begin 2dup > while dup @ . cell + repeat
|
2019-03-10 23:51:24 +00:00
|
|
|
cr drop drop more ;
|
2019-03-09 23:49:45 +00:00
|
|
|
|
2019-02-04 02:04:02 +00:00
|
|
|
: doactivate ( task ip -- )
|
|
|
|
over task-ip !
|
|
|
|
dup task-stack over task-sp !
|
|
|
|
dup task-rstack over task-rsp !
|
|
|
|
drop
|
|
|
|
;
|
|
|
|
|
|
|
|
: activate
|
|
|
|
here 4 cells + lit
|
|
|
|
' doactivate ,
|
|
|
|
' ret ,
|
|
|
|
; immediate
|
|
|
|
|
2019-03-09 23:49:45 +00:00
|
|
|
: try-send ( val task -- b )
|
|
|
|
mailbox dup @ if drop drop 0 else ! 1 then ;
|
|
|
|
|
|
|
|
: wait-send ( val task -- )
|
2019-02-04 02:04:02 +00:00
|
|
|
mailbox
|
|
|
|
begin dup @ while suspend repeat ( wait for empty mailbox )
|
|
|
|
! ;
|
|
|
|
|
2019-03-09 23:49:45 +00:00
|
|
|
: send ( val task -- ) try-send drop ;
|
|
|
|
|
2019-02-04 02:04:02 +00:00
|
|
|
: receive ( -- val )
|
|
|
|
running mailbox
|
|
|
|
begin dup @ not while suspend repeat ( wait for mail )
|
2019-02-24 17:18:34 +00:00
|
|
|
dup @ 0 <rot ! ;
|
2019-02-04 02:04:02 +00:00
|
|
|
|