Last active
October 4, 2020 15:32
-
-
Save matarillo/cf800a65f61f3946e04b5147d37e7bb9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class Bounce{ | |
public: | |
virtual ~Bounce(){}; | |
}; | |
class Num : public Bounce{ | |
public: | |
int value; | |
Num(int n) { | |
this->value=n; | |
} | |
~Num(){}; | |
}; | |
class Closure : public Bounce{ | |
public: | |
Bounce* (*fun)(int); | |
int arg; | |
Closure(Bounce*(*f)(int), int n):fun(f),arg(n){}; | |
Bounce* operator()() | |
{ | |
return (*fun)(arg); | |
} | |
~Closure(){}; | |
}; | |
int trampoline(Bounce* b) | |
{ | |
while (Closure* f=dynamic_cast<Closure*>(b)){ | |
b = (*f)(); | |
delete f; | |
} | |
int r=((Num*)b)->value; | |
delete (Num*)b; | |
return r; | |
} | |
Bounce* loop(int n) | |
{ | |
int x[10]; | |
if (n == 12345678) { | |
return new Num(n); | |
} | |
if(!(n%1000)) { | |
cout << "."; | |
return new Closure(loop,++n); | |
} | |
else return loop(++n); | |
} | |
int main() | |
{ | |
int t = trampoline(loop(0)); | |
cout << endl; | |
cout << t << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment