Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- struct CONTINUATION{
- struct CONTINUATION (*FUNC)(int);
- int ARG;
- };
- typedef struct CONTINUATION CONT;
- CONT bounce1(int arg);
- CONT bounce2(int arg);
- CONT bounce3(int arg);
- void trampoline(CONT cont){
- if (cont.FUNC != NULL){
- trampoline(cont.FUNC(cont.ARG));
- }
- }
- CONT bounce3(int arg){
- CONT next;
- next.FUNC = bounce1;
- next.ARG = abs(arg) - 3;
- printf("Bounce3: %d -> %d\n", arg, next.ARG);
- return next;
- }
- CONT bounce2(int arg){
- CONT next;
- next.FUNC = bounce3;
- next.ARG = arg / 2;
- printf("Bounce2: %d -> %d\n", arg, next.ARG);
- return next;
- }
- CONT bounce1(int arg){
- CONT next;
- next.FUNC = bounce2;
- next.ARG = arg * 3;
- printf("Bounce1: %d -> %d\n", arg, next.ARG);
- if (arg * 5 == 1 || arg * 5 == 0){
- next.FUNC = NULL;
- }
- return next;
- }
- int main() {
- CONT init;
- init.FUNC = bounce1;
- init.ARG = 1;
- trampoline(init);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement