summaryrefslogtreecommitdiffhomepage
path: root/misc/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-02-23 20:40:48 +0100
committerTyge Løvset <[email protected]>2023-02-23 20:40:48 +0100
commit95656f384672748dcf02c256e23534728bef80d9 (patch)
tree8ae535749a40f2a2f5c7a716bd339b177f657bef /misc/examples
parentfadfc47abd5f8fe0757244b3b9eb5a1ae78e0dce (diff)
downloadSTC-modified-95656f384672748dcf02c256e23534728bef80d9.tar.gz
STC-modified-95656f384672748dcf02c256e23534728bef80d9.zip
Replaced coroutine.h with ccoro.h. Stackbased only.
Diffstat (limited to 'misc/examples')
-rw-r--r--misc/examples/cofib.c31
-rw-r--r--misc/examples/coread.c32
2 files changed, 34 insertions, 29 deletions
diff --git a/misc/examples/cofib.c b/misc/examples/cofib.c
index ea37669a..b58775ae 100644
--- a/misc/examples/cofib.c
+++ b/misc/examples/cofib.c
@@ -1,27 +1,29 @@
-#include <stc/algo/coroutine.h>
+#include <stc/algo/ccoro.h>
#include <stdio.h>
#include <stdint.h>
// Use coroutine to create a fibonacci sequence generator:
typedef long long llong;
+struct fibonacci {
+ int n;
+ int ccoro_state;
+ llong a, b, idx;
+};
-llong fibonacci_sequence(cco_handle* z, unsigned n) {
- assert (n < 95);
+llong fibonacci(struct fibonacci* U) {
+ assert (U->n < 95);
- cco_context(z,
- llong a, b, idx;
- );
- cco_routine(U,
+ ccoro_execute(U,
U->a = 0;
U->b = 1;
- for (U->idx = 0; U->idx < n; U->idx++) {
- cco_yield (U->a);
- llong sum = U->a + U->b; // NB! locals only lasts until next cco_yield!
+ for (U->idx = 0; U->idx < U->n; U->idx++) {
+ ccoro_yield (U->a);
+ llong sum = U->a + U->b; // NB! locals only lasts until next ccoro_yield!
U->a = U->b;
U->b = sum;
}
- cco_finish:
+ ccoro_final:
);
return -1;
@@ -29,11 +31,12 @@ llong fibonacci_sequence(cco_handle* z, unsigned n) {
int main(void) {
- cco_handle z = 0;
printf("Fibonacci numbers:\n");
+ struct fibonacci fib = {.n = 14};
+
for (;;) {
- llong x = fibonacci_sequence(&z, 14);
- if (!z) break;
+ llong x = fibonacci(&fib);
+ if (!ccoro_alive(&fib)) break;
printf(" %lld", x);
}
puts("");
diff --git a/misc/examples/coread.c b/misc/examples/coread.c
index e65ab533..a3eac7f9 100644
--- a/misc/examples/coread.c
+++ b/misc/examples/coread.c
@@ -1,41 +1,43 @@
#include <stc/cstr.h>
-#include <stc/algo/coroutine.h>
+#include <stc/algo/ccoro.h>
#include <errno.h>
// Read file line by line using coroutines:
-cstr file_nextline(cco_handle* z, const char* name)
+struct file_nextline {
+ const char* filename;
+ int ccoro_state;
+ FILE* fp;
+ cstr line;
+};
+
+cstr file_nextline(struct file_nextline* U)
{
- cco_context(z,
- FILE* fp;
- cstr line;
- );
- cco_routine(U,
- U->fp = fopen(name, "r");
+ ccoro_execute(U,
+ U->fp = fopen(U->filename, "r");
U->line = cstr_NULL;
while (cstr_getline(&U->line, U->fp))
- cco_yield (cstr_clone(U->line));
+ ccoro_yield (cstr_clone(U->line));
- cco_finish: // cco_finish is needed to support cco_stop.
+ ccoro_final: // ccoro_final is needed to support ccoro_stop.
printf("finish\n");
cstr_drop(&U->line);
fclose(U->fp);
);
-
return cstr_NULL;
}
int main(void) {
- cco_handle z = 0;
+ struct file_nextline z = {__FILE__};
int n = 0;
do {
- c_with (cstr line = file_nextline(&z, __FILE__), z, cstr_drop(&line)) {
+ c_with (cstr line = file_nextline(&z), ccoro_alive(&z), cstr_drop(&line)) {
printf("%3d %s\n", ++n, cstr_str(&line));
// stop after 15 lines:
- if (n == 15) file_nextline(cco_stop(&z), __FILE__);
+ if (n == 15) file_nextline(ccoro_stop(&z));
}
- } while (z);
+ } while (ccoro_alive(&z));
}