Debian Codegraph @

terps/tads/tads3/test/data/finally.t

raw
1 #include "tads.h"
2
3 function _main(args)
4 {
5 tadsSay('entering main\n');
6
7 break_test();
8
9 cont_test();
10
11 tadsSay('ret_test(nil) = ' + ret_test(nil));
12 tadsSay('ret_test(true) = ' + ret_test(true));
13
14 goto_test();
15
16 exc_test();
17
18 tadsSay('done with main\n');
19 }
20
21 /* ------------------------------------------------------------------------ */
22 /*
23 * This function tests processing of 'finally' when breaking out of
24 * loops and switches.
25 */
26 function break_test()
27 {
28 tadsSay('Break Tests\b');
29
30 tadsSay('loop 1\n');
31 for (local i = 1 ; i < 5 ; ++i)
32 {
33 tadsSay('entering try1\n');
34 try
35 {
36 tadsSay('loop 1 - i = ' + i + '\n');
37 if (i > 3)
38 {
39 tadsSay('breaking...\n');
40 break;
41 }
42 }
43 finally
44 {
45 tadsSay('this is the finally for try 1\n');
46 }
47 tadsSay('done with try1\n');
48 }
49 tadsSay('end of loop 1\b');
50
51 tadsSay('loop 2 - label LOOP2:\n');
52 loop2:
53 for (local i = 1 ; i < 5 ; ++i)
54 {
55 tadsSay('(( inner loop 2 - i = ' + i + '\n');
56 for (local j = 1 ; j < i ; ++j)
57 {
58 tadsSay('\t+ loop 2a - j = ' + j + '\n');
59 if (j == 3)
60 {
61 tadsSay('breaking out of LOOP2...\n');
62 break loop2;
63 }
64 }
65 tadsSay(')) end of inner loop 2 - i = ' + i + '\n');
66 }
67 tadsSay('end of loop 2\n');
68 }
69
70 /* ------------------------------------------------------------------------ */
71 /*
72 * This function tests continuing from protected code
73 */
74 function cont_test()
75 {
76 tadsSay('\bContinue Test\b');
77
78 local i;
79 i = 0;
80 loop1:
81 while (i < 5)
82 {
83 try
84 {
85 ++i;
86 tadsSay('loop1 - i = ' + i + '\n');
87
88 for (local j = 1 ; j < 100 ; ++j)
89 {
90 try
91 {
92 tadsSay('\tinner loop 1a - j = ' + j + '\n');
93 if (j == 2)
94 {
95 tadsSay('+ continuing...\n');
96 continue loop1;
97 }
98 }
99 finally
100 {
101 tadsSay('inner finally\n');
102 }
103 }
104 }
105 finally
106 {
107 tadsSay('outer finally\n');
108 }
109 }
110 tadsSay('end loop1\n');
111 }
112
113 /* ------------------------------------------------------------------------ */
114 /*
115 * This function tests returning from protected code
116 */
117 function ret_test(ret_middle)
118 {
119 tadsSay('\bReturn Test -- will ' + (ret_middle ? '' : 'not ') +
120 'return in the middle of the try block\b');
121
122 tadsSay('entering try 1\n');
123 try
124 {
125 tadsSay('entering try 1a\n');
126 try
127 {
128 if (ret_middle)
129 {
130 tadsSay('returning...\n');
131 return 'TRUE!!!';
132 }
133 else
134 {
135 tadsSay('not returning\n');
136 }
137
138 tadsSay('end of inner try 1a\n');
139 }
140 finally
141 {
142 tadsSay('finally 1a\n');
143 }
144
145 tadsSay('end out outer try 1\n');
146 }
147 finally
148 {
149 tadsSay('finally 1\n');
150 }
151
152 tadsSay('end of ret_test\n');
153
154 return 'NIL!!!';
155 }
156
157 /* ------------------------------------------------------------------------ */
158 /*
159 * goto test - test jumping around within a function with 'goto', and
160 * the interaction with 'finally' handlers
161 */
162 function goto_test()
163 {
164 tadsSay('\nGoto Tests\b');
165
166 tadsSay('simple goto s1...\n');
167 goto s1;
168
169 s3:
170 tadsSay('now at s3!\n');
171
172 tadsSay('jumping into a try - s4\n');
173 goto s4;
174
175 s1:
176 tadsSay('here we are at s1!\n');
177
178 tadsSay('entering try 1\n');
179 try
180 {
181 tadsSay('goto within try - s2\n');
182 goto s2;
183
184 s4:
185 tadsSay('at s4!\n');
186
187 tadsSay('leaving try 1 again - s5\n');
188 goto s5;
189
190 s2:
191 tadsSay('here we are at s2!\n');
192
193 tadsSay('jumping out of try 1 to s3\n');
194 goto s3;
195
196 // this is unreachable
197 tadsSay('??? how did we get here???\n');
198 }
199 finally
200 {
201 tadsSay('finally 1\n');
202 }
203
204 // this is also unreachable
205 tadsSay('??? unreachable!\n');
206
207 s5:
208 tadsSay('okay, this is s5\n');
209 }
210
211
212 /* ------------------------------------------------------------------------ */
213 /*
214 * This function ends up throwing an uncaught error (a VM-level run-time
215 * exception), so it will never return. This tests processing 'finally'
216 * blocks while throwing an exception out of a protected block.
217 */
218 function exc_test()
219 {
220 local i;
221
222 tadsSay('\bException Throw Tests\b');
223
224 tadsSay('entering try 1\n');
225 try
226 {
227 tadsSay('this should be innocuous!\n');
228 }
229 finally
230 {
231 tadsSay('this is the first finally\n');
232 }
233
234 tadsSay('entering try 2\n');
235 try
236 {
237 tadsSay('entering nested try 2a\n');
238 try
239 {
240 tadsSay('in try - calling exc_test_thrower - valid value...\n');
241 exc_test_thrower(3);
242
243 tadsSay('in try - calling exc_test_thrower - INVALID value...\n');
244 exc_test_thrower('x');
245
246 tadsSay('??? back from invalid exc_test_thrower call ???\n');
247 }
248 finally
249 {
250 tadsSay('this is finally 2a\n');
251 }
252
253 tadsSay('after nested try???\n');
254 }
255 finally
256 {
257 tadsSay('this is the second finally block!\b');
258 }
259 }
260
261 function exc_test_thrower(x)
262 {
263 tadsSay('+ in exc_test_thrower - entering try\n');
264 try
265 {
266 tadsSay('+ in try - computing value\n');
267 local i = 5 + x;
268 tadsSay('+ result = ' + i + '\n');
269 }
270 finally
271 {
272 tadsSay('+ exc_test_thrower finally\n');
273 }
274
275 tadsSay('+ exc_test_thrower - returning\n');
276 }
277