Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C coroutines

Name: Anonymous 2019-02-08 10:27

#ifndef COROUTINE_H
#define COROUTINE_H

#include <stdlib.h>

/*
* `scr' macros for static coroutines.
*/

#define scrBegin static int scrLine = 0; switch(scrLine) { case 0:;
#define scrFinish(z) } return (z)
#define scrFinishV } return

#define scrReturn(z) \
do {\
scrLine=__LINE__;\
return (z); case __LINE__:;\
} while (0)
#define scrReturnV \
do {\
scrLine=__LINE__;\
return; case __LINE__:;\
} while (0)

/*
* `ccr' macros for re-entrant coroutines.
*/

#define ccrContParam void **ccrParam

#define ccrBeginContext struct ccrContextTag { int ccrLine
#define ccrEndContext(x) } *x = (struct ccrContextTag *)*ccrParam

#define ccrBegin(x) if(!x) {x= *ccrParam=malloc(sizeof(*x)); x->ccrLine=0;}\
if (x) switch(x->ccrLine) { case 0:;
#define ccrFinish(z) } free(*ccrParam); *ccrParam=0; return (z)
#define ccrFinishV } free(*ccrParam); *ccrParam=0; return

#define ccrReturn(z) \
do {\
((struct ccrContextTag *)*ccrParam)->ccrLine=__LINE__;\
return (z); case __LINE__:;\
} while (0)
#define ccrReturnV \
do {\
((struct ccrContextTag *)*ccrParam)->ccrLine=__LINE__;\
return; case __LINE__:;\
} while (0)

#define ccrStop(z) do{ free(*ccrParam); *ccrParam=0; return (z); }while(0)
#define ccrStopV do{ free(*ccrParam); *ccrParam=0; return; }while(0)

#define ccrContext void *
#define ccrAbort(ctx) do { free (ctx); ctx = 0; } while (0)

#endif /* COROUTINE_H */

Name: Anonymous 2019-02-08 11:03

FREEZE MY ANUS

Name: Anonymous 2019-02-08 12:42

Name: Anonymous 2019-02-08 13:28

>>3
he's cool. he has one other article about preprocessor metaprogramming: https://www.chiark.greenend.org.uk/~sgtatham/mp/

take notes FrozenAnus, that's how you do it in a readable/understandable way

Name: Anonymous 2019-02-09 12:36

Nice. TinyProg need more posts liek that.

Name: Anonymous 2019-02-09 15:54

>>5

Moar poasts

Name: Anonymous 2019-02-09 18:53

Name: Anonymous 2019-02-09 20:19

>>7
Coroutines are great and libcello looks fun.
But why would you want to bring the insanity of exceptions to a language that, despite its flaws, at least is not afflicted with them?

Name: Anonymous 2019-02-09 20:35

>>8
exceptions suck, but error-handling by return values in complex software sucks more. there are better ways but I guess bolting multiple returns, either types, option types or monadic errors onto C is harder than longjmp

Name: Anonymous 2019-02-09 21:29

>>9
setjmp?

Name: Anonymous 2019-02-09 23:30

#define dubs(n) n##n
#define claim(x) return (x)

#define postBegin int main() {
#define postFinish }

postBegin
claim(dubs(1));
postFinish

Name: Anonymous 2019-02-10 11:38

You can now run and debug C in a browser.
https://vasyop.github.io/miniC-hosting/

Name: Anonymous 2019-02-10 20:00

>>9
Either types are monadic errors. Option types are isomorphic to Either Unit.

Name: Anonymous 2019-02-11 1:40

postBegin
claim(dubs(12));
postFinish

hmm

Name: Anonymous 2019-02-11 7:00

>>13
wrong. either types can be monadic errors, but it is trivial to implement them in a way that doesn't conform to the definition of a monad. just a tagged union is enough to have either types. of course having them be a proper monad is good and makes error handling easier, but it is still possible to do it the other way.

Name: Anonymous 2019-02-13 14:02

This magic can't exist in D, because it has no preprocessors.
https://dlang.org/articles/pretod.html

Name: Anonymous 2019-02-14 16:02

I hate C preprocessor so much.

Name: Anonymous 2019-02-16 5:20

>>17
baka

Don't change these.
Name: Email:
Entire Thread Thread List