DD 10000 dup(0)
rstck: DD 16 dup(0)
+_frame: DD 0 ; frame pointer for catch/throw
+
_dp: DD _start ; dictionary pointer: points to next free location in memory
- ; free memory starts at _start
+ ; free memory starts at _start
_hp: DD 0 ; head pointer: points to first unused head
_head: DD 10000 dup (0)
+
section '.text' executable writable align 4096
public main
code 0< ( x -- flag )
pop eax
- or eax, eax
- mov eax, 0
- jns zless1
- dec eax
-zless1: push eax
+ sar eax,31
+ push eax
next
;
code c@ ( c-addr -- c )
pop edx
- xor eax, eax
+ xor eax, eax
mov al,byte [edx]
- push eax
+ push eax
next
;
next
;
+code sp@ ( -- x )
+ push esp
+ next
+;
+
+code sp! ( x -- )
+ pop esp
+ next
+;
+
+code rp@ ( -- x )
+ push ebp
+ next
+;
+
+code rp! ( x -- )
+ pop ebp
+ next
+;
: negate ( n1 -- n2 )
0 swap - ;
: does> ( -- ) \ set code field of last defined word
r> lit hp @ 1 - h@ dup >r 1 cells - ! lit dodoes r> !
-;
+;
+
+: catch ( i*x xt -- j*x 0 | i*x err )
+ lit frame @ >r sp@ >r rp@ lit frame ! execute r> drop r> lit frame ! 0 ;
+
+: throw ( i*x 0 | i*x err -- j*x err )
+ ?dup 0= ?exit lit frame @ rp! r> swap >r sp! drop r> r> lit frame ! ;
+
: cold ( -- )
's' emit 'e' dup emit emit 'd' emit 10 emit
lit +lit h, \ 41 29
lit and h, \ 42 2A
lit or h, \ 43 2B
+ lit catch h, \ 44 2C
+ lit throw h, \ 45 2D
+ lit sp@ h, \ 46 2E
+ lit sp! h, \ 47 2F
+ lit rp@ h, \ 48 30
+ lit rp! h, \ 49 31
tail interpreter ;
pre
$1C #FUN: here $1D #FUN: allot $1E #FUN: , $1F #FUN: c,
$20 #FUN: fun $21 #FUN: interpreter $22 #FUN: compiler $23 #FUN: create
$24 #FUN: does> $25 #FUN: cold $26 #FUN: depth $27 #FUN: compile,
-$28 #FUN: new ( $29 #FUN: +lit ) $2A #FUN: and $2B #FUN: or
+$28 #FUN: new ( $29 #FUN: +lit ) $2A #FUN: and $2B #FUN: or
+$2C #FUN: catch $2D #FUN: throw $2E #FUN: sp@ $2F #FUN: sp!
+$30 #FUN: rp@ $31 #FUN: rp!
: [ ( -- ) 0 SUBMIT ;
: ] ( -- ) compiler ;
cr type empty-stack ;'
': t{ ( i*x -- )
- empty-stack ;'
+ '.' #, emit empty-stack ;'
': -> ( -- )
depth actual-depth !
?ok 2drop
-cr 't' # emit 'e' # emit 's' # emit 't' # emit 'i' # emit 'n' # emit 'g' # emit
+cr 't' # emit 'e' # emit 's' # emit 't' # emit 'i' # emit 'n' # emit 'g' # emit cr
\ t{ 3 # 4 # + -> 7 # }t
\ t{ 3 # 4 # + -> 8 # }t
': negative -12345 #, ;'
t{ negative -> -12340 # 5 # - }t
+t{ 10 # ' dup catch -> 10 # 10 # 0 # }t
+
+': err99 ( x -- ) dup 9 #, = IF 99 #, throw THEN 1 #, + ;'
+
+t{ 1 # ' err99 catch -> 2 # 0 # }t
+t{ 5 # 9 # ' err99 catch nip -> 5 # 99 # }t
+
+': rot ( a b c -- b c a ) >r swap r> swap ;'
+t{ 10 # sp@ 20 # 30 # rot sp! -> 10 # }t
+
+
+': rp!-test ( -- ) rp@ 10 #, >r 20 #, >r 30 #, >r rp! ;'
+
+t{ 99 # rp!-test -> 99 # }t
+
+t{ 0 # 0< -> 0 # }t
+t{ 1 # 0< -> 0 # }t
+t{ 2 # 0< -> 0 # }t
+t{ 1 # negate 0< -> -1 # }t
+t{ 2 # negate 0< -> -1 # }t
+
cr 'd' # emit 'o' # emit 'n' # emit 'e' # emit cr
end