summaryrefslogtreecommitdiffhomepage
path: root/misc/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-02-25 12:09:11 +0100
committerTyge Løvset <[email protected]>2023-02-25 12:09:11 +0100
commit25d3a00cf37496f5be2f8a7f1aea42fc4d596ab2 (patch)
treee5af1d5d3872fe1380d6325dde6bf60a2917cc62 /misc/examples
parent0befe31c0f7c2cc86ba872073af610b3e0d9c9fb (diff)
downloadSTC-modified-25d3a00cf37496f5be2f8a7f1aea42fc4d596ab2.tar.gz
STC-modified-25d3a00cf37496f5be2f8a7f1aea42fc4d596ab2.zip
Renamed algo/ccoro.h => algo/coroutine.h. Using cco_ as prefix. Changed ccoro_execute(c) => cco_begin(c); ... cco_end(); (was required).
Diffstat (limited to 'misc/examples')
-rw-r--r--misc/examples/coread.c18
-rw-r--r--misc/examples/coroutines.c (renamed from misc/examples/cosub.c)32
2 files changed, 26 insertions, 24 deletions
diff --git a/misc/examples/coread.c b/misc/examples/coread.c
index a3eac7f9..6b0bfb37 100644
--- a/misc/examples/coread.c
+++ b/misc/examples/coread.c
@@ -1,30 +1,30 @@
#include <stc/cstr.h>
-#include <stc/algo/ccoro.h>
+#include <stc/algo/coroutine.h>
#include <errno.h>
// Read file line by line using coroutines:
struct file_nextline {
const char* filename;
- int ccoro_state;
+ int cco_state;
FILE* fp;
cstr line;
};
cstr file_nextline(struct file_nextline* U)
{
- ccoro_execute(U,
+ cco_begin(U)
U->fp = fopen(U->filename, "r");
U->line = cstr_NULL;
while (cstr_getline(&U->line, U->fp))
- ccoro_yield (cstr_clone(U->line));
+ cco_yield(cstr_clone(U->line));
- ccoro_final: // ccoro_final is needed to support ccoro_stop.
+ cco_final: // cco_final is needed to support cco_stop.
printf("finish\n");
cstr_drop(&U->line);
fclose(U->fp);
- );
+ cco_end();
return cstr_NULL;
}
@@ -33,11 +33,11 @@ int main(void) {
struct file_nextline z = {__FILE__};
int n = 0;
do {
- c_with (cstr line = file_nextline(&z), ccoro_alive(&z), cstr_drop(&line)) {
+ c_with (cstr line = file_nextline(&z), cco_alive(&z), cstr_drop(&line)) {
printf("%3d %s\n", ++n, cstr_str(&line));
// stop after 15 lines:
- if (n == 15) file_nextline(ccoro_stop(&z));
+ if (n == 15) file_nextline(cco_stop(&z));
}
- } while (ccoro_alive(&z));
+ } while (cco_alive(&z));
}
diff --git a/misc/examples/cosub.c b/misc/examples/coroutines.c
index 87dcb9a1..d3295102 100644
--- a/misc/examples/cosub.c
+++ b/misc/examples/coroutines.c
@@ -1,4 +1,4 @@
-#include <stc/algo/ccoro.h>
+#include <stc/algo/coroutine.h>
#include <stdio.h>
#include <stdint.h>
@@ -8,24 +8,24 @@ typedef long long intll_t;
struct fibonacci {
int n;
- int ccoro_state;
+ int cco_state;
intll_t a, b, idx;
};
intll_t fibonacci(struct fibonacci* F) {
assert (F->n < 95);
- ccoro_execute(F,
+ cco_begin(F);
F->a = 0;
F->b = 1;
for (F->idx = 0; F->idx < F->n; F->idx++) {
- ccoro_yield (F->a);
- intll_t sum = F->a + F->b; // NB! locals only lasts until next ccoro_yield!
+ cco_yield(F->a);
+ intll_t sum = F->a + F->b; // NB! locals only lasts until next cco_yield!
F->a = F->b;
F->b = sum;
}
- ccoro_final:
- );
+ cco_final:
+ cco_end();
return -1;
}
@@ -35,20 +35,22 @@ intll_t fibonacci(struct fibonacci* F) {
struct iterate {
int max_x, max_y;
- int ccoro_state;
+ int cco_state;
int x, y;
};
bool iterate(struct iterate* I, struct fibonacci* F) {
- ccoro_execute(I,
- for (I->x = 0; I->x < I->max_x; I->x++)
- for (I->y = 0; I->y < I->max_y; I->y++)
+ cco_begin(I);
+ for (I->x = 0; I->x < I->max_x; I->x++) {
+ for (I->y = 0; I->y < I->max_y; I->y++) {
if (I->x == 1 && I->y == 1)
- ccoro_yield_call (F, fibonacci(F), true);
+ cco_yield_coroutine(F, fibonacci(F), true);
else
- ccoro_yield (true);
- ccoro_final:
- );
+ cco_yield(true);
+ }
+ }
+ cco_final:
+ cco_end();
return false;
}