Skip to content

Instantly share code, notes, and snippets.

@jspahrsummers
Created May 23, 2011 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jspahrsummers/986175 to your computer and use it in GitHub Desktop.
Save jspahrsummers/986175 to your computer and use it in GitHub Desktop.
Tentative coroutine syntax
#define coroutine(ARGS) \
__block unsigned long ext_coroutine_line_ = 0; \
\
return ^(ARGS){ \
for (;; ext_coroutine_line_ = 0) \
switch (ext_coroutine_line_) \
default:
#define return_coroutine \
}
#define yield \
if ((ext_coroutine_line_ = __LINE__) == 0) \
case __LINE__: \
; \
else \
return
id myCoroutine =
^{
__block int myVariable = 5;
coroutine(int n) {
// do stuff
yield 5 + n;
// do more stuff
yield ++myVariable;
}
return_coroutine;
}();
@jiangyewen
Copy link

jiangyewen commented Sep 30, 2016

this is very tricky And obscure...
I was trying to expand the macros, only to find it was more confusing...
//===before expand

__block int i;

    int (^myCoroutine)(void) = coroutine(void)({
        for (i = 0;i < 3;++i) {
            NSLog(@"%@",@(i));
            yield i;
        }
    });

//after expanding

int (^myCoroutine)(void) =  ^{ 
     __block unsigned long ext_coroutine_line_ = 0; 

    ^(void) { 
        for (;; ext_coroutine_line_ = 0) 
            switch (ext_coroutine_line_) 
                default: 
                for (i = 0;i < 3;++i) {
                    if ((ext_coroutine_line_ = __LINE__) == 0)
                        case __LINE__: 
                    ; 
                    else 
                        return i;
        } 
            } 
    }()


  ({
        for (i = 0;i < 3;++i) {
            if ((ext_coroutine_line_ = __LINE__) == 0)
                case __LINE__: 
                ; 
            else 
                return i;
        }
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment