blob: 2363989a7b87cee0fe8378410152ef87f2171c0f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
#include <stc/algo/cco.h>
#include <stdio.h>
#include <stdint.h>
// Use coroutine to create a fibonacci sequence generator:
typedef long long llong;
llong fibonacci_sequence(cco_handle* z, unsigned n) {
assert (n < 95);
cco_context(z,
llong a, b, idx;
);
cco_routine(u,
u->a = 0;
u->b = 1;
for (u->idx = 2; u->idx < n; u->idx++) {
llong sum = u->a + u->b; // NB! locals only lasts until a cco_yield!
u->a = u->b;
u->b = sum;
cco_yield (sum);
}
cco_finish:
);
return 0;
}
int main(void) {
cco_handle z = 0;
printf("Fibonacci numbers:\n");
for (;;) {
llong x = fibonacci_sequence(&z, 30);
if (!z) break;
printf(" %lld", x);
}
puts("");
}
|