| 1 : |
anton
|
1.1
|
\ catch, throw, etc. |
| 2 : |
|
|
|
| 3 : |
anton
|
1.7
|
\ Copyright (C) 1999,2000,2003 Free Software Foundation, Inc. |
| 4 : |
anton
|
1.1
|
|
| 5 : |
|
|
\ This file is part of Gforth. |
| 6 : |
|
|
|
| 7 : |
|
|
\ Gforth is free software; you can redistribute it and/or |
| 8 : |
|
|
\ modify it under the terms of the GNU General Public License |
| 9 : |
|
|
\ as published by the Free Software Foundation; either version 2 |
| 10 : |
|
|
\ of the License, or (at your option) any later version. |
| 11 : |
|
|
|
| 12 : |
|
|
\ This program is distributed in the hope that it will be useful, |
| 13 : |
|
|
\ but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 : |
|
|
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 : |
|
|
\ GNU General Public License for more details. |
| 16 : |
|
|
|
| 17 : |
|
|
\ You should have received a copy of the GNU General Public License |
| 18 : |
|
|
\ along with this program; if not, write to the Free Software |
| 19 : |
anton
|
1.3
|
\ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
| 20 : |
anton
|
1.1
|
|
| 21 : |
|
|
\ !! use a separate exception stack? anton |
| 22 : |
|
|
|
| 23 : |
|
|
\ user-definable rollback actions |
| 24 : |
|
|
|
| 25 : |
|
|
Defer 'catch |
| 26 : |
|
|
Defer 'throw |
| 27 : |
|
|
|
| 28 : |
|
|
' noop IS 'catch |
| 29 : |
|
|
' noop IS 'throw |
| 30 : |
|
|
|
| 31 : |
|
|
\ has? backtrace [IF] |
| 32 : |
|
|
Defer store-backtrace |
| 33 : |
|
|
' noop IS store-backtrace |
| 34 : |
|
|
\ [THEN] |
| 35 : |
|
|
|
| 36 : |
anton
|
1.5
|
: (try) ( ahandler -- ) |
| 37 : |
anton
|
1.1
|
r> |
| 38 : |
anton
|
1.5
|
swap >r \ recovery address |
| 39 : |
anton
|
1.1
|
rp@ 'catch >r |
| 40 : |
|
|
sp@ >r |
| 41 : |
|
|
fp@ >r |
| 42 : |
|
|
lp@ >r |
| 43 : |
|
|
handler @ >r |
| 44 : |
|
|
rp@ handler ! |
| 45 : |
|
|
backtrace-empty on |
| 46 : |
anton
|
1.5
|
>r ; |
| 47 : |
anton
|
1.1
|
|
| 48 : |
|
|
: try ( compilation -- orig ; run-time -- ) \ gforth |
| 49 : |
anton
|
1.5
|
\ !! does not work correctly for gforth-native |
| 50 : |
|
|
POSTPONE lit >mark POSTPONE (try) ; immediate compile-only |
| 51 : |
anton
|
1.1
|
|
| 52 : |
|
|
: (recover) ( -- ) |
| 53 : |
|
|
\ normal end of try block: restore handler, forget rest |
| 54 : |
|
|
r> |
| 55 : |
|
|
r> handler ! |
| 56 : |
|
|
rdrop \ lp |
| 57 : |
|
|
rdrop \ fp |
| 58 : |
|
|
rdrop \ sp |
| 59 : |
|
|
r> rp! |
| 60 : |
|
|
rdrop \ recovery address |
| 61 : |
|
|
>r ; |
| 62 : |
|
|
|
| 63 : |
|
|
: recover ( compilation orig1 -- orig2 ; run-time -- ) \ gforth |
| 64 : |
|
|
\ !! check using a special tag |
| 65 : |
|
|
POSTPONE (recover) |
| 66 : |
|
|
POSTPONE else ; immediate compile-only |
| 67 : |
|
|
|
| 68 : |
|
|
: endtry ( compilation orig -- ; run-time -- ) \ gforth |
| 69 : |
|
|
POSTPONE then ; immediate compile-only |
| 70 : |
|
|
|
| 71 : |
|
|
:noname ( x1 .. xn xt -- y1 .. ym 0 / z1 .. zn error ) \ exception |
| 72 : |
|
|
try |
| 73 : |
|
|
execute 0 |
| 74 : |
|
|
recover |
| 75 : |
|
|
nip |
| 76 : |
|
|
endtry ; |
| 77 : |
|
|
is catch |
| 78 : |
|
|
|
| 79 : |
|
|
:noname ( y1 .. ym error/0 -- y1 .. ym / z1 .. zn error ) \ exception |
| 80 : |
|
|
?DUP IF |
| 81 : |
|
|
[ here forthstart 9 cells + ! ] |
| 82 : |
|
|
store-backtrace |
| 83 : |
|
|
handler @ ?dup-0=-IF |
| 84 : |
anton
|
1.6
|
>stderr cr ." uncaught exception: " .error cr |
| 85 : |
anton
|
1.1
|
2 (bye) |
| 86 : |
anton
|
1.6
|
\ quit |
| 87 : |
anton
|
1.1
|
THEN |
| 88 : |
|
|
rp! |
| 89 : |
|
|
r> handler ! |
| 90 : |
|
|
r> lp! |
| 91 : |
|
|
r> fp! |
| 92 : |
|
|
r> swap >r sp! drop r> |
| 93 : |
|
|
rdrop 'throw |
| 94 : |
|
|
THEN ; |
| 95 : |
|
|
is throw |
| 96 : |
|
|
|