This repository contains the Tiny C Compiler, developed as part of the MC921 (Compilers) course at Unicamp. The compiler was implemented using Python and the SLY framework. You can run and debug Tiny C code using the uc_code.py script. Below, you'll find detailed instructions on how to set up and use the project.
- Python: Version 3.10 or newer.
- Pip Packages:
slypytestsetuptoolsgraphviz
We recommend using a virtual environment to isolate the project dependencies from your system packages. Follow these steps to install the project:
python3 -m venv .venv
source .venv/bin/activate
pip install -e .Alternatively, you can use Docker and Docker Compose (requires Docker Engine 19.03.0+) to set up the environment without modifying your local system.
You can execute the uc_code.py script directly with Python. To see the available options, run:
python3 uc/uc_code.py -hIf you have installed the project in your environment, you can use the uc-code command to run the code generator:
uc-code -hTo test the compiler with one of the sample inputs, use the files provided in the tests/in-out/ directory. For example:
uc-code tests/in-out/t01.inAdditionally, the uc_compiler.py script provides a CLI interface through the ucc command. For more details:
ucc -hIf you’re working in the Docker environment, you can run uc_code.py with the following commands:
docker-compose run --rm test uc/uc_code.py -hExample of running a test file:
docker-compose run --rm test uc/uc_code.py tests/in-out/t01.inYou can automatically test the compiler using the pytest framework with the files in the tests/in-out/ directory.
Make the source files accessible to the tests using one of these methods:
-
Install the project in editable mode:
pip install -e . -
Add the repository to
PYTHONPATH:source setup.sh
Once the setup is complete, run all tests from the root of the repository:
pytestIn the Docker environment, use this command to run all tests:
docker compose run --rm pytestFor maintaining code quality, use the following tools:
- Linting:
flake8 - Formatting:
blackandisort
Install the additional tools in the virtual environment:
python3 -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"Run the following commands from the root of the repository to check for errors and coding style:
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
flake8 . --count --exit-zero --max-line-length=120 --statisticsThe first command highlights errors that need to be fixed. The second provides warnings for improving code style.
To format the code, use the following commands:
isort .
black .