Emulating lambdas in C?

I should mention that I am generating C code, not manually. I say this because it does not really matter if there is a lot of code behind it, because the compiler has to get around this. Anyway, how would I go around emulating lambda in C? I thought I could just generate a function with some random name somewhere in the source code, and then call? I'm not too sure. I have not tried anything yet, because I wanted to bring this idea to its implementation.

Is there some kind of preprocessor directive that I can do, or some kind of macro that this cleaner will do? I was inspired by John Beale to try compiler development, and he seemed to use Lambdas in his Jai language. However, I think it does something where it generates bytecode, and then in C? I'm not sure.

Edit: I am working on a compiler, the compiler is just my project so that I am busy and also want to know more about compilers. I mainly use clang, I'm on Ubuntu 14.10. I do not have a garbage collection, but I wanted to try my hand at some kind of smart memory model with a pointer -y / rust / ARC for garbage collection, i.e. Little overhead. I chose C because I wanted to pat it more. My project is free software, just a hobby project.

+4
source share
1 answer

There are several ways to do this (“have” lambda in C). It is important to understand that lambdas give closure and that closures mix "code" with "data" (closed values); note that objects also mix “code” with “data”, and there is a similarity between objects and closures. See also this answer to Programmers .

C , . , , GTK: , , . ( C void*) .

C- ( , MELT, - Linux- ++ , dlopen -s), , .

static, .

lambda.h , trampoline ( , , ). JIT ( libjit, GNU lightning, LLVM, asmjit,....) . . libffi (, ).

, , ( GC), GC. ++ 11 - ( ++ 11). , C, , , Boehm ( dlopen), GC-ed. ( GC, Ravenbrook MPS unmaintained Qish...) , C .

(, Lisp; if Scheme SICP) Queinnec Lisp ( , ).

+8

All Articles