Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. struct CONTINUATION{
  6.     struct CONTINUATION (*FUNC)(int);
  7.     int ARG;
  8. };
  9.  
  10. typedef struct CONTINUATION CONT;
  11.  
  12. CONT bounce1(int arg);
  13. CONT bounce2(int arg);
  14. CONT bounce3(int arg);
  15.  
  16. void trampoline(CONT cont){
  17.     if (cont.FUNC != NULL){
  18.         trampoline(cont.FUNC(cont.ARG));
  19.     }
  20. }
  21.  
  22. CONT bounce3(int arg){
  23.     CONT next;
  24.     next.FUNC = bounce1;
  25.     next.ARG = abs(arg) - 3;
  26.     printf("Bounce3: %d -> %d\n", arg, next.ARG);
  27.  
  28.     return next;
  29. }
  30.  
  31. CONT bounce2(int arg){
  32.     CONT next;
  33.     next.FUNC = bounce3;
  34.     next.ARG = arg / 2;
  35.     printf("Bounce2: %d -> %d\n", arg, next.ARG);
  36.  
  37.     return next;
  38. }
  39.  
  40. CONT bounce1(int arg){
  41.     CONT next;
  42.     next.FUNC = bounce2;
  43.     next.ARG = arg * 3;
  44.     printf("Bounce1: %d -> %d\n", arg, next.ARG);
  45.  
  46.     if (arg * 5 == 1 || arg * 5 == 0){
  47.         next.FUNC = NULL;
  48.     }
  49.     return next;
  50. }
  51.  
  52. int main() {
  53.     CONT init;
  54.     init.FUNC = bounce1;
  55.     init.ARG = 1;
  56.     trampoline(init);
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement