| Last change
 on this file since 4dc3b8c was             90b9e4b, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago | 
        
          | 
add Simila, Python and uC++ example coroutines
 | 
        
          | 
              
Property                 mode
 set to                 100644 | 
        
          | File size:
            1014 bytes | 
      
      
| Rev | Line |  | 
|---|
| [90b9e4b] | 1 | BEGIN | 
|---|
|  | 2 | CLASS Fibonacci; | 
|---|
|  | 3 | HIDDEN fn, main;                                        ! private members; | 
|---|
|  | 4 | BEGIN | 
|---|
|  | 5 | INTEGER fn;                                                     ! communication; | 
|---|
|  | 6 |  | 
|---|
|  | 7 | PROCEDURE main;                                         ! mimic uC++ coroutine main; | 
|---|
|  | 8 | BEGIN | 
|---|
|  | 9 | INTEGER fn1, fn2; | 
|---|
|  | 10 |  | 
|---|
|  | 11 | fn := 0; fn1 := fn; | 
|---|
|  | 12 | Detach;                                                 ! suspend(); | 
|---|
|  | 13 | fn := 1; fn2 := fn1; fn1 := fn; | 
|---|
|  | 14 | Detach;                                                 ! suspend(); | 
|---|
|  | 15 | WHILE TRUE DO BEGIN | 
|---|
|  | 16 | fn := fn1 + fn2; fn2 := fn1; fn1 := fn; | 
|---|
|  | 17 | Detach;                                         ! suspend(); | 
|---|
|  | 18 | END; | 
|---|
|  | 19 | END; | 
|---|
|  | 20 |  | 
|---|
|  | 21 | INTEGER PROCEDURE next; | 
|---|
|  | 22 | BEGIN | 
|---|
|  | 23 | Call( THIS Fibonacci );                 ! resume(); | 
|---|
|  | 24 | next := fn; | 
|---|
|  | 25 | END; | 
|---|
|  | 26 | ! Fibonacci constructor code; | 
|---|
|  | 27 | Detach;                                                         ! return to declaration; | 
|---|
|  | 28 | main;                                                           ! call main as last line of constructor; | 
|---|
|  | 29 | END Fibonacci; | 
|---|
|  | 30 | ! program main equivalent; | 
|---|
|  | 31 | REF(Fibonacci) f1, f2;                                  ! objects are references; | 
|---|
|  | 32 | INTEGER i; | 
|---|
|  | 33 |  | 
|---|
|  | 34 | f1 :- NEW Fibonacci; | 
|---|
|  | 35 | f2 :- NEW Fibonacci; | 
|---|
|  | 36 | FOR i := 1 STEP 1 UNTIL 10 DO BEGIN | 
|---|
|  | 37 | OutInt( f1.next, 3 ); | 
|---|
|  | 38 | OutText( " " ); | 
|---|
|  | 39 | OutInt( f2.next, 3 ); | 
|---|
|  | 40 | OutImage; | 
|---|
|  | 41 | END | 
|---|
|  | 42 | END; | 
|---|
|  | 43 |  | 
|---|
|  | 44 | ! Local Variables: ; | 
|---|
|  | 45 | ! tab-width: 4 ; | 
|---|
|  | 46 | ! compile-command: "cim Fib.sim" ; | 
|---|
|  | 47 | ! End: ; | 
|---|
       
      
  Note:
 See   
TracBrowser
 for help on using the repository browser.