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 languagepython language/main.pyFor interactive use, simply run:
piloxThis 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> exitTo execute a Pilox script file:
pilox ./path/to/script.plxprint "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: 120pilox/
βββ language/
β βββ main.py # Entry point for the interpreter
β βββ scanner.py # Lexical analyzer
β βββ TokenType.py # Token definitions
βββ examples/ # Example pilox programs
βββ README.mdPilox 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