source: src/tests/coroutine.c@ 6e300d9

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 6e300d9 was e04b636, checked in by Thierry Delisle <tdelisle@…>, 9 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.