Annotation of gforth/except.fs, revision 1.6

1.1       anton       1: \ catch, throw, etc.
                      2: 
1.2       anton       3: \ Copyright (C) 1999,2000 Free Software Foundation, Inc.
1.1       anton       4: 
                      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
1.3       anton      19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
1.1       anton      20: 
                     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: 
1.5       anton      36: : (try) ( ahandler -- )
1.1       anton      37:     r>
1.5       anton      38:     swap >r \ recovery address
1.1       anton      39:     rp@ 'catch >r
                     40:     sp@ >r
                     41:     fp@ >r
                     42:     lp@ >r
                     43:     handler @ >r
                     44:     rp@ handler !
                     45:     backtrace-empty on
1.5       anton      46:     >r ;
1.1       anton      47: 
                     48: : try ( compilation  -- orig ; run-time  -- ) \ gforth
1.5       anton      49:     \ !! does not work correctly for gforth-native
                     50:     POSTPONE lit >mark POSTPONE (try) ; immediate compile-only
1.1       anton      51: 
                     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
1.6     ! anton      84:            >stderr cr ." uncaught exception: " .error cr
1.1       anton      85:            2 (bye)
1.6     ! anton      86: \          quit
1.1       anton      87:        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: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>