Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

CoffeeScript is a language with a very clean Ruby-like syntax that transcompiles to JavaScript. Does the same thing exists with C? Then writing more readable and as fast as original C programs would be possible. If it doesn't exist, do you think that it would be a good idea?

share|improve this question

closed as not constructive by gnat, Glenn Nelson, Martijn Pieters, MichaelT, Dynamic Mar 17 '13 at 22:04

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

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
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
C programs are fast not because they are written in C, but because they are, well, C-style programs. The language is very close to assembly, and has close to zero "high-level magic" - the thing responsible for readability. With C, you get close-to-assembly speed, but you also get close-to-assembly readability. – dasblinkenlight Mar 8 '13 at 11:45
pascal or fortran? – user40980 Mar 8 '13 at 13:02
@MichaelT: I wanted to mention Pascal but then saw your comment. Maybe one could add Ada to list. – Giorgio Mar 16 '13 at 15:37
up vote 17 down vote accepted

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.

share|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.

share|improve this answer
C is very far away from the assembly... – Sarge Borsch Jun 23 '14 at 12:34
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

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

share|improve this answer
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

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.

share|improve this answer

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

share|improve this answer
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
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.
share|improve this answer

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