Py To CPP searches files for Python-unique syntax and converts it to the C++ equivalent.
Clone the repository:
git clone https://github.com/savirsingh/pytocpp
Go to the directory and start up the conversion script:
python convert.py
There may be some bugs, and this project certainly isn't ready for professional use. It can be helpful for testing, personal use, and competitive programming, since it can generate large C++ files in seconds based on your Python code.
Statements/Syntax | Status |
---|---|
If/Else If/Else | ✅ |
For Loops | ✅ |
While Loops | ✅ |
Print Statements | ✅ |
Input (int/str) | ✅ |
Variable Assignments (int/str/bool) | ✅ |
Format Conversions | ✅ |
Single Line Comments | ✅ |
Multiline Comments | ❌ |
Feel free to contribute and add/improve any of these features.
My email address is [email protected] if you need it.
-
Don't use unnecessary brackets in your output/print statement.
a = 100 b = 10 for i in range(10): print((a+b)*10)
a = 100 b = 10 c = a+b for i in range(10): print(c*10)
-
Don't rely entirely on Py To CPP.
- Convert print statements to std::cout (by simply replacing "print(" with "cout << " and replacing ")" with either " << endl;" or another string of your choice).
- Convert both string inputs and integer inputs to std::cin (through removal of " = input()" and addition of " >> cin").
- Convert for loops by replacing relevant syntax and adding "}" to the next unindented line.
- Convert lists to std::vector by (inefficiently) analysing list's contents (int/str).
- Convert integers to strings and vice versa by detecting and replacing the relevant syntax if "input" is not present in the line.
- Convert pass statements to ";" (by literally finding "pass" and changing it to ";").
- Output new C++ file contents in shell window.
- Ask to save C++ file (.cpp) in the same directory.
- Check if files actually exist in directory, if not, return an error and restart.
The main conversion script is convert.py and the main C++ starter template (boilerplate) the code currently uses is template.cpp.
Thank you for spending the time to look at my repository. I hope it benefits you and you learn something new from it! I feel it's pretty amazing that I was able to make this without using any artificial intelligence packages (I didn't want it to have any dependencies).
I must admit, I will be using this in future!
- Savir Singh ([email protected])