Skip to content

Instantly share code, notes, and snippets.

@matarillo
Last active October 4, 2020 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matarillo/cf800a65f61f3946e04b5147d37e7bb9 to your computer and use it in GitHub Desktop.
Save matarillo/cf800a65f61f3946e04b5147d37e7bb9 to your computer and use it in GitHub Desktop.
#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