I tried to generate C code starting from a scheme function and I do not manage to find any translator from scheme to C. I tried to convert this function to C.
(define f
(lambda(n)
(if (= n 0) 1
(* n (f (- n 1))))))
(display (f 10))
(newline)
I tried to use gambit (gsc) and it generates a C file that looks merely like a file to load in some interpreter, not a file containing a main
function that can be executed.
Is there some application that generates C code that can be directly executed? The functions from standard scheme library like display
should be linked with some object file.
EDIT:
My purpose is to understand the algorithms used by professional translators.
There are many such translators, dating back at least to the 1980s I think CHICKEN is a good current one.
If you want to use that:
make
incantation (this was painless for me on OSX, it should be very painless indeed on Linux therefore, although it may be harder on Windows);f.scm
.chicken f.scm
which will produce a few hundred lines of incomprehensible C;csc
to create it.There is an extensive manual which you will need to read if you want to do anything nontrivial, such as linking in C libraries or talking to Scheme code from C.
Without knowing what you are after, this smells as if it may be an XY problem. In particular:
There are many examples of each of these. Some of these systems may also compile to, or via, C, but one does not depend on the other.
Finally, if you want to understand how Scheme compilers which target C work (or how Scheme compilers which target any language, including assembler), then the traditional approach probably still works best: find a well-written one for which source is available, and read & tinker with its source code.
thank you. Can you provide some links to the internals of chicken, to understand the algorithms used to make the conversion? I wish I would read some artictles that teaches me how to implement myself the algorithms of conversion.
@alinsoar: not really. It will be as hairy as any other compiler I suspect: it just happens to target C. C is famously spoken of as a 'portable assembler': Scheme compilers which target C are an example of just that.
the internals of these interpreters and compilers is described in the book of Christian Queinnec, however it is interesting to follow that book looking in the same time at some concrete implementations.
@alinsoar: no: some particular example techniques for compiling and intepreting Scheme are described in that book: there is absolutely no reason at all why compilers need to follow those examples. It is perfectly legitimate to compile or interpret Scheme programs by invoking a djinn, for instance. That's certainly how I always do it.
@alinsoar: GR is a good example: some techniques from the early history of the theory may still be useful (I'm not sure though), but the techniques people use today to do calculations in GR are both widely varied and hugely developed from anything Einstein knew. Anyway, I'm done here.