CoffeeScript is a language that transpiles to JavaScript, with a clean syntax, inspired by Ruby. Is there a similar language that transpiles to C, allowing for more readable code without compromising on performance? If nothing like that exists, is there a good reason for not creating it?

  • 10
    Note, however, that "compiles to C" is not synonymous with "as fast as the original C programs". When something compiles to C, it'll often generate code rather (or completely) different from what any normal person would write in C. It might easily be considerably slower (or sometimes faster) than what you'd normally write by hand. – Jerry Coffin Apr 10 '12 at 3:48
  • 4
    What I don't like about any of these answers, is that they propose totally different languages, such as Vala. The thing about CoffeeScript is, that it IS Javascript, but with a different syntax. – Prof. Falken May 20 '12 at 15:53
  • 2
    pascal or fortran? – user40980 Mar 8 '13 at 13:02
  • 6
    I really wish "transcompile" (the word) would just die. It's "compile" and "compiler". You don't need a new word just because the output language isn't x86 (and if you think you do, you don't understand compilers). – Leushenko Feb 18 '15 at 13:48
  • 2
    The word transpile means (or has lately come to mean) to translate code to the source code of another language with the same semantics, typically statement for equivalent statement (allowing a source map to also be created). Nobody is claiming that transpilers do something different to compilers. Transpilers are a specific type of compiler. – Carl Smith Aug 24 '19 at 22:26

CoffeeScript compiles to JavaScript for a very simple reason, JavaScript is the de facto client side language and it would be unreasonable to expect browser vendors to natively support CoffeeScript, when all it offers is an alternative syntax.

In a very similar manner, the main point of high level language to C translators is immediate portability, as there's a C compiler for almost every platform and an abundance of C libraries. Vala, for example, was designed to:

  1. be a compiler for the GObject,
  2. build native executables (through the machine's C compiler),
  3. automate reference counting, and
  4. still be accessible to GNOME C programmers

GNOME is a traditionally C oriented project and GObject specifically is written in C, Vala wouldn't probably find much love amongst GNOME developers if it compiled to machine code, regardless of it's friendlier nature (and syntax). Not everyone seemed to like the syntax, to the point that another language, Genie, was build to improve upon it.

For a C++ example, Facebook developed HipHop, a PHP to C++ translator. They were trying to solve a very specific issue, CPU usage, without having to replace all their PHP code and re-train their engineers (or worst, replace them). This is a far more specific example, as Facebook scalability issues are, well, unique, and again having access to the intermediate C++ code can be useful, as PHP extensions are written in C and C++.

So a translator from a high level language to another is a good idea mostly when you access to the intermediate code is required. For CoffeeScript, the JavaScript code is necessary because of its wide browser adoption, and for Vala, Genie and HipHop because of the existing codebase. Obviously having access to the intermediate code means that you can further optimize it if need be.

But generally speaking, it wouldn't be such a good idea to build a language that translates to C, or any other language, if you didn't have any use of the resulting code. There are so many languages out there, if you can't cope with C, just pick an other. Coincidentally the first C++ compiler written by Bjarne Stroustrup, CFront, was a C with Classes to C translator, but that was mainly because as a new language, it was impossible to bootstrap C with Classes.

| improve this answer | |

I'm going to cover a few points Yannis Rizos didn't in his otherwise great answer.

Yes, many languages exist. C is a common target for compiler back-ends as it's incredibly portable and heavily optimized, although with LLVM there's not much point to it.

A few implementations I know that do this are:

  • C++ (At least in the early days)
  • GHC Haskell (Although the main code generator is C--)
  • Gambit/Chicken/Bigloo Scheme
  • ECL (Common Lisp)
  • Perl
  • Vala & Genie

as fast as original C programs

No, just because it uses C as an intermediate language doesn't mean you will reach its speed. The reason C is fast is because of the method of writing the code which is obviously different for other languages. It's just a portable assembly, nothing special.

| improve this answer | |
  • 1
    C is very far away from the assembly... – Display Name Jun 23 '14 at 12:34
  • 1
    Some people think of C as "high level assembler". It's pretty close to the machine, but of course it's not a bunch of opcodes. – dstromberg Sep 19 '15 at 17:26

Rock is an ooc compiler that generates C99 source. The ooc-lang is a programming language with objects, first-class functions, and pink unicorns. The ooc is a dynamic-language and walks so far away. It generates fatter and slower c codes. u need modified more to suite your requirement. But it is a good start point.

| improve this answer | |

Vala and Genie are both languages that compile into C. haxe compiles into C++, but I'm not sure that's what you want.

| improve this answer | |
  • 2
    would you mind expanding a bit on what each of these resources have and why do you recommend these as answering the question asked? "Link-only answers" are not quite welcome at Stack Exchange – gnat Oct 12 '13 at 20:14

OCaml can compile to bytecode, to native code, can be interpreted directly, or can compile to C.

| improve this answer | |
  • 1
    would you mind explaining more on what it does and why do you recommend it as answering the question asked? "Link-only answers" are not quite welcome at Stack Exchange – gnat Oct 12 '13 at 20:13
  • 3
    I fail to see how my answer is a "link-only answer". Actually, I could remove the link and it would still be a valid answer. The question is "Is there a language that does X" and my answer is "language Y does X". This comment also applies to Bilijk's answer. – barjak Oct 14 '13 at 11:57
  • Bjarne Stroustrop's original C++ compiler, "cfront", compiled C++ to C, which it would then optionally run the C compiler against to produce object code. C++ is about as "non-theoretical" as you can get :-)
  • The Unix "yacc" and GNU "Bison" compiler-compilers translate their input languages to C. Many, many sophisticated systems have been written with them.
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.