source: tests/exceptions/fibonacci_nonlocal.cfa @ 64e3ac7

Last change on this file since 64e3ac7 was bef2245, checked in by caparsons <caparson@…>, 11 months ago

added basic tests for non-local exceptions

  • Property mode set to 100644
File size: 1.9 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2017 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// fibonacci.c -- 3-state finite-state machine
8//
9// Author           : Colby Parsons
10// Created On       : Thu July  6 07:29:37 2023
11// Last Modified By : Colby Parsons
12// Last Modified On : Thu July  6 07:29:37 2023
13// Update Count     : 0
14//
15
16#include <fstream.hfa>
17#include <coroutine.hfa>
18#include <stdlib.hfa>
19
20exception fib_num {
21    int num;
22};
23vtable(fib_num) fib_num_vt;
24
25exception fib_2_num {
26    int num1, num2;
27};
28vtable(fib_2_num) fib_2_num_vt;
29
30coroutine Fibonacci { int fn; };                                                // used for communication
31
32void main( Fibonacci & fib ) with( fib ) {                              // called on first resume
33    int fn1, fn2;                                                                               // retained between resumes
34    try{
35        fn = 0;  fn1 = fn;                                                                      // 1st case
36        poll( fib );
37        suspend;                                                                                        // restart last resume
38        fn = 1;  fn2 = fn1;  fn1 = fn;                                          // 2nd case
39        poll( fib );
40        suspend;                                                                                        // restart last resume
41        for () {
42            fn = fn1 + fn2;  fn2 = fn1;  fn1 = fn;                      // general case
43            poll( fib );
44            suspend;                                                                            // restart last resume
45        } // for
46    } catchResume ( fib_num * e ) {
47        fn1 = e->num;
48        fn2 = e->num;
49        fn = fn1 + fn2;
50    }  catchResume ( fib_2_num * e ) {
51        fn1 = e->num1;
52        fn2 = e->num1;
53        fn = fn1 + fn2;
54    }
55}
56
57int main() {
58        Fibonacci f1, f2;
59        for ( 3 ) {                                                                             // print N Fibonacci values
60                sout | resume( f1 ).fn;
61        } // for
62    for ( i; 3 ) {
63        fib_num except{ &fib_num_vt, i };
64        resumeAt( f1, except );
65        sout | resume( f1 ).fn;
66    }
67    {
68        fib_2_num except{ &fib_2_num_vt, 10, 12 };
69        resumeAt( f1, except );
70    }
71    sout | resume( f1 ).fn;
72}
73
74// Local Variables: //
75// tab-width: 4 //
76// compile-command: "cfa fibonacci.cfa" //
77// End: //
Note: See TracBrowser for help on using the repository browser.