Imported Upstream version 0.1beta
[debian/yforth] / exceptio.c
1 /* yForth? - Written by Luca Padovani (C) 1996/97
2  * ------------------------------------------------------------------------
3  * This software is FreeWare as long as it comes with this header in each
4  * source file, anyway you can use it or any part of it whatever
5  * you want. It comes without any warranty, so use it at your own risk.
6  * ------------------------------------------------------------------------
7  * Module name:     exceptio.c
8  * Abstract:        exception word set
9  */
10
11 #include <malloc.h>
12 #include <setjmp.h>
13 #include "yforth.h"
14 #include "core.h"
15 #include "exceptio.h"
16
17 /**************************************************************************/
18 /* VARIABLES **************************************************************/
19 /**************************************************************************/
20
21 struct exception_frame *top_frame;  /* ptr to the top of exception stack */
22
23 /**************************************************************************/
24 /* WORDS ******************************************************************/
25 /**************************************************************************/
26
27 void _catch() {
28         register struct exception_frame *frame =
29                 (struct exception_frame *) malloc(sizeof(struct exception_frame));
30         if (frame) {
31                 register int ret_val;
32                 if ((ret_val = setjmp(frame->catch_buf)) == 0) {
33             /* Executed when "catch" is invoked */
34                         save_input_specification();
35                         frame->sp = sp + 1;
36                         frame->rp = rp;
37                         frame->bp = bp;
38                         frame->fp = fp;
39                         frame->last = top_frame;
40                         top_frame = frame;
41                         exec_word((struct word_def *) *sp++);
42                         *--sp = 0;
43                 } else *--sp = ret_val;
44                 frame = top_frame;
45                 sp = frame->sp;
46                 rp = frame->rp;
47                 bp = frame->bp;
48                 top_frame = frame->last;
49                 free(frame);
50                 restore_input_specification();
51         }
52 }
53
54 void _throw() {
55         register Cell n = *sp++;
56         if (n) {
57                 if (top_frame) longjmp(top_frame->catch_buf, n);
58                 else if (n == -1) ;
59                 else if (n == -2) _type();
60                 sp = sp_top;
61                 longjmp(warm_start_jump, 1);
62         }
63 }
64