Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheme to C translator

Tags:

c

scheme

r6rs

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.

like image 646
alinsoar Avatar asked Oct 30 '25 11:10

alinsoar


2 Answers

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:

  1. get CHICKEN;
  2. build & install it with the appropriate 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);
  3. stash your code in a file I'll call f.scm.
  4. if you want to see the C code, compile with chicken f.scm which will produce a few hundred lines of incomprehensible C;
  5. if you want just the executable, use 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:

  • if you want a Scheme system which will allow you talk to code written in C, then you probably want a system with an FFI, not one that compiles to C;
  • if you want a Scheme system which will create native executables, then you probably want, well, a Scheme system which will create native executables, not one which compiles to C.

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.

Basically no scheme to C translators will do what you want. They create hideous code not meant to be read and they rely on the underlying C compiler to do much of the optimization. Chicken and Gambit make use of header files while I have Stalin, which does not but it is based on R4RS instead of R5RS and later.

You are probably better off reading Abdulaziz Ghuloum's paper An Incremental Approach to Compiler Construction (PDF) or perhaps Matt Mights articles on parsing, continuations and compilations. Longer down he actually has a Scheme to C and Scheme to Java with different approaches to closure conventions. In the end nothing beats doing it yourself so have a go!

like image 39
Sylwester Avatar answered Nov 01 '25 03:11

Sylwester



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!