2005-01-29 14:18:28 -05:00
|
|
|
! Copyright (C) 2004, 2005 Slava Pestov.
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
|
|
|
IN: kernel DEFER: callcc1
|
2005-02-24 20:52:17 -05:00
|
|
|
IN: streams DEFER: line-number
|
|
|
|
IN: parser DEFER: file
|
|
|
|
IN: errors USING: kernel-internals lists namespaces streams ;
|
|
|
|
|
|
|
|
TUPLE: undefined-method object generic ;
|
2004-07-17 18:35:09 -04:00
|
|
|
|
2005-01-23 21:31:32 -05:00
|
|
|
: undefined-method ( object generic -- )
|
2005-02-24 20:52:17 -05:00
|
|
|
#! We 2dup here to leave both values on the stack, for
|
|
|
|
#! post-mortem inspection.
|
|
|
|
2dup <undefined-method> throw ;
|
2005-01-23 21:31:32 -05:00
|
|
|
|
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.
|
2004-08-10 21:32:10 -04:00
|
|
|
namespace [
|
2004-08-18 19:22:15 -04:00
|
|
|
"col" get
|
2004-08-10 21:32:10 -04:00
|
|
|
"line" get
|
2005-02-24 20:52:17 -05:00
|
|
|
line-number get
|
|
|
|
file get
|
2004-08-10 21:32:10 -04:00
|
|
|
global [
|
2004-08-18 19:22:15 -04:00
|
|
|
"error-file" set
|
2004-08-10 21:32:10 -04:00
|
|
|
"error-line-number" set
|
|
|
|
"error-line" set
|
2004-08-18 19:22:15 -04:00
|
|
|
"error-col" set
|
2004-08-10 21:32:10 -04:00
|
|
|
"error" set
|
2004-08-23 02:15:10 -04:00
|
|
|
datastack "error-datastack" set
|
|
|
|
callstack "error-callstack" set
|
2004-08-10 21:32:10 -04:00
|
|
|
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* ;
|