Changeset 5186775


Ignore:
Timestamp:
Jul 7, 2023, 7:34:18 AM (10 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
b29a1e8
Parents:
64e3ac7
Message:

alternative version of fibonacci_nonlocal

Location:
tests/exceptions
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • tests/exceptions/.expect/fibonacci_nonlocal.txt

    r64e3ac7 r5186775  
    1 0
    2 1
    3 1
    4 0
    5 2
    6 4
    7 20
     1    0     0
     2    1     1
     3    1     1
     4    2     2
     5    3     3
     6    5     5
     7    8     8
     8   13    13
  • tests/exceptions/fibonacci_nonlocal.cfa

    r64e3ac7 r5186775  
    99// Author           : Colby Parsons
    1010// 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
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Thu Jul  6 21:49:04 2023
     13// Update Count     : 18
    1414//
    1515
     
    1818#include <stdlib.hfa>
    1919
    20 exception fib_num {
    21     int num;
    22 };
     20exception fib_num {};
    2321vtable(fib_num) fib_num_vt;
    24 
    25 exception fib_2_num {
    26     int num1, num2;
    27 };
    28 vtable(fib_2_num) fib_2_num_vt;
     22fib_num except{ &fib_num_vt };
    2923
    3024coroutine Fibonacci { int fn; };                                                // used for communication
    3125
    3226void 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     }
     27        int fn1, fn2;                                                                           // retained between resumes
     28        try {
     29                poll( fib );
     30                suspend;                                                                                // restart last resume
     31        } catchResume ( fib_num * e ) {
     32                fn = 0;  fn1 = fn;                                                              // 1st case
     33        }
     34        try {
     35                poll( fib );
     36                suspend;                                                                                // restart last resume
     37        } catchResume ( fib_num * e ) {
     38                fn = 1;  fn2 = fn1;  fn1 = fn;                                  // 2nd case
     39        }
     40        try {
     41                for () {
     42                        poll( fib );
     43                        suspend;                                                                        // restart last resume
     44                } // for
     45        } catchResume ( fib_num * e ) {
     46                fn = fn1 + fn2;  fn2 = fn1;  fn1 = fn;                  // general case
     47        }
    5548}
    5649
    5750int main() {
    5851        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;
     52        for ( i; 8 ) {
     53                resumeAt( f1, except );  resumeAt( f2, except );
     54                sout | wd( 5, resume( f1 ).fn ) | wd( 5, resume( f2 ).fn );
     55        }
    7256}
    7357
    7458// Local Variables: //
    75 // tab-width: 4 //
    76 // compile-command: "cfa fibonacci.cfa" //
     59// compile-command: "cfa fibonacci_nonlocal.cfa" //
    7760// End: //
Note: See TracChangeset for help on using the changeset viewer.