A complete mini compiler/interpreter implementation written in Rust featuring lexical analysis, parsing, Abstract Syntax Tree (AST) generation, and execution of a custom toy programming language.
- Variables & Data Types: Numbers, strings, booleans, and nil
- Arithmetic Operations:
+,-,*,/,%(modulo) - Comparison Operations:
>,>=,<,<=,==,!= - Logical Operations:
&&,||,! - Control Flow:
if/elsestatements,whileloops - Functions: Declaration, parameters, return values, recursion
- Variable Scoping: Lexical scoping with proper environment management
- String Concatenation: Automatic type coercion for mixed-type operations
- Recursive Functions: Full support for recursive algorithms
- Error Handling: Comprehensive error messages with line/column information
- REPL Mode: Interactive Read-Eval-Print Loop for testing
- File Execution: Run
.toyfiles directly with proper file extension validation - Comment Support: Single-line comments using
// - Mathematical Functions: Built-in support for complex mathematical operations
- Complete Lexer: Tokenization with position tracking
- Recursive Descent Parser: Produces clean Abstract Syntax Trees
- Tree-Walking Interpreter: Direct AST execution with environment management
- Memory Safe: All implemented in safe Rust with proper error handling
toy_lang/
โโโ Cargo.toml # Rust project configuration
โโโ README.md # This file
โโโ src/
โ โโโ main.rs # Entry point and REPL
โ โโโ lib.rs # Library exports
โ โโโ lexer.rs # Tokenization and lexical analysis
โ โโโ parser.rs # Recursive descent parser
โ โโโ ast.rs # Abstract Syntax Tree definitions
โ โโโ interpreter.rs # Tree-walking interpreter
โโโ examples/
โโโ hello.toy # Basic syntax examples
โโโ fibonacci.toy # Recursive algorithms
โโโ functions.toy # Advanced function examples
โโโ calculator.toy # Mathematical operations
โโโ algorithms.toy # Complex algorithms and patterns
- Rust 1.70+ (2021 edition)
- Cargo (comes with Rust)
# Clone the repository
git clone https://github.com/AarambhDevHub/toy-lang.git
cd toy_lang
# Build the project
cargo build --release
# Or run directly
cargo runStart the interactive interpreter:
cargo runExample REPL session:
Toy Language REPL v0.1.0
Type 'exit' to quit
toy> let x = 10;
toy> let y = 20;
toy> print("Sum: " + (x + y));
Sum: 30
toy> exit
Run a .toy file:
cargo run examples/hello.toy
cargo run examples/fibonacci.toy
cargo run examples/functions.toy# Run REPL (no arguments)
cargo run
# Execute a file
cargo run <filename.toy>
# Multiple files not supported - run one at a time// Variable declaration
let name = "Alice";
let age = 25;
let pi = 3.14159;
let isActive = true;
let empty = nil;// Arithmetic
let sum = 10 + 5; // 15
let product = 4 * 7; // 28
let remainder = 17 % 5; // 2
// Comparisons
let isEqual = (5 == 5); // true
let isGreater = (10 > 3); // true
// Logical operations
let both = true && false; // false
let either = true || false; // true// If statements
if (age >= 18) {
print("You are an adult");
} else {
print("You are a minor");
}
// While loops
let i = 0;
while (i < 5) {
print("Count: " + i);
i = i + 1;
}// Function declaration
function greet(name, age) {
print("Hello, " + name + "! You are " + age + " years old.");
}
// Function with return value
function factorial(n) {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}
// Function calls
greet("Bob", 30);
let result = factorial(5);
print("5! = " + result); // 5! = 120// String concatenation with automatic type conversion
let name = "Alice";
let age = 25;
print("Name: " + name + ", Age: " + age);
// Output: Name: Alice, Age: 25// This is a single-line comment
let x = 10; // End-of-line commentprint("Hello, World!");
let name = "Alice";
print("Hello, " + name + "!");
let x = 10;
let y = 20;
print("Sum: " + (x + y));function fib(n) {
if (n <= 1) {
return n;
}
return fib(n - 1) + fib(n - 2);
}
print("Fibonacci of 10 is: " + fib(10));function power(base, exponent) {
let result = 1;
let i = 0;
while (i < exponent) {
result = result * base;
i = i + 1;
}
return result;
}
print("2^8 = " + power(2, 8)); // 256function isPrime(n) {
if (n <= 1) {
return false;
}
let i = 2;
while (i * i <= n) {
if (n % i == 0) {
return false;
}
i = i + 1;
}
return true;
}function collatz(n) {
let steps = 0;
while (n != 1) {
if (n % 2 == 0) {
n = n / 2;
} else {
n = n * 3 + 1;
}
steps = steps + 1;
}
return steps;
}- Lexer (
lexer.rs): Converts source code into tokens - Parser (
parser.rs): Builds Abstract Syntax Tree from tokens - AST (
ast.rs): Defines tree node structures and value types - Interpreter (
interpreter.rs): Executes the AST with environment management
- Lexical Errors: Unknown characters, unterminated strings
- Parse Errors: Syntax errors with context
- Runtime Errors: Type mismatches, undefined variables, division by zero
- All errors include line and column information
- Tree-walking interpreter: Direct AST execution (not bytecode)
- Recursive descent parsing: O(n) parsing time
- Variable lookup: O(1) in current scope, O(d) across scope chain
- Memory usage: Proportional to AST size and call stack depth
# Basic examples
cargo run examples/hello.toy
cargo run examples/fibonacci.toy
cargo run examples/functions.toy
# Advanced examples
cargo run examples/calculator.toy
cargo run examples/algorithms.toyThis project demonstrates:
- Compiler Construction: Lexing, parsing, and interpretation phases
- Language Design: Syntax choices and semantic decisions
- Rust Programming: Advanced Rust features like enums, pattern matching, and borrowing
- Algorithm Implementation: Recursive algorithms and mathematical computations
- Software Architecture: Clean separation of concerns across modules
Potential improvements and extensions:
- Arrays/Lists: Data structure support
- Objects/Classes: Object-oriented programming features
- Modules/Imports: Code organization and reuse
- Standard Library: Built-in functions for common operations
- Bytecode Compilation: Performance improvements
- Garbage Collection: Automatic memory management
- Type System: Static or dynamic typing improvements
- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Make your changes and add tests
- Commit changes:
git commit -am 'Add feature' - Push to branch:
git push origin feature-name - Submit a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
If you find Ignitia helpful, consider supporting the project:
- Inspired by "Crafting Interpreters" by Robert Nystrom
- Built with the Rust programming language
- Thanks to the Rust community for excellent documentation and tools
Happy Coding! ๐
For questions, issues, or contributions, please visit the GitHub repository or open an issue.