pradyun / py2c Goto Github PK
View Code? Open in Web Editor NEWPy2C — a Python to C++ converter
License: BSD 3-Clause "New" or "Revised" License
Py2C — a Python to C++ converter
License: BSD 3-Clause "New" or "Revised" License
A common mistake is to say C/C++ when you talk about these two languages.
Not only is this correct, it also implies that they're the same language (they're not).
For example, the sample code you've posted on the main page (the hello world program converted to C++) can't compile on C because it doesn't support the << operator (it only supports printf).
Not only that, in addition to the futures that were introduced in C++ only existing in that language (ie, classes, templates, exception handling, etc.), there's certain code that would compile on C but not on C++, like this:
int main()
{
double sq2 = sqrt(2); /* Not C++: call undeclared function */
int s = sizeof('a'); /* silent difference: 1 in C++ sizeof(int) in C */
}
And this:
void* malloc(size_t);
void f(int n)
{
int* p = malloc(n*sizeof(char)); /* not C++. In C++, allocate using `new' */
char c;
void* pv = &c;
int* pi = pv; /* implicit conversion of void* to int*. Not in C++ */
}
(both are taken from Stroustrup (the original designer of C++)'s website.)
Sources:
I cloned the project and build and install on setup.py.
Wondering how to proceed after this to translate a simple python file.
Hi,
I tried to adapt codegen which is Python -> ast.AST
-> Python to generate C++ in the last step. And it roughly works 😄. It is here.
Using it should look like :
import ast
import codegen_cpp
py_code = """
s = 10
s = s + 5
"""
node = ast.parse(py_code, mode='exec')
cpp_code = codegen_cpp.to_source(node)
print( py_code, "\n----------------------\n")
print(cpp_code , "\n----------------------\n")
I like the Idea behind this project, But seeing it not even pass the prototype stage even after 4 years is not good sign. So will this project ever be usable? or Did you just leave it?
doesn't compile at all
what happened
On line 26 of /py2c/translator.py it says:
from py2c.syntax_tree import python
When I attempted to run the file, it said:
>>>
Traceback (most recent call last):
File "C:\Users\Daniel\Desktop\Py2C-master\py2c\translator.py", line 26, in <module>
from py2c.syntax_tree import python
ImportError: cannot import name 'python'
>>>
It seems to be trying to import python.ast and failing. I have no idea what the proper method of loading it is, though appearently it is not import.
If anyone knows how to deal with this, I'd be happy to fix it.
Hello ,
May I learn please how it works ? and thank you !
Hi there,
As one of many guys out there who loves python and would appreciate an automated way to transform prototyped python code into readable C++ I must say this project is far from usable. And at this stage you should ask yourself, is it worth the troubles in the long run?
The goal seems to be a conversion which does not facilitate the Python API in any way to be as fast as possible and creates readable code.
Both Shed Skin and Nuitka create C++ code. The former in a almost readable form (quite close to python code with many library calls and defines) but with some restrictions, while the latter is CPython feature complete and has ambitions for speed ups.
IMO, Shed Skin is amazing and the readability might improve in future versions.
Nuitka on the other hand takes the performance path, which is already well served with Cython and JITs. I say only humans are able to to write code that is readable and high(est) performance at the same time. So better join up with Shed Skin, or Nuitka.
Hi,
when I try to convert python code to c on windows with python file placed in same location where py2c.sh exists. I get an error running the following command
py2c -o file Encoder.py --keep
error
'py2c' is not recognized as an internal or external command,
operable program or batch file.
I get same error even if I run
py2c.sh -o file Encoder.py --keep
I don't know how to actually USE Py2C to convert Python into C/C++. What specific function do you use in the py2c folder to convert the code?
This is a meta-issue for tracking the progress of the project till it's first public release.
I'll keep posting updates here.
Hi,
Would you please tell me how can I run Py2C after install? I couldn't find anything that could help me with this.
I'd happy to make an online demo for Py2C if you like.
I would like to guide some students to contribute to this project and would like to get your inputs on the priorities you might have.
Thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.