37

I've been looking for a language that provides the same functionality that Coffeescript has, but for C/C++. I mean a language that converts the code into C, but readable, just like CoffeeScript converts to Javascript, readable and all.

Ian
  • 4,195
  • 2
  • 25
  • 32

8 Answers8

23

I think this is possible, and even desirable (I grudgingly deal with C++ when writing Node.js native modules), but more challenging than with a higher-level language like JavaScript.

What you're asking for is a language that would provide syntactic sugar without sacrificing performance or flexibility. Some syntactic sugars (say, syntactic whitespace or Ruby-style def/end blocks instead of curly braces) would be trivial to add. But adding anything more advanced, you'd run into two major hurdles: static typing, and garbage collection.

For instance, let's say that you wanted to add implicit returns. It seems like a small feature, but think about it: In order for the feature to be useful, you'd have to—at the very least—throw a compile-time error when the value of the last expression doesn't match the function's return type. That means that your compiler needs to inspect a line like

a->b

and figure out what type it is. That's possible in principle, but it's a heck of a lot more work than the CoffeeScript compiler does.

Or say you added list comprehensions. That means you're allocating an array whose length isn't known at compile-time, which means you'll need to later deallocate it yourself. So the syntactic sugar could actually hurt you. The rule "If you malloc it, you free it" doesn't work if the compiler is adding in the malloc for you, unless it can figure out where to put the free (which, again, is generally possible but would take a lot of work).

So, while I'd love to someone give C++ the CoffeeScript treatment, I don't expect it to happen any time soon—if ever. I think it's more likely that the world will eventually move on to something like D or Go for system-level programming.

Trevor Burnham
  • 76,828
  • 33
  • 160
  • 196
  • 6
    I'm not sure how this answers the question, and I think you're confusing things. CoffeeScript generates javascript files that are relatively readable and have no special runtime. The aim here should be the same: generating C++ from a higher language that is semantically close to it. Adding a runtime that does garbage collection is off-topic. You have to accept some things are semantically tied to C++ and can't be removed, but you can still make a language that is easier to write and read. ... – Steph Thirion Aug 15 '11 at 22:40
  • 3
    ... Even the lower hanging fruits like not having to repeat yourself in header/implementation files are already a huge win, but more complex problems can be solved. Case in point, you mentioned static typing, it's been improved without completely removing it by some projects mentioned in this thread, and if you read their docs you will see many other examples of things they solved brilliantly (vala/genie without GObject, ooc (unfortunately they went the garbage collection road)), or inv which Grigory Javadyan mentioned in the comments). – Steph Thirion Aug 15 '11 at 22:40
  • This comment was true when posted. But now C++14 can return auto as in "auto f () { return 42; }". decltype and auto should be enough. Figuring out when to free() isn't just a syntax issue. Coffeescript++ would probably provide syntax for smart pointers and value semantics. I imagine a list comprehension would become a vector value and its destruction would be managed the same as in C++. Coffeescript++ need only implement the logic of the std smart pointers to do this. – Samuel Danielson Mar 05 '16 at 05:18
13

I think OOC is probably the closest thing to Coffeescript for C. It's a programming language with a lot of the features you'd expect from dynamic languages (objects, first class functions, clean syntax) that compiles directly into C99.

http://ooc-lang.org/

maxhawkins
  • 878
  • 6
  • 18
  • 1
    I like it! Looks very promising. – Ian Jul 11 '11 at 18:06
  • 4
    ooc is super cool, but unfortunately, it adds garbage collection. So it's not as flexible and portable as the CoffeeScript approach, and kind of unusable for high performance code like games. Apparently, GC can be disabled, but currently "if you use the current SDK without the GC, it will just leak through every hole": http://docs.ooc-lang.org/compilers/no-gc.html – Steph Thirion Sep 10 '11 at 02:45
11

SugarCpp is a language which can compile to C++11. It should be what you are looking for. Visit https://github.com/curimit/SugarCpp for more details.

yangzh
  • 391
  • 1
  • 3
  • 6
11

One item missing from Jacindas list you might want to know about: Vala/Genie is a compiler targetting C with the GObject library implementing objects, written by Gnome. Vala is a C#-like syntax, and Genie a Python-like syntax, but for the rest they are the same system. It was actually created because bare C + GObject became too much of a pain to work with for the Gnome guys. Vala does objects and automatic memory management based on reference counting or ownership tracking, and a lot of other things you'd expect in a C# like language.

As for the CoffeeScript-like property, I just saw that there was an experimental feature to disable the dependency of the generated code on GObject, so it generates just plain C without any runtime dependencies. Doing so disables a number of more advanced OO features, but it still leaves you with a better syntax, a basic object system, and (semi-)automatic memory management.

I don't know how readable the output is, but if you run it through a pretty printer it might be very close to what you're looking for.

JanKanis
  • 6,346
  • 5
  • 38
  • 42
6

For Python specifically, take a look at this question: Convert Python program to C/C++ code?

They mention Shed Skin, which will take a subset of pure python and convert to standalone C++ code.

Cython is typically used to create Python extension modules, but can create standalone programs if the Python interpreter is embedded. This doesn't sound like what you're looking for, though.

Cython is based on Pyrex, and they are compatible with each other in many ways.

For some of the other languages you mentioned there seem to be similar projects: Ruby and PHP. Toba for Java (though no longer maintained), Marst for Algol, BCX for BASIC, COB2C, PtoC for Pascal and I should probably stop there before this turns into "List of Converters from Foo to C/C++."

Hope that helps!

Community
  • 1
  • 1
Jacinda
  • 4,932
  • 3
  • 26
  • 37
4

Take a look at this fresh new project: https://bixense.com/coffeepp/

Coffee++

Coffee++ is a little language that compiles into C++. It has been created to have something similar to CoffeeScript for C++. Currently Coffee++ is in a alpha state and not at all usable or final. Check out the source on Github to get involved.

The golden rule of Coffee++ is: "It's just C++". The code compiles one-to-one into the equivalent C++, and there is no runtime library. You can use any existing C++ library seamlessly from Coffee++ (and vice-versa).

Overview:

source file Test.cf++

include iostream

int main():
age := 5
dog := Dog(age)
if age != 7:
    dog.bark()

class Dog:
    public Dog(int age):
        this->age := age
    public void bark():
        std::cout << "Woof!\n"
    private int age
};

compiled Test.hpp

#pragma once

int main();

class Dog {
    public:
        Dog();
        void bark();
    private:
        int age;
};

compiled Test.cpp

#include "test.hpp"
#include <iostream>

int main() {
    auto age = 5;
    auto dog = Dog(age);
    if (age != 7) {
        dog.bark();
    }
}

Dog::Dog(int age) : age(age) {
}

void Dog::bark() {
    std::cout << "Woof!\n";
}
Community
  • 1
  • 1
Josue
  • 71
  • 1
  • 4
1

Since vala and genie were already mentioned, I'll put BaCon (Basic Converter) out there for those who reminisce about hand coding programs from a monthly print publication, but want to use it with a modern GUI.

Must run on each Unix/Linux/BSD platform, including MacOSX
Converted sourcecode must be compilable with GCC
Must resemble genuine BASIC with implicit variable delarations
Spoken language constructs are preferred

The website http://www.basic-converter.org/ has lots of examples (some of theme pretty complex for "BASIC") and plugins for nearly every opensource IDE or you can use the BACON IDE.

technosaurus
  • 7,676
  • 1
  • 30
  • 52
1

Well, this is not what you want, but.. : http://www.campbell.nu/oscar/cython/index.html - This cython/cytoc is a significant space (pythonish) transpiler for C/C++ that I coded around 1999/2000, it has no relation to the cython project that arrived seven years later.

Frankly, I wrote it in Perl and it's heuristical, using regular expressions. I used it for an entire project of a Gameboy Color game (regular ansi C). But I wouldn't trust it... Which is why I'm looking around too, instead of using that dusty old bugger ;)

Follow up: I've been working on Onyx (https://github.com/ozra/onyx-lang) for a year plus now, and finally realized the obvious thing to do is rewrite it to compile to C++ instead of LLVM-IR. The re-target idea is brand fresh, so rewrite is still vapor. But your input would be made well use of in RFC's, if you like the idea of the language, it's your chance to shape it.