yield equivalent in C/JavaScript?
Robin Becker
robin at jessikat.fsnet.co.uk
Sun Jun 30 16:05:37 EDT 2002
In message <20020630180139.GA38821 at hishome.net>, Oren Tirosh <oren-py-
l at hishome.net> writes
very smart! That Duff did it Fortran as I recall.
>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
>
>
--
Robin Becker
More information about the Python-list
mailing list