pascal-rs - A Modern Pascal Compiler in Rust
pascal-rs is a Pascal compiler written in Rust, featuring trait-based architecture, comprehensive testing, and clean code generation. It combines Rust's safety guarantees with Pascal's elegance, creating a modern platform for learning compiler construction and experimenting with language design.
โจ Status: Active Development | Tests: 169/169 Passing | Language: Core Pascal Complete
Note: pascal-rs is ideal for education, research, and small projects. For production use with large codebases, consider mature compilers like Free Pascal or Delphi.
๐ฏ Why pascal-rs?
The Problem with Traditional Pascal Compilers
Traditional Pascal compilers suffer from several issues:
- Aging codebases - Difficult to maintain and extend with modern features
- Limited test coverage - Insufficient validation of edge cases
The pascal-rs Solution
pascal-rs addresses these problems with a modern, Rust-based architecture:
1. Memory Safety by Design
// Rust's ownership model prevents:
// - Null pointer dereferences โ
// - Buffer overflows โ
// - Data races โ
// - Memory leaks โ
2. Trait-Based Architecture
- Clean abstractions through Rust's trait system
- Dependency inversion - depend on traits, not concrete implementations
- Easy testing with mock implementations
- Extensible design for new features
3. Comprehensive Testing (169 tests)
- Unit tests for all components
- Integration tests for end-to-end validation
- Complex validation tests for edge cases
- Performance benchmarks
- 100% test pass rate โ
4. Modern Developer Experience
- Clear, colored error messages with source locations
- Fast compilation with incremental builds
- Excellent IDE support via Rust tooling
- Comprehensive documentation
5. Optimizations
- Constant folding and dead code elimination
- Register allocation with graph coloring
- Common subexpression elimination
- Function inlining and loop unrolling
- SIMD vectorization support
๐ Comparison with Other Pascal Compilers
Feature Comparison Table
| Feature | pascal-rs | Free Pascal (FPC) | Delphi | GNU Pascal (GPC) |
|---|---|---|---|---|
| Language | โ | โ | โ | โ |
| Memory Safety | โ Rust guaranteed | C++ implementation | C++ implementation | C implementation |
| Test Coverage | โ 169 tests | โ ๏ธ Limited | โ ๏ธ Limited | โ Minimal |
| Trait-Based Design | โ Yes | โ No | โ No | โ No |
| Interpreter Mode | โ Built-in | โ ๏ธ Limited | โ No | โ No |
| x86-64 Code Gen | โ Optimized | โ Mature | โ Mature | โ Basic |
| Active Development | โ Yes | โ Yes | โ Yes | โ No (2006) |
| Cross-Platform | โ Yes | โ Yes | โ ๏ธ Windows only | โ Yes |
| Compilation Speed | โ Fast | โ ๏ธ Moderate | โ ๏ธ Moderate | โ ๏ธ Slow |
| Error Messages | โ Modern, colored | โ ๏ธ Basic | โ Good | โ ๏ธ Basic |
Unique Advantages
1. Memory Safety
- Rust's ownership model prevents buffer overflows, null pointers, and data races
- Traditional C/C++ implementations vulnerable to these issues
2. Trait-Based Architecture
// Easy to test, easy to extend
3. Comprehensive Testing
- 169 tests, 100% pass rate
- Edge cases validated
- Complex scenarios covered
4. Modern Build System
# Simple and fast
5. Interpreter Mode
# Run directly without assembly/linking
๐ Quick Start
Installation
# Clone the repository
# Build the compiler
# The binary is now available at ./target/release/pascal
Your First Pascal Program
Create hello.pas:
program Hello;
begin
writeln('Hello, World!');
end.
Compile and run:
# Compile with assembly output
# Run using the interpreter
# View generated assembly
Output:
Hello, World!
๐ Usage Guide
Basic Compilation
# Compile a Pascal program
# Compile with optimization
# Compile with verbose output
# Compile with assembly output
# Compile to specific directory
Running Programs
# Run using interpreter
# Run with verbose output
# Run with debug information
Working with Units
# Compile a unit (generates .ppu file)
# Inspect a compiled unit
# Compile a program that uses the unit
Command Reference
| Command | Description | Example |
|---|---|---|
compile |
Compile Pascal source | pascal compile prog.pas |
run |
Run with interpreter | pascal run prog.pas |
info |
Show PPU file info | pascal info module.ppu |
clean |
Remove build artifacts | pascal clean |
Options
| Option | Description |
|---|---|
-o, --output <DIR> |
Output directory |
-O, --optimize <LEVEL> |
Optimization level (0-3) |
-v, --verbose |
Verbose output |
-S, --assembly |
Generate assembly output |
-d, --debug |
Debug information |
-I, --include <DIR> |
Add search path |
--no-cache |
Disable caching |
๐ก Examples
Example 1: Variables and Arithmetic
program Arithmetic;
var
a, b, c, d: integer;
result: real;
begin
a := 10;
b := 5;
c := 3;
d := 2;
// Complex expression: 10 + 5 * 3 - 2 / 2
result := a + b * c - d div d;
writeln('Result: ', result); // Output: 24.0
end.
Example 2: Control Structures
program ControlStructures;
var
i, sum, product: integer;
flag: boolean;
begin
sum := 0;
product := 1;
flag := false;
// For loop with sum of squares
for i := 1 to 5 do
begin
sum := sum + i * i;
if (i mod 2) = 0 then
flag := true;
end;
// While loop
i := 10;
while i > 0 do
begin
product := product * (i mod 3 + 1);
i := i - 1;
end;
writeln('Sum: ', sum);
writeln('Product: ', product);
writeln('Flag: ', flag);
end.
Example 3: Arrays and Records
program DataStructures;
type
Person = record
name: string;
age: integer;
salary: real;
end;
var
people: array[1..3] of Person;
i: integer;
begin
// Initialize records
people[1].name := 'Alice';
people[1].age := 30;
people[1].salary := 50000.00;
people[2].name := 'Bob';
people[2].age := 25;
people[2].salary := 45000.00;
people[3].name := 'Charlie';
people[3].age := 35;
people[3].salary := 60000.00;
// Display information
for i := 1 to 3 do
begin
writeln('Name: ', people[i].name);
writeln('Age: ', people[i].age);
writeln('Salary: ', people[i].salary);
writeln('---');
end;
end.
Example 4: Boolean Logic
program BooleanLogic;
var
a, b, c, d, result: boolean;
begin
a := true;
b := false;
c := true;
d := false;
// Complex boolean expressions
result := (a and c) or (not b and not d);
writeln('(true AND true) OR (NOT false AND NOT false) = ', result);
result := (a and b) or (c and d);
writeln('(true AND false) OR (true AND false) = ', result);
result := not ((a and b) or (c and d));
writeln('NOT ((true AND false) OR (true AND false)) = ', result);
end.
Example 5: Functions and Procedures
program Functions;
(n: integer): integer;
begin
if n <= 1 then
Factorial := 1
else
Factorial := n * Factorial(n - 1);
end;
(rows: integer);
var
i, j: integer;
begin
for i := 1 to rows do
begin
for j := 1 to i do
write('*');
writeln;
end;
end;
var
n: integer;
begin
n := 5;
writeln('Factorial of ', n, ' is ', Factorial(n));
writeln;
writeln('Triangle:');
PrintTriangle(5);
end.
Example 6: Real-World Calculator
program Calculator;
var
num1, num2, result: real;
operation: char;
begin
writeln('=== Simple Calculator ===');
writeln('Enter first number: ');
readln(num1);
writeln('Enter operation (+, -, *, /): ');
readln(operation);
writeln('Enter second number: ');
readln(num2);
case operation of
'+': result := num1 + num2;
'-': result := num1 - num2;
'*': result := num1 * num2;
'/': result := num1 / num2;
else
writeln('Invalid operation!');
exit;
end;
writeln('Result: ', result);
end.
Example 7: Unit System
Create a unit (MathUtils.pas):
unit MathUtils;
interface
(a, b: integer): integer;
(a, b: integer): integer;
(n: integer): boolean;
implementation
(a, b: integer): integer;
begin
Add := a + b;
end;
(a, b: integer): integer;
begin
Multiply := a * b;
end;
(n: integer): boolean;
begin
IsEven := (n mod 2) = 0;
end;
end.
Compile the unit:
Use the unit (Calculator.pas):
program Calculator;
uses MathUtils;
var
x, y: integer;
begin
x := 10;
y := 5;
writeln('Addition: ', Add(x, y));
writeln('Multiplication: ', Multiply(x, y));
writeln('Is Even (10): ', IsEven(x));
writeln('Is Even (5): ', IsEven(y));
end.
๐๏ธ Latest Capabilities
โ Trait-Based Architecture
Our modern trait-based design provides:
Core Traits
TryAs<T>- Type-safe conversions with error handlingFormattedDisplay- Configurable output formattingScopeOperations- Variable storage abstractionFunctionRegistry- Function management interfaceStatementExecutor- Pluggable execution strategiesExpressionEvaluator- Custom evaluation strategies
Benefits
// Before: Tightly coupled code
// After: Trait-based, testable code
Benefits:
- โ Easy mocking for testing
- โ Multiple implementations possible
- โ Clear separation of concerns
- โ Better maintainability
See TRAIT_ARCHITECTURE.md for details.
โ Comprehensive Testing (169 Tests)
Library tests: 87 โ
Basic tests: 1 โ
Compiler tests: 10 โ
Complex validation: 9 โ
NEW!
Integration tests: 10 โ
Interpreter tests: 11 โ
Simple compiler: 18 โ
Simple interpreter: 13 โ
Type checker: 10 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Total: 169 โ
Test Categories
1. Complex Validation Tests (NEW!)
- โ Complex arithmetic with operator precedence
- โ Nested control structures (5 levels deep)
- โ Character operations and comparisons
- โ Complex boolean expressions (AND, OR, NOT)
- โ Real number arithmetic
- โ Boundary conditions (zero, positive, negative)
- โ Comprehensive programs combining all features
2. Integration Tests
- โ Hello World
- โ Factorial calculation
- โ Fibonacci sequence
- โ Prime number generation
- โ Nested loops
- โ Record operations
- โ Array operations
- โ Case statements
- โ Repeat until loops
- โ String operations
3. Compiler Tests
- โ Simple program compilation
- โ Arithmetic operations
- โ Control structures
- โ Variable declarations
- โ Type declarations
- โ Arrays and records
- โ Case statements
- โ Repeat until loops
- โ String operations
See COMPLEX_VALIDATION_SUMMARY.md for full details.
โ Advanced Interpreter Features
Built-in Functions (20+):
abs,sqr,sqrt,sin,cos,ln,expround,trunc,ord,chrlength,pos,concat,copy,upcase,lowercaseodd,succ,pred,randominttostr,strtoint
Built-in Procedures:
write,writeln,readlninc,dec,halt
Control Flow:
- โ if/else statements
- โ while loops
- โ for loops (to/downto)
- โ repeat until loops
- โ case statements
- โ begin/end blocks
โ Code Generation
x86-64 Assembly (Intel syntax):
- Register allocation with graph coloring
- Stack-based variable management
- Proper function prologue/epilogue
- Multiple calling conventions (System V, Win64)
Optimizations:
- Constant folding
- Dead code elimination
- Common subexpression elimination
- Function inlining
- Loop unrolling
- Peephole optimization
โ Language Support
Data Types:
- โ Integer (signed 64-bit)
- โ Real (IEEE 754 double precision)
- โ Boolean
- โ Char
- โ String
- โ Arrays (multi-dimensional)
- โ Records
- โ Pointers
- โ Enumerations
- โ Sets
Operators:
- โ
Arithmetic:
+,-,*,/,div,mod - โ
Comparison:
=,<>,<,<=,>,>= - โ
Logical:
and,or,not - โ
Bitwise:
&,|,xor,<<,>> - โ
Unary:
+,-,not
Control Structures:
- โ if/else
- โ while/do
- โ for/to/downto
- โ repeat/until
- โ case/of
- โ begin/end blocks
โ ๏ธ Current Limitations
While pascal-rs successfully compiles and interprets core Pascal programs, there are features not yet implemented:
Not Yet Implemented:
- โ Object Pascal (classes, inheritance, polymorphism)
- โ Exception handling (try-except-finally)
- โ Generics/templates
- โ File I/O operations
- โ Dynamic arrays
- โ Variant records
- โ Operator overloading
- โ Properties
- โ Interfaces
- โ Advanced standard library (only basic built-ins)
Platform Support:
- โ x86-64 (primary target, well-tested)
- ๐ง ARM, RISC-V, MIPS, PowerPC (planned)
- ๐ง WebAssembly (planned)
For these features, consider using Free Pascal or Delphi.
๐งช Testing
Running Tests
# Run all tests
# Run specific test categories
# Run with output
Test Results
running 169 tests
test result: ok. 169 passed; 0 failed; 0 ignored; 0 measured
All tests passing! โ
Test Coverage
- Lexical Analysis: Complete token coverage
- Parsing: All AST node types
- Type Checking: All type combinations
- Code Generation: All statement types
- Interpreter: All language features
- Edge Cases: Boundary conditions validated
๐ Architecture
Modular Design
pascal-rs/
โโโ src/
โ โโโ lexer.rs # Lexical analysis
โ โโโ parser/ # Syntax analysis
โ โ โโโ mod.rs
โ โ โโโ expression.rs
โ โ โโโ statement.rs
โ โ โโโ decl.rs
โ โโโ interpreter.rs # Tree-walking interpreter
โ โโโ interpreter_value.rs # Value type
โ โโโ interpreter_scope.rs # Scope management
โ โโโ interpreter_function.rs # Function registry
โ โโโ interpreter_traits.rs # Trait abstractions
โ โโโ type_checker.rs # Type validation
โ โโโ optimizer.rs # Optimization passes
โ โโโ unit_codegen.rs # Code generation
โ โโโ resolver.rs # Symbol resolution
โโโ tests/
โ โโโ run_compiler_tests.rs # Compiler validation
โ โโโ run_integration_tests.rs # End-to-end tests
โ โโโ run_complex_validation_tests.rs # Complex scenarios (NEW!)
โ โโโ integration/
โ โโโ complex_validation_tests.rs
โโโ examples/ # Example programs
Trait-Based Components
// Value operations
Compilation Pipeline
Source Code โ Lexer โ Tokens โ Parser โ AST
โ
Type Checker
โ
Optimizer
โ
Code Generator
โ
Assembly
๐ Documentation
- ARCHITECTURE.md - Detailed architecture and design
- TRAIT_ARCHITECTURE.md - Trait-based design guide
- COMPLEX_VALIDATION_SUMMARY.md - Test validation report
- TESTING.md - Testing guide and best practices
- TEST_STATUS.md - Test status and coverage
- TODO.md - Development roadmap
๐ What's New
Latest Release Highlights
โ Trait-Based Architecture (NEW!)
- Added comprehensive trait abstractions
- Improved testability and maintainability
- Better separation of concerns
- See
TRAIT_ARCHITECTURE.md
โ Complex Validation Tests (NEW!)
- 9 new comprehensive tests
- Edge cases and boundary conditions
- Complex operator precedence
- Nested control structures
- 100% pass rate
โ Enhanced Testing Support
- Added
current_scope()method for testing - Added
get_variable_value()for value inspection - Made
Scopestruct public withget()method
โ Improved Documentation
- Trait architecture guide
- Test validation summary
- Comprehensive examples
- Better inline documentation
๐ฏ Use Cases
1. Education
- Learn compiler construction with clean, modern code
- Understand trait-based design patterns
- Study optimization techniques
- Explore language implementation
2. Research & Experimentation โ Perfect Fit
- Test new optimization algorithms
- Experiment with trait-based architectures
- Benchmark compilation strategies
- Prototype language features
- Study type systems
- Research memory-safe compiler design
3. Small Projects โ Suitable
- Command-line utilities
- Scripts and automation
- Learning exercises
- Algorithm implementation
- Data processing tasks
- Homework assignments
4. Legacy Modernization โ ๏ธ Consider Limitations
- Works for standard Pascal code
- Missing Object Pascal features (classes, exceptions)
- Good for simple procedural Pascal programs
- May need modifications for advanced features
Not Recommended For (Yet):
- โ Large commercial applications (use Free Pascal or Delphi)
- โ Complex GUI applications (no framework support yet)
- โ Database-heavy applications (limited database library support)
- โ Embedded systems (limited platform support)
- โ Projects requiring Object Pascal classes/exceptions
๐ Acknowledgments
- Inspired by the Free Pascal Compiler (FPC)
- Built with Rust and Cargo
- Uses modern compiler construction techniques
- Benefits from the Rust community's expertise
Made with โค๏ธ in Rust
Status: Active Development | Tests: 169/169 Passing | Language: Core Pascal Complete