Skip to content

Instantly share code, notes, and snippets.

@jdpage
Created April 19, 2013 03:52
Show Gist options
  • Save jdpage/5418032 to your computer and use it in GitHub Desktop.
Save jdpage/5418032 to your computer and use it in GitHub Desktop.
C continuation passing
#include <stdio.h>
#include <stdlib.h>
#define NEXT(n) ((n)->next(n))
typedef struct co_fib {
int (*next)(struct co_fib *);
int a;
int b;
} * co_fib_t;
int co_fib_next(struct co_fib *state) {
int t = state->a + state->b;
state->a = state->b;
state->b = t;
return state->a;
}
co_fib_t co_fib_new() {
co_fib_t f = (co_fib_t)malloc(sizeof(struct co_fib));
f->a = 0;
f->b = 1;
f->next = co_fib_next;
}
void co_fib_del(co_fib_t f) {
free(f);
}
int main() {
co_fib_t f = co_fib_new();
int k;
for (k = 0; k < 30; k++) {
printf("%d\n", NEXT(f));
}
co_fib_del(f);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment