1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
/*
* Copyright (C) 2008 Free Software Foundation, Inc.
* This file is part of GNU Modula-2.
*
* GNU Modula-2 is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2, or (at your option) any later
* version.
*
* GNU Modula-2 is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with gm2; see the file COPYING. If not, write to the Free Software
* Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <setjmp.h>
#include <malloc.h>
#include <stdio.h>
typedef enum jmpstatus {
jmp_normal,
jmp_retry,
jmp_exception,
} jmp_status;
struct setjmp_stack {
jmp_buf env;
struct setjmp_stack *next;
} *head = NULL;
void pushsetjmp (void)
{
struct setjmp_stack *p = (struct setjmp_stack *) malloc (sizeof (struct setjmp_stack));
p->next = head;
head = p;
}
void exception (void)
{
printf("invoking exception handler\n");
longjmp (head->env, jmp_exception);
}
void retry (void)
{
printf("retry\n");
longjmp (head->env, jmp_retry);
}
void popsetjmp (void)
{
struct setjmp_stack *p = head;
head = head->next;
free (p);
}
static int *ip = NULL;
void fly (void)
{
printf("fly main body\n");
if (ip == NULL) {
printf("ip == NULL\n");
exception();
}
if ((*ip) == 0) {
printf("*ip == 0\n");
exception();
}
if ((4 / (*ip)) == 4)
printf("yes it worked\n");
else
printf("no it failed\n");
}
/*
* a GNU C version of the Modula-2 example given in the ISO standard.
* This is written to prove that the underlying runtime system
* will work with the C interpretation. Thus gm2 will produce
* trees which follow the "template" setjmp/longjmp schema below
* when compiling EXCEPT/TRY statements.
*/
void tryFlying (void)
{
void tryFlying_m2_exception () {
printf("inside tryFlying exception routine\n");
if ((ip != NULL) && ((*ip) == 0)) {
(*ip) = 1;
retry();
}
}
int t;
pushsetjmp ();
do {
t = setjmp (head->env);
} while (t == jmp_retry);
if (t == jmp_exception) {
/* exception called */
tryFlying_m2_exception ();
/* exception has not been handled, invoke previous handler */
printf("exception not handled here\n");
popsetjmp();
exception();
}
printf("tryFlying main body\n");
fly();
popsetjmp();
}
void keepFlying (void)
{
void keepFlying_m2_exception () {
printf("inside keepFlying exception routine\n");
if (ip == NULL) {
ip = (int *)malloc (sizeof (int));
*ip = 0;
retry();
}
}
int t;
pushsetjmp ();
do {
t = setjmp (head->env);
} while (t == jmp_retry);
if (t == jmp_exception) {
/* exception called */
keepFlying_m2_exception ();
/* exception has not been handled, invoke previous handler */
popsetjmp();
exception();
}
printf("keepFlying main body\n");
tryFlying();
popsetjmp();
}
main ()
{
keepFlying();
printf("all done\n");
}
|