Skip to content

Instantly share code, notes, and snippets.

@Yoxem
Last active April 11, 2019 16:57
Show Gist options
  • Save Yoxem/b9a2ce957b7ff3a3f1adb054b7e4bd93 to your computer and use it in GitHub Desktop.
Save Yoxem/b9a2ce957b7ff3a3f1adb054b7e4bd93 to your computer and use it in GitHub Desktop.
implementing closure in C (incompleted)
/*
a = 5;
b = 6;
A = lambda(x){
return A + a;
}
*/
typedef struct closure_A
{
int * a;
int * b;
void (*call)(void);
} closure_A;
/*
typedef struct closure_B
{
int * a_;
int * b_;
int * x_;
void (*call)(void);
} closure_B;
*/
int call_A(void *env, int * x){
closure_A * clousure_A = (closure_A*) env;
int result = *x + *(clousure_A->b);
return result;
}
int main() {
int *a = malloc(sizeof(int));
*a = 5;
int *b = malloc(sizeof(int));
*b = 5;
closure_A * clu_A = malloc(sizeof(closure_A));
clu_A->a = a;
clu_A->b = b;
clu_A->call = (void (*)(void)) call_A;
int *x = malloc(sizeof(int));
*x = 5;
((int(*)(void*,int*))clu_A->call)(clu_A, x);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment