factor/library/errors.factor

84 lines
2.8 KiB
Factor
Raw Normal View History

! :folding=indent:collapseFolds=1:
2004-07-17 18:35:09 -04:00
! $Id$
!
! Copyright (C) 2004 Slava Pestov.
!
! Redistribution and use in source and binary forms, with or without
! modification, are permitted provided that the following conditions are met:
!
! 1. Redistributions of source code must retain the above copyright notice,
! this list of conditions and the following disclaimer.
!
! 2. Redistributions in binary form must reproduce the above copyright notice,
! this list of conditions and the following disclaimer in the documentation
! and/or other materials provided with the distribution.
!
! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
IN: kernel
DEFER: callcc1
2004-07-17 18:35:09 -04:00
IN: errors
USE: kernel
2004-12-24 02:52:02 -05:00
USE: kernel-internals
2004-07-17 18:35:09 -04:00
USE: lists
2004-08-26 22:21:17 -04:00
USE: math
2004-07-17 18:35:09 -04:00
USE: namespaces
USE: strings
USE: vectors
: undefined-method ( object generic -- )
#! This word is redefined in tools/debugger.factor with a
#! more useful definition once unparse is available.
"No suitable method" throw ;
2004-11-25 21:51:47 -05:00
! This is a very lightweight exception handling system.
2004-11-25 23:14:17 -05:00
: catchstack ( -- cs ) 6 getenv ;
: set-catchstack ( cs -- ) 6 setenv ;
2004-11-25 21:51:47 -05:00
2004-11-25 23:14:17 -05:00
: >c ( catch -- ) catchstack cons set-catchstack ;
: c> ( catch -- ) catchstack uncons set-catchstack ;
2004-07-17 18:35:09 -04:00
2004-07-17 19:33:35 -04:00
: save-error ( error -- )
2004-07-30 16:22:20 -04:00
#! Save the stacks and parser state for post-mortem
#! inspection after an error.
namespace [
2004-08-18 19:22:15 -04:00
"col" get
"line" get
"line-number" get
2004-08-18 19:22:15 -04:00
"file" get
global [
2004-08-18 19:22:15 -04:00
"error-file" set
"error-line-number" set
"error-line" set
2004-08-18 19:22:15 -04:00
"error-col" set
"error" set
datastack "error-datastack" set
callstack "error-callstack" set
namestack "error-namestack" set
catchstack "error-catchstack" set
] bind
2004-08-10 23:48:08 -04:00
] when ;
2004-07-17 18:35:09 -04:00
: catch ( try catch -- )
2004-07-19 16:10:18 -04:00
#! Call the try quotation. If an error occurs restore the
#! datastack, push the error, and call the catch block.
#! If no error occurs, push f and call the catch block.
[ >c >r call c> drop f r> f ] callcc1 rot drop swap call ;
2004-07-17 18:35:09 -04:00
: rethrow ( error -- )
#! Use rethrow when passing an error on from a catch block.
#! For convinience, this word is a no-op if error is f.
[ c> call ] when* ;