Re: generator functions in another language



En Mon, 05 May 2008 00:09:02 -0300, hdante <hdante@xxxxxxxxx> escribió:

Isn't this guy a bot ? :-) It's learning fast. I believe there is a
"frame" in C, composed of its stack and globals. For generators in C,
you may look for "coroutines". For example, see:

http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html

A sample code follows:

#define crBegin static int state=0; switch(state) { case 0:
#define crReturn(i,x) do { state=i; return x; case i:; } while (0)
#define crFinish }
int function(void) {
static int i;
crBegin;
for (i = 0; i < 10; i++)
crReturn(1, i);
crFinish;
}

The "suspended state" is saved in the static variable. It's not
necessary to save the complete "frame", only the suspended state.

Quoting the author himself, "this is the worst piece of C hackery ever seen"

--
Gabriel Genellina

.



Relevant Pages

  • [PATCH 2/2] FRV: Improve signal handling
    ... if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) ... /* set up registers for signal handler */ ... struct rt_sigframe __user *frame; ... static int handle_signal(unsigned long sig, siginfo_t *info, ...
    (Linux-Kernel)
  • Re: GDB question
    ... GDB, with the break point at main. ... There's some relevant information at ... mainisn't really your program's first stack frame -- ...
    (comp.os.linux.development.apps)
  • Re: GDB question
    ... GDB, with the break point at main. ... The way GDB is deciding who holds the initial frame is linked to the ... ebp at 0xbfa86278, eip at 0xbfa8627c ...
    (comp.os.linux.development.apps)