A simple programming language implementation for understanding interpreters and compilers
Pilox is an interpreted programming language built from scratch as a learning project. It's inspired by the Crafting Interpreters approach but with some unique additions. This project aims to demystify how programming languages work under the hood.
Note
This project is currently under active development. Many features are incomplete or may change.
- C-like syntax with modern language features
- Support for variables, functions, and control structures
- First-class functions
- Object-oriented programming capabilities
- Dynamic typing
- Interactive REPL mode for quick testing
- File execution mode for running scripts
# Clone the repository
git clone https://github.com/ppmpreetham/pilox.git
cd pilox
# No installation required - pure Python implementation
- Generate ASTs:
python -m language.tool.Expr language
python language/main.py
For interactive use, simply run:
pilox
This will start the Pilox REPL where you can write and execute code line by line.
pilox> var greeting = "Hello, world!";
pilox> print greeting;
Hello, world!
pilox> exit
To execute a Pilox script file:
pilox ./path/to/script.plx
print "Hello, World!";
Note
Coming soon and will look like:
var name = "Pilox";
var version = 0.1;
print "Running " + name + " version " + version;
Note
Coming soon and will look like:
var max = 10;
var i = 0;
while (i < max) {
print i;
i = i + 1;
}
Note
Coming soon and will look like:
fun factorial(n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
print factorial(5); // Outputs: 120
pilox/
├── language/
│ ├── main.py # Entry point for the interpreter
│ ├── scanner.py # Lexical analyzer
│ └── TokenType.py # Token definitions
├── examples/ # Example pilox programs
└── README.md
Pilox is built following a classic compiler pipeline:
- Scanning/Lexing (Implemented): Converting source text into tokens
- Parsing (In progress): Building an abstract syntax tree The grammar goes like this:
primary → NUMBER | STRING | "true" | "false" | "nil" | "(" expression ")" ;
unary → ( "!" | "-" ) unary | primary ;
factor → unary ( ( "/" | "*" ) unary )* ;
term → factor ( ( "-" | "+" ) factor )* ;
comparison → term ( ( ">" | ">=" | "<" | "<=" ) term )* ;
equality → comparison ( ( "!=" | "==" ) comparison )* ;
expression → equality ;
- Static Analysis (Planned): Scope resolution and type checking
- Interpretation/Compilation (Planned): Executing or translating the code
Contributions are welcome! If you'd like to help develop Pilox:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE
for more information.
- Crafting Interpreters by Robert Nystrom
- The Python programming language
- Lexical analysis (Scanner)
- Parser implementation
- AST interpreter
- Variable declarations and scopes
- Control flow statements
- Functions and closures
- Classes and instances
- Standard library