views:

302

answers:

4

Are there any compilers out there for function or lisp-ish languages that compile to idiomatic C? Most compilers out there seem to provide something resembling a machine language composed of C macros. I'm wondering if there is anything out there that can produce readable C code based on a higher-level language.

A: 

I only found a C->C++ converter. I'm not sure that you can convert most functional languages to LISP. The structure and syntax doesn't match up very well. I'm sure that you could find many converters for C++ though.

monksy
+4  A: 

For Common Lisp there is currently only one widely used compiler that compiles to C: ECL. You would need to look if the output it is readable enough.

Years ago a company sold a Common Lisp compiler for exactly this purpose: compiling Lisp to 'maintainable' C. The compiler was very expensive. It is no longer available, but it still seems to be used in one or two companies: Lisp to C .

Rainer Joswig
A: 

The haskell compiler built-in compiles to C. Although IIRC it isn't that pretty looking.
(Checked, 162 lines for main = putStrLn "Hello, World!"

Edit: GHC of course.

codebliss
There are several Haskell compilers and more than one that can compile to C, so it would be helpful if you stated which one you where talking about. If you're referring to GHC, then yes, its generated C is not pretty and far from idiomatic. JHC is much better in this respect. GHC is trying hard these days to move away from compiling via C though. They're working on a new fancy backend which will be able to do all the low level optimizations they've used the C compiler for before.
svenningsson
I fail to see why I deserve a downvote, as my answer is not wrong?
codebliss
I didn't downvote you, but sorry, it _is_ wrong. I specifically asked for "idiomatic C", and you answered with something that generates C that "isn't that pretty looking", the opposite of what i asked for. (But thanks anyway!)
Steve
My bad, I literally didn't see that word there at all.
codebliss
+5  A: 

Given that you tagged this question with Haskell I'll give a Haskell answer. JHC is a Haskell compiler which produces fairly idiomatic ansi-C. It's an optimizing whole-program compiler that can produce incredibly fast binaries. I'm not sure how stable it is at the moment though, and there seems to be limits to how big programs it can handle. But don't take my word for it, check it out yourself.

svenningsson
I gave the point to Lisp-to-C, since this project was designed specifically for my question, but it's unfortunate that it's no longer available. I was really considering giving you the point, since JHC seems to generate pretty friendly code as you suggested. If only I could give multiple points. Anyways, thanks for pointing me to JHC, it's a very nice project. I generated a simple executable that was only 16K!
Steve