Skip to content

Instantly share code, notes, and snippets.

@arms22
Last active February 17, 2017 02:46
Show Gist options
  • Save arms22/617cdeb939d9fa53988129825b01ccfa to your computer and use it in GitHub Desktop.
Save arms22/617cdeb939d9fa53988129825b01ccfa to your computer and use it in GitHub Desktop.
#include <functional>
#include <stdio.h>
typedef struct {
    unsigned int state;
} co_state_t;
#define co_begin(T)\
    {\
        co_state_t *__current = &(T);\
        switch(__current->state){\
        case 0:;\
            __current->state = -1;
#define co_yield\
    do {\
            __current->state = __LINE__;\
            goto __co_suspend;\
        case __LINE__:;\
            __current->state = -1;\
    } while(0);
#define co_end\
        __co_suspend:;\
        }\
    }
void test1()
{
    static co_state_t t = {0};
    printf("\n%d ", t.state);
    co_begin(t) {
        printf("Hello ");
        co_yield;
        printf("World\n");
    }
    co_end;
}
int main(void){
    co_state_t t1;
   
    co_begin(t1) {
    }
    co_end;
   
    // Here your code !
    test1();
    test1();
    test1();
    test1();
    co_state_t t2 = {0};
    auto test2 = [&]{
        printf("\n%d ", t2.state);
        co_begin(t2) {
            printf("Hello ");
            co_yield;
            printf("World\n");
        }
        co_end;
    };
    test2();
    test2();
    test2();
    test2();
   
    co_state_t t3 = {0};
    int t3_count = 0;
    auto test3 = [&]{
        printf("\n%d ", t3.state);
        co_begin(t3) {
            while(1){
                printf("%d", ++t3_count);
                co_yield;
            }
        }
        co_end;
    };
    test3();
    test3();
    test3();
    test3();
    test3();
    test3();
    test3();
    test3();
    test3();
   
    auto test4 = [count=0,t4=co_state_t()]() mutable {
        printf("\n%d ", t4.state);
        co_begin(t4) {
            while(1){
                printf("%d", ++count);
                co_yield;
            }
        }
        co_end;
    };
    test4();
    test4();
    test4();
    test4();
    test4();
    test4();
    test4();
    test4();
    test4();
}
#include <stdio.h>
typedef struct {
    unsigned int state;
} co_state_t;
#define co_begin(T)\
    {\
        co_state_t *_co_current = &(T);\
        switch(_co_current->state){\
        if(0) case 0:
#define co_yield\
            _co_current->state = __LINE__;\
        }\
        else if(0) case __LINE__:\
        {\
            _co_current->state = -1;
#define co_end\
        else if(0) default:{}\
        }\
    }
int main(void){
    co_state_t t1 = {0};
    co_state_t t2 = {0};
   
    for(int i=0; i<5; i++){
    co_begin(t1) {
        printf("one\n");
        co_yield;
        printf("two\n");
        co_yield;
        printf("three\n");
    } co_end;
    co_begin(t2) {
        printf("1\n");
        co_yield;
        printf("2\n");
        co_yield;
        printf("3\n");
    } co_end;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment