source: src/tests/coroutine.c @ 39c5ea3

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 39c5ea3 was e04b636, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Implemented and tested coroutine keyword

  • Property mode set to 100644
File size: 856 bytes
RevLine 
[c15b805]1#include <fstream>
[4a3334cf]2#include <coroutine>
[c15b805]3
[e04b636]4coroutine Fibonacci {
[c15b805]5      int fn; // used for communication
6};
7
[78b3f52]8void ?{}(Fibonacci* this) {
9      this->fn = 0;
[80d9e49]10}
11
[7fbe450]12void main(Fibonacci* this) {
[c15b805]13      int fn1, fn2;             // retained between resumes
14      this->fn = 0;
15      fn1 = this->fn;
[9129a84]16      suspend();                // return to last resume
[c15b805]17
18      this->fn = 1;
19      fn2 = fn1;
20      fn1 = this->fn;
[9129a84]21      suspend();                // return to last resume
[c15b805]22
23      for ( ;; ) {
24            this->fn = fn1 + fn2;
25            fn2 = fn1;
26            fn1 = this->fn;
[9129a84]27            suspend();  // return to last resume
[c15b805]28      }
29}
30
31int next(Fibonacci* this) {
32      resume(this); // transfer to last suspend
33      return this->fn;
34}
35
[9129a84]36int main() {
[d9c44c3]37      Fibonacci f1, f2;
[c15b805]38      for ( int i = 1; i <= 10; i += 1 ) {
[d9c44c3]39            sout | next(&f1) | ' ' | next(&f2) | endl;
[c15b805]40      }
[9129a84]41
42      return 0;
[0e76cf4f]43}
Note: See TracBrowser for help on using the repository browser.