8 ms·
Show HN: Python to C++14 transpiler
- haberman 11y agoKey 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 11y agoThis 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 11y agoThere is also nuitka - http://nuitka.net/ http://nuitka.net/
- Fede_V 11y agoThere'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 11y 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.
- pjmlp 11y agoIt is called a compiler even it outputs code in another language, transpiler is some neologism from JavaScript developers without a background in compiler design.
- agumonkey 11y agoor pretty printing
- srean 11y 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 !
- morgenkaffee 11y agoYou 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.
- jmgao 11y agoAlternatively, translator, the word that CFront used.
- Sharlin 11y agoAnd "translation" is still the term of art for the compilation process at least in the C and C++ standards.
- nimitkalra 11y agoFrom 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.
- pjmlp 11y agoThe output of gcc can be straight readable Assembly, once upon a time a normal language to write business applications in. Also C compilers used to generate Assembly text files, which were then piped into the Assembler. So no, transpiler doesn't make any sense.
- Sharlin 11y agoShould 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 11y agoYes 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 11y agoWell, 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 11y agoPython: 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)
- seivan 11y agoWould 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!
- morgenkaffee 11y agoIt 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.
- inglor 11y agoThis 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 11y agoI 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 11y agoIn 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.
- aldanor 11y agoThat's what C++ concepts are for (maybe we'll see them in C++17).
- techdragon 11y agoWriting 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 11y agoNo need to phrase it so nastily.
- marktangotango 11y agoYou say nasty, I say colorful. No need to characterize it so prejudicially.