yield equivalent in C/JavaScript?
Oren Tirosh
oren-py-l at hishome.net
Sun Jun 30 14:01:39 EDT 2002
On Sat, Jun 29, 2002 at 07:12:46PM +0100, Robin Becker wrote:
> Is there any way to do yielding easily in C? I have a bunch of programs
> which are normally connected together using unix pipes. I would like to
> merge them and the problem is then how to get them to communicate nicely
> using a single process.
It's possible to implement this in C using Duff's Device:
#define BEGIN_GENERATOR static int _GenState=0; \
switch(_GenState) { \
case 0:
#define END_GENERATOR } \
_GenState = 0;
#define YIELD(x) _GenState=__line__; return (x); case __line__:
int generator(arguments) {
static int foo, bar, etc; /* Make all variables static */
BEGIN_GENERATOR
for(...) {
if(...) {
...
YIELD(x)
...
} else {
...
}
...
YIELD(y)
...
}
END_GENERATOR
}
If you want this to be reentrant use a struct passed as an argument
to the function instead of static variables.
Oren
More information about the Python-list
mailing list