
Show HN: Python to C++14 transpiler - morgenkaffee
https://github.com/lukasmartinelli/py14
======
pjmlp
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
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.

~~~
pjmlp
So for you a C compiler that follows the initial workflow is a transpiler?

Or an Eiffel implementation, as another example.

~~~
chrisseaton
I think a transpiler is a compiler that does a high-level to high-level
translation with only a few simple transformations and almost no optimisations
and produces relatively readable output.

If a C or Eiffel compiler meets that definition then yes. I'd still call it a
compiler, but I would also call it a transpiler.

If you're offended by the term transpiler because all transpilers are also
compilers, then I don't know why you wouldn't also be offended by the term
compiler because all compilers are also 'programs', so lets just call
everything a 'program'.

~~~
pjmlp
Apparently I am the one wrong here, given the 1964 paper link provided by
Ded7xSEoPKYNsDd.

Still the word sounds strange.

------
haberman
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
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/](http://github.com/serge-
sans-paille/pythran/)

~~~
anon4
There is also nuitka - [http://nuitka.net/](http://nuitka.net/)

------
Sharlin
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

~~~
exprx
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.

~~~
marvy
Python:

    
    
        x = 2
        if stringy: x = '2'
        print x+x # 4 or 22???
    

C++:

    
    
        int x = 2;
        if(stringy) string x = "2";
        print x+x; // 4 (string x is out of scope)

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

~~~
seivan
I see. In e.g Swift, you'd have to define before hand that T1 could be used
with <= while here I guess it adds in the method for "each" T1 that could be
used with it.

I assume this just duplicates the method for each type at compile time instead
of at runtime try to figure it out?

Damnit, now I wanna rewrite back to C++.

~~~
jschwartzi
In C++ it just prints an error message and halts compilation if you try to use
a type which doesn't support the semantics of the template function. It does
not add functions which are not declared already.

------
techdragon
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
No need to phrase it so nastily.

~~~
marktangotango
You say nasty, I say colorful. No need to characterize it so prejudicially.

