Skip to content

Commit

Permalink
catch&throw seem ok
Browse files Browse the repository at this point in the history
  • Loading branch information
jkotlinski committed Dec 15, 2024
1 parent cc6c527 commit fc931b1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
19 changes: 14 additions & 5 deletions forth/exception.fs
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
variable handler 0 handler !

code sp@ w stx, tsx, txa, w ldx,
w ldy, ' pushya jmp, end-code
: catch
[ txa, ' pushya jsr, ] \ sp@
>r handler @ >r
[ w stx, tsx, txa, w ldx,
' pushya jsr, ] \ rp@
handler ! execute
r> handler ! r> drop 0 ;

: catch ( xt -- e|0 )
handler @ >r sp@ handler !
execute r> handler ! r> drop 0 ;
: throw
?dup if handler @
[ w stx, lsb lda,x tax, txs,
w ldx, inx, ] \ rp!
r> handler ! r> swap >r
[ lsb lda,x tax, ] \ sp!
drop r> then ;
2 changes: 1 addition & 1 deletion test/testexception.fs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ T{ C2 -> 1 2 8 0 }T \ 0 THROW does nothing
: C3 1 2 ['] T3 CATCH ;
T{ C3 -> 1 2 99 }T \ Restores stack to CATCH depth

: T4 1- DUP 0> IF RECURSE ELSE 999 THROW -222 THEN ;
: T4 1- DUP 0 > IF RECURSE ELSE 999 THROW -222 THEN ;
: C4 3 4 5 10 ['] T4 CATCH -111 ;
T{ C4 -> 3 4 5 0 999 -111 }T \ Test return stack unwinding

Expand Down

0 comments on commit fc931b1

Please sign in to comment.