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 bc934cdf 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
|
| Line | |
|---|
| 1 | #include <fstream>
|
|---|
| 2 | #include <coroutine>
|
|---|
| 3 |
|
|---|
| 4 | coroutine Fibonacci {
|
|---|
| 5 | int fn; // used for communication
|
|---|
| 6 | };
|
|---|
| 7 |
|
|---|
| 8 | void ?{}(Fibonacci* this) {
|
|---|
| 9 | this->fn = 0;
|
|---|
| 10 | }
|
|---|
| 11 |
|
|---|
| 12 | void 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 |
|
|---|
| 31 | int next(Fibonacci* this) {
|
|---|
| 32 | resume(this); // transfer to last suspend
|
|---|
| 33 | return this->fn;
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | int 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.