Transpiler from a Python subset of instructions to C++ implemented using the PLY library.
To use the project certain packages are required.
To install them you can run the following command in your terminal:
pip install -r requirements.txt
To use the transpiler itself, you can execute the following command from the src
directory:
python main.py (file_name) [debug]
Where (file_name)
corresponds to an obligatory parameter and a file with the following characteristics:
- The file must have a
.py
extension. - The file must exist.
And [debug]
corresponds to an optional parameter, if set to the string "debug"
it will print out PLY's debugging mecanisms and the generated AST (Abstract Syntax Tree).
The generated code will be in output/main.cpp
.
To compile the generated code, run the following command in the output
directory:
g++ -o transpiled_code main.cpp
Then, to execute it, run the following command in the output
directory:
./transpiled_code
An execution example can be done with the file test_principal_cases.py
python main.py tests/transpiler_test_files/test_principal_cases.py
To run it in debug mode you can do so the following way:
python main.py tests/transpiler_test_files/test_principal_cases.py debug
To run the unit tests for the lexer, the pytest module is utilized.
To execute all tests once, you must be in the src/tests/lexer_test_files
directory and run the following command:
python -m pytest
In case of wanting to run a specific test, you can execute the following command:
python -m pytest [file_name]
The current options for <file_name> are:
test_indentation.py
test_tokens.py
To execute the unit tests for the parser, pytest is not required.
Therefore, inside the src
directory the following command must be executed.
python ./tests/test_parsing.py [is_python3]
Where current options for <is_python3> are:
1
to call the test cases usingpython3
0
to call the test cases usingpython
This will run all the parsing tests at once, and tell you the total amount of tests passed and failed.
The transpiler tests are in the directory src/test/transpiler_test_files
.
To execute the tests, there are a few steps to follow:
-
Select a test to run from the directory.
-
Transpile the Python code following the Use Manual for the selected test.
-
Compile and execute the transpiled code following the Use Manual.
-
Execute the desired test in Python with the following command, where
(path_to_test)
corresponds to the path of the desired test:python (path_to_test)
-
Finally, compare both outputs.
Note: the sets and dictionaries in C++ are stored in a different order than Python. Hence, differences in the output regarding this order are not alarming.
- Luis David Solano Santamaría
- Angie Solís Manzano
- Emilia Víquez Mora