top | item 10919564

Show HN: Python to C++14 transpiler

96 points| morgenkaffee | 10 years ago |github.com

44 comments

order

pjmlp|10 years ago

It is called a compiler even it outputs code in another language, transpiler is some neologism from JavaScript developers without a background in compiler design.

chrisseaton|10 years ago

I'd defend the word 'transpiler' and I did my masters and PhD on language implementation, and that's where I work professionally now, so I'm not ignorant of compilers.

I think it implies a translation from one high level language to another (not that 'high level' is well defined either), with only desugaring and maybe type-checking - no real lowering or optimisations. That's a useful subset of compilers, so can have its own word I believe.

poizan42|10 years ago

Why are people giving this word so much flak? Yes it's a neologism, but it's a useful one. A decompiler is a compiler to, it just goes from a low level language to a high level one. Do you suggest that we stop using the word decompiler as well?

kitd|10 years ago

'Transpiler' goes back long before the Javascript/CoffeScript world. Pascal/C and FORTRAN/C have a history of 'transpilation' products.

It's just short-hand for source-to-source compiler and IMHO has been used enough to warrant its inclusion in a programmer's dictionary.

srean|10 years ago

> It is called a compiler even it outputs code in another language

Indeed and most compilers do exactly that. Cant help getting a little annoyed whenever I hear this 'transpiler' word. I guess the ship has sailed, oh well !

nimitkalra|10 years ago

From what I've noticed, these "transpilers" output code that is readable (the code itself is written as though a human wrote it) where as "compilers" output code that has been optimized and show effects of name mangling in the code itself, etc. Just an observation.

I think it makes sense to use a different term for this "compiler"-esque behavior. For example, I might edit the output of CoffeeScript generated Javascript whereas I wouldn't know how to modify the output of gcc.

omaranto|10 years ago

People believe some languages are "higher level" than others and call a tool that translates programs written in A to programs written in B a compiler if A is clearly higher level than B and a transpiler if B is of a level higher than or roughly equal to A.

You can certainly think this distinction doesn't merit using a different word, but you shouldn't think that people who use the word "transpile" use it as a synonym for "compiler".

lmm|10 years ago

The distinction is meaningful and valuable. Language evolves. No need for the attacks.

morgenkaffee|10 years ago

You are right. C++ is quite different to Python and so it should be called a compiler.

But it fits the project since I don't have any background or experience in compiler design.

tormeh|10 years ago

Transpilers are a subcategory of compilers. It's just a more specific word.

jmgao|10 years ago

Alternatively, translator, the word that CFront used.

drvortex|10 years ago

Strictly you are right. However, by the strict definition of a compiler, an interpreter is nothing but a sort-of "real time compiler".

But a compiler has come to mean a program that translates source-code into machine code.

By this new understanding, a transpiler is a combination of a compiler and a decompiler with different input and output languages.

So po-tay-toes, po-taa-toes. No one cares, we know what is meant by transpiler.

programmer_man|10 years ago

The word transpiler was used before we had web browsers, much less javascript.

Point taken that it is still a compiler.

throwaway999888|10 years ago

I for one am just waiting for the cispiler.

haberman|10 years ago

Key sentence: "The goal is to showcase the power of C++14 templates and not to create a fully functional transpiler."

Viewed through that lens, this is a really novel and cool demonstration.

Joky|10 years ago

This seems to me to be very close to what Pythran [0] is doing (since 2011), except that Pythran includes some type inference and bridge with Python code.

So view through that lens I'm not really seeing the novelty right now?

[0]: http://github.com/serge-sans-paille/pythran/

Sharlin|10 years ago

Should be noted that this only works on a "statically typeable" subset of Python where every variable has a de facto static type inferred at the first assignment. For instance, the following valid Python code would output invalid C++:

  var = []
  var = 2

morgenkaffee|10 years ago

Yes you would have to program in a subset of python. But type annotations are not always needed.

For the array I do a little hackery. You can define the array without an initial value in the container and I can guess the value type.

  arr = []
  arr.append(1)
it will spit out

  std::vector<decltype(1)> arr{};
  arr.push_back(1);

exprx|10 years ago

Well, you could always begin another scope with each assignment.

    auto var = // this type can't be inferred because it's not used
    {
      auto var = 2;
    }
I'm too exhausted to think why this may not be applicable.

Fede_V|10 years ago

There's a few similar process. Shedskin, Nuitka, Pythran, etc. They are all pretty cool projects, and worth looking into just to learn new techniques.

srean|10 years ago

... and unpython, spyke .... all very cool and with their own spin. What is a little worrisome is the rate at which these get abandoned and how many of them target some specific subset of Python. I particularly liked unpython's take.

seivan|10 years ago

Would someone be kind to explain how the transpiler/compiler knows that num of T1 generic(?) type can be used with the <= operator? Or that something the user themselves have to define?

Wouldn't be something like "T1 where T1 is Numeric"?

Thanks!

inglor|10 years ago

This is how C++ templates work. They are a form of polymorphism through how you use the object and not based on its type.

If you pass a type that cannot be used with the <= operator it will error in compile time.

Being able to do this is part of why C++ templates are much more powerful than Java/C# generics and why they enable a different (and alternative) form of polymorphism to inheritance and explicit interfaces.

morgenkaffee|10 years ago

It is all magic of Clang or GCC. Once you call the template with T1 the C++ compiler will enforce that your passed type supports the <= operator.

techdragon|10 years ago

Writing something like this for Python 2 is like throwing a urine filled water balloons at all the progressive developers working hard to get the Python community transitioned to Python 3.

Don't have enough reasons to stick with shitty old Python 2, well then here's another anchor for your boat!

Edit: The first pull request was for Python 3 support, hooray.

anc84|10 years ago

No need to phrase it so nastily.