pydotcpp is a tree-walking Python interpreter written completely in C++.
It works by scanning and parsing Python source code to construct an Abstract Syntax Tree (AST), which it then directly evaluates using a tree-walking execution process.
Currently, pydotcpp supports a core subset of Python's syntax and behavior:
- Variables and Assignment: Standard dynamic variable bindings.
- Arithmetic and Logical Operations: Standard math operations (
+,-,*,/,//,%,**), logical operations (or,and,not) and comparison operations (==,!=,>,<,<=,>=). - Conditionals:
if,elifandelsebranching mechanisms. - Loops:
whileloops for iteration. - Control Flow:
breakandcontinuestatements to control loop execution. - Functions: Definition of custom functions with parameters, scoped variables, and
returnstatements. - Data Structures: Support for Python
Lists.
def sum_even_numbers(limit):
total = 0
i = 0
while i < limit:
if i % 2 == 0:
total = total + i
i = i + 1
return total
my_list = [1, 2, sum_even_numbers(10)]To build and run pydotcpp, you will need the following installed on your system:
- A C++ compiler (e.g.,
g++orclang++) make(GNU Make)clang-format(Optional, for code formatting during development)
The project includes a Makefile to simplify building, running, and testing the interpreter. All compiled objects and executables are safely placed in the build/ directory.
-
Build the interpreter:
make
This compiles the source code and creates the executable at
build/pydotcpp. -
Build and run immediately:
make run
-
Clean build artifacts:
make clean
This removes the
build/directory and all compiled.oand.dfiles.
The project is broken down into modular tests that verify the different stages of the interpreter pipeline. You can run these tests individually:
- Test the Lexer: (Validates token generation)
make test-lexer
- Test the Parser: (Validates Abstract Syntax Tree generation)
make test-parser
- Test the Evaluator: (Validates the tree-walking execution logic)
make test-evaluator
If you are contributing to or modifying the C++ source code, you can use the built-in formatting targets to maintain a consistent code style:
-
Auto-format code:
make format
Runs
clang-format -iacross all.cppand.hfiles in thesrc/directory. -
Check formatting (CI/Dry-run):
make check-format
Verifies that the code complies with the formatting rules without making actual changes. Useful for continuous integration pipelines.
src/- Core interpreter implementation (Lexer, Parser, Evaluator, AST definitions, Runtime definitions).tests/- C++ test files for the interpreter's discrete components.entrypoint.cpp- The main entry point for the compiled executable.Makefile- Build, test, and format configuration.