A compiler implementation for the RPAL (Right-reference Pedagogical Algorithmic Language) programming language that transforms RPAL source code into an executable form using a CSE (Control Structure Evaluator) machine.
This project implements a full compiler pipeline for the RPAL language with the following components:
- Scanner (
scanner.py): Performs lexical analysis to convert source code into tokens. - Parser (
ASTParser.py): Converts tokens into an Abstract Syntax Tree (AST). - Standardizer (
standardizer.py): Transforms the AST into a Standardized Tree (ST) with simplified constructs. - CSE Machine (
standardizer.py): Executes the ST code using a control-stack-based evaluator.
| File | Description |
|---|---|
ASTNode.py |
Defines the Abstract Syntax Tree (AST) node class |
ASTParser.py |
Implements the recursive descent parser |
scanner.py |
Implements the lexical analyzer |
standardizer.py |
Transforms AST and implements the CSE machine |
environment.py |
Defines the environment structure for variable scoping |
myrpal.py |
Main driver script |
Makefile |
Build automation for testing |
tests/ |
Comprehensive test suite with 20+ RPAL test programs |
- ✅ Full implementation of RPAL language constructs
- 🧠 AST visualization with
-astflag - ♻️ Support for:
- Lambda expressions and closures
- Recursive functions
- Higher-order functions
- Tuples and tuple operations
- Conditional expressions
- Built-in functions:
Print,Stem,Stern,Conc, etc.
Run from the command line:
python myrpal.py [-ast] file.rpal-ast: Display the Abstract Syntax Tree (AST) of the input RPAL program.
python myrpal.py test.rpal # Run and evaluate the RPAL program
python myrpal.py -ast test.rpal # Display the AST without executing
python myrpal.py tests/test_factorial.rpal # Run a test from the test suite
python myrpal.py -ast tests/test_tuples.rpal # View AST for tuple operationslet Sum(A) = Psum(A, Order A)
where rec Psum(T, N) = N eq 0 -> 0
| Psum(T, N-1) + T N
in Print( Sum(1,2,3,4,5) )
This defines a Sum function using a recursive helper Psum to compute the sum of a tuple. Executing this program prints:
15
- Scanning: Tokenizes the source code.
- Parsing: Builds the AST from tokens.
- Standardization: Converts AST into simplified ST.
- Control Structure Generation: Wraps ST into control structures.
- Execution: CSE machine executes the control structures.
- Identifies tokens: keywords, operators, identifiers, numbers, symbols.
- Implements recursive descent parsing for RPAL grammar rules.
- Transforms high-level constructs (e.g.,
let,where,rec) into pure functional representations usinglambda,gamma, andtau.
- Uses:
- Control Stack: Instructions to execute.
- Value Stack: Operands/results.
- Environment Stack: Variable scoping.
RPAL is a functional language inspired by ML, Lisp, and Scheme.
- First-class functions and higher-order functions
- Pattern matching with conditional expressions
- Lexical scoping and recursive definitions
- Tuple operations and built-in library
The tests/ directory contains comprehensive test cases covering various RPAL language features:
test_basic_let.rpal- Basic let expressions and arithmetictest_conditional.rpal- Conditional expressionstest_string.rpal- String literals and operationstest_where.rpal- Where clauses
test_function_definitions.rpal- Function definitions withwithinclausetest_lambda_function.rpal- Lambda expressions (fn notation)test_function_parameter.rpal- Passing functions as parameterstest_function_return.rpal- Returning functions from functionstest_conditional_function.rpal- Selecting functions using conditionalstest_nary_function.rpal- N-ary functions using tuples
test_factorial.rpal- Recursive factorial functiontest_nested_scopes.rpal- Nested scope behaviortest_simultaneous_definitions.rpal- Simultaneous definitions with 'and'test_string_length.rpal- Recursive string length calculationtest_perfect_square.rpal- Perfect square detection using recursion
test_tuples.rpal- Basic tuple operations and nested data structurestest_arrays.rpal- Array-like tuple structures with indexingtest_multidimensional_arrays.rpal- Tuples of tuples (matrices)test_triangular_array.rpal- Triangular array data structuretest_tuple_function.rpal- Functions stored in tuples
test_normal_order.rpal- Normal order vs PL order evaluationtest_at_operator.rpal- The @ operator for function applicationtest_sum_list.rpal- List summation with partial functionstest_vector_sum.rpal- Vector addition operations
Using Make (Recommended):
make all # Run all tests
make test_basic_let # Run specific test
make test-basic # Run basic language feature tests
make test-functions # Run function-related tests
make test-recursion # Run recursion-related tests
make test-data-structures # Run data structure tests
make test-advanced # Run advanced feature tests
make ast-all # Run all tests with AST output
make list # List all available testsDirect Python Execution:
python myrpal.py tests/test_factorial.rpal
python myrpal.py -ast tests/test_lambda_function.rpalFuture enhancements may include:
- Adding more built-in functions
- Type checking and error recovery
- Debugging and trace output
- Optimizing the CSE machine
- Python 3.6+
This project is provided for educational purposes.
- University project implementation by Anas Hussaindeen & Tumasha Deshan