source: src/tests/coroutine.c @ 58cb21c

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 58cb21c 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
Line 
1#include <fstream>
2#include <coroutine>
3
4coroutine Fibonacci {
5      int fn; // used for communication
6};
7
8void ?{}(Fibonacci* this) {
9      this->fn = 0;
10}
11
12void main(Fibonacci* this) {
13      int fn1, fn2;             // retained between resumes
14      this->fn = 0;
15      fn1 = this->fn;
16      suspend();                // return to last resume
17
18      this->fn = 1;
19      fn2 = fn1;
20      fn1 = this->fn;
21      suspend();                // return to last resume
22
23      for ( ;; ) {
24            this->fn = fn1 + fn2;
25            fn2 = fn1;
26            fn1 = this->fn;
27            suspend();  // return to last resume
28      }
29}
30
31int next(Fibonacci* this) {
32      resume(this); // transfer to last suspend
33      return this->fn;
34}
35
36int main() {
37      Fibonacci f1, f2;
38      for ( int i = 1; i <= 10; i += 1 ) {
39            sout | next(&f1) | ' ' | next(&f2) | endl;
40      }
41
42      return 0;
43}
Note: See TracBrowser for help on using the repository browser.