A fully-featured programming language interpreter built in Rust, featuring a complete lexer, parser, AST, and execution engine with comprehensive error handling, arrays, and a REPL.
- Data Types: Numbers (integers/floats), Strings, Booleans, Null, Arrays
- Variables: Dynamic typing with
letdeclarations - Operators: Arithmetic (+, -, *, /, %), Comparison (==, !=, >, <, >=, <=), Logical (&&, ||, !)
- Control Flow: If/else statements, while loops
- Functions: User-defined functions with parameters and return values
- Recursion: Full support for recursive function calls
- Arrays: Dynamic arrays with indexing
arr[0], literals[1, 2, 3], and manipulation
- Math:
abs(),sqrt(),pow(),min(),max() - Utility:
len()(string/array length),type()(type inspection) - Arrays:
push(),pop(),first(),last(),sort(),reverse(),includes(),find() - Strings:
split(),join(),substring(),to_upper(),to_lower(),trim(),starts_with(),ends_with(),contains(),replace() - I/O:
print()(output),read_file(),write_file()
- Objects/Maps:
{name: "John", age: 30}with property access viaobj[key] - Enhanced Arrays: Multi-dimensional arrays, sorting, searching, and manipulation
- String Manipulation: Comprehensive string processing with split/join, case conversion, and pattern matching
- Classes: Define classes with methods and properties
- Inheritance: Extend classes with inheritance support
- Object Instantiation: Create instances with the
newkeyword - Method Calls: Call methods on class instances
- Match Expressions: Pattern matching with destructuring
- Literal Patterns: Match numbers, strings, booleans
- Variable Binding: Capture values in patterns
- Array/Object Destructuring: Extract values from complex data structures
- Import/Export: Modular code organization with
importandexport - File-based Modules: Import functions and variables from other files
- Namespace Management: Clean separation of concerns
- REPL: Interactive Read-Eval-Print Loop for experimentation
- Enhanced Error Messages: Detailed error reporting with source context and visual pointers
- Comprehensive Testing: Full test suite with example programs
- CLI Interface: Command-line tool for running programs
- Production-Ready CI/CD: Comprehensive pipeline with quality checks, security audits, and cross-platform testing
- Rust 1.70+ (install from rustup.rs)
git clone <repository-url>
cd custom-lang-interpreter
cargo build --releaseThis project features a production-ready CI/CD pipeline with multiple quality gates:
- Code Quality: Automated formatting checks with
rustfmt - Linting: Comprehensive linting with
clippy(zero warnings policy) - Testing: Full test suite including unit and integration tests
- Feature Testing: Comprehensive test suite covering all language features
- Example Validation: All 15+ example programs tested automatically
- Multi-Version Support: Testing on Rust stable and beta channels
- Security Audits: Automated dependency vulnerability scanning with
cargo-audit - Code Coverage: Coverage reporting with
cargo-llvm-covand Codecov integration - Performance Testing: Automated performance benchmarks on example programs
- Multi-OS Testing: Automated testing on Ubuntu, Windows, and macOS
- Target Verification: Cross-compilation testing for multiple targets
- Example Validation: All example programs tested on each platform
- Documentation Generation: Automated API documentation with
cargo doc - Release Automation: Automated GitHub releases with artifacts
- Repository Maintenance: Automatic topic updates for discoverability
# Run all quality checks locally
make ci
# Test all language features comprehensively
./test_all_features.sh
# Test stable examples only
./test_examples.sh
# Run individual checks
make fmt # Format code
make clippy # Run lints
make test # Run tests
make build # Build project# Run a program file
cargo run -- examples/test.cl
# Run with verbose output (shows compilation stages)
cargo run -- examples/test.cl --verbose
# Skip semantic analysis for faster execution
cargo run -- examples/test.cl --no-semantic
# Start interactive REPL
cargo run -- --repl
# Start REPL with verbose mode
cargo run -- --repl --verbose
# Show comprehensive help
cargo run -- --help
# Show version information
cargo run -- --version--verbose, -v: Enable detailed output showing compilation stages--no-semantic: Skip semantic analysis (faster but less safe)--repl, -r: Start interactive Read-Eval-Print Loop--help, -h: Display help information--version, -V: Show version information
# Basic features demo
cargo run -- examples/demos/basic_features.cl
# Functions and recursion
cargo run -- examples/demos/functions_demo.cl
# Built-in functions
cargo run -- examples/demos/builtin_functions_demo.cl
# Algorithms showcase
cargo run -- examples/demos/algorithms_demo.cllet number = 42;
let text = "Hello, World!";
let flag = true;
let nothing = null;// If/else statements
if (x > 10) {
print "x is large";
} else {
print "x is small";
}
// While loops
let i = 0;
while (i < 5) {
print i;
i = i + 1;
}// Simple function
function greet(name) {
return "Hello, " + name + "!";
}
// Recursive function
function factorial(n) {
if (n <= 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
print greet("World");
print factorial(5);The interpreter comes with comprehensive example programs demonstrating all features:
test.cl- Basic language features and built-in functionscalculator.cl- Mathematical operations and functionsmath_playground_simple.cl- Mathematical computations and algorithmsnumber_game.cl- Interactive number guessing game
arrays_demo.cl- Array creation, indexing, and basic operationsenhanced_arrays_demo.cl- Advanced array operations (sort, reverse, search)objects_demo.cl- Object/map creation and property accessobjects_simple_demo.cl- Simplified object operationsstring_demo.cl- String manipulation and processing
file_io_demo.cl- File reading and writing operationserror_handling_demo.cl- Enhanced error messages and suggestionsperformance_test.cl- Performance testing and benchmarking
classes_demo.cl- Object-oriented programming with classespattern_matching_demo.cl- Pattern matching and destructuringmodules_demo.cl- Module system with import/exportmath_utils.cl- Utility module for mathematical functions
# Core examples
cargo run --release -- examples/test.cl
cargo run --release -- examples/calculator.cl
# Advanced examples
cargo run --release -- examples/arrays_demo.cl
cargo run --release -- examples/file_io_demo.cl
# Experimental features (requires --no-semantic flag)
cargo run --release -- --no-semantic examples/classes_demo.cl
cargo run --release -- --no-semantic examples/pattern_matching_demo.cl
# Run all examples with comprehensive testing
./test_all_features.sh- Numbers:
42,3.14,-5 - Strings:
"Hello","World" - Booleans:
true,false - Null:
null - Arrays:
[1, 2, 3],["a", "b"],[1, "hello", true]
let name = "Alice";
let age = 25;
let isStudent = true;
let nothing = null;
let numbers = [1, 2, 3, 4, 5];
let mixed = [42, "hello", true];+Addition (also string concatenation)-Subtraction*Multiplication/Division%Modulo
==Equal!=Not equal<Less than<=Less than or equal>Greater than>=Greater than or equal
&&Logical AND||Logical OR!Logical NOT
if (condition) {
// code
} else if (other_condition) {
// code
} else {
// code
}while (condition) {
// code
}function functionName(param1, param2) {
// function body
return value; // optional
}let result = functionName(arg1, arg2);let empty = [];
let numbers = [1, 2, 3, 4, 5];
let mixed = [42, "hello", true, null];
let nested = [[1, 2], [3, 4]];
// Array indexing
print numbers[0]; // 1
print numbers[2]; // 3
print mixed[1]; // "hello"
print nested[0][1]; // 2
// String indexing (bonus feature)
let text = "Hello";
print text[0]; // "H"
print text[4]; // "o"print len(numbers); // 5
print len(empty); // 0
print type(numbers); // "array"abs(x) // Absolute value
sqrt(x) // Square root
pow(x, y) // x raised to the power of y
min(x, y) // Minimum of two values
max(x, y) // Maximum of two valueslen(string) // Length of stringlen(array) // Length of array
push(array, value) // Add element to array (returns new array)
pop(array) // Get last element of array
first(array) // Get first element of array
last(array) // Get last element of arraytype(value) // Returns type as string ("number", "string", "boolean", "null", "array")
print(value) // Output value to consoleprint abs(-10); // 10
print sqrt(16); // 4
print pow(2, 3); // 8
print min(5, 3); // 3
print max(5, 3); // 5
print len("hello"); // 5
print type(42); // "number"
print type("text"); // "string"
print type(true); // "boolean"
print type(null); // "null"
print type([1, 2, 3]); // "array"
// Array examples
let arr = [10, 20, 30];
print first(arr); // 10
print last(arr); // 30
print push(arr, 40); // [10, 20, 30, 40]
print pop(arr); // 30Run the comprehensive test suite:
# Run all tests
./run_tests.sh
# Run specific examples
cargo run -- examples/demos/basic_features.cl
cargo run -- examples/demos/functions_demo.cl- Lexer (
src/lexer.rs) - Tokenizes source code - Parser (
src/parser.rs) - Builds Abstract Syntax Tree - AST (
src/ast.rs) - Defines language constructs - Interpreter (
src/interpreter.rs) - Executes the program - Environment (
src/environment.rs) - Manages variable scope - Error Handling (
src/error.rs) - Comprehensive error reporting - REPL (
src/repl.rs) - Interactive shell
The interpreter provides comprehensive error reporting with source context:
Error: Lexical error at line 5, column 12: Unexpected character '@'
Source context:
4 | let x = 10;
5 | let y = @invalid;
^
Error: Parse error at line 7, column 15: Expected ';' after expression
Source context:
6 | let x = 10;
7 | let y = 20
8 | print x;
^
Error: Semantic error: Invalid operands for +: number and boolean
Source context:
5 | let x = 10;
6 | let result = x + true;
^
Error: Runtime error: Undefined variable 'unknown_var'
Source context:
8 | print x;
9 | print unknown_var;
^
In the interactive REPL:
help- Show available commands and language featuresexit- Exit the REPL- Any valid language expression or statement
- Persistent state: Variables and functions persist across commands
- Error recovery: Syntax errors don't crash the REPL
- History: Use arrow keys to navigate command history
- Multi-line support: Functions and blocks can span multiple lines
# Make sure the file exists and path is correct
ls examples/test.cl
cargo run -- examples/test.cl# Warnings are normal and don't affect functionality
# To see clean output, redirect stderr:
cargo run -- examples/test.cl 2>/dev/null# For faster execution, skip semantic analysis:
cargo run -- large_program.cl --no-semantic
# Use release build for better performance:
cargo build --release
./target/release/custom-lang examples/test.cl# Use verbose mode to see compilation stages:
cargo run -- examples/test.cl --verbose
# This shows:
# - Tokenization results
# - Parse tree information
# - Semantic analysis status
# - Execution progressSee the examples/ directory for comprehensive demonstrations:
basic_features.cl- Language fundamentalsfunctions_demo.cl- Function definitions and recursionbuiltin_functions_demo.cl- Built-in function showcasealgorithms_demo.cl- Complex algorithms and patterns
This repository is tagged with the following topics for discoverability:
rust- Built with Rust programming languageinterpreter- Programming language interpreterprogramming-language- Custom programming language implementationlexer- Lexical analysis implementationparser- Syntax analysis implementationast- Abstract Syntax Tree implementationrepl- Read-Eval-Print Looparrays- Dynamic array supportcompiler- Language compilation and executioneducation- Educational programming language projectrust-lang- Rust language implementationlanguage-design- Programming language designsemantic-analysis- Static analysis implementation
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
This project is open source and available under the MIT License.
Built with Rust and the following excellent crates:
clap- Command-line argument parsingrustyline- REPL functionalitythiserror- Error handlingcolored- Terminal colors