Author: Muhammad Musa Khan
Course: CSCI-GA 3110 Honors Programming Languages
Institute: New York University, Courant Institute of Mathematical Sciences
The MiniOO Interpreter is a functional implementation of a simple object-oriented programming language, MiniOO. It supports key programming language constructs such as procedures, dynamic memory allocation, and conditional statements, etc.
This project uses OCaml with components like Menhir for parsing and OCamllex for lexical analysis. The interpreter implements:
- Static semantics for lexical scoping validation.
- Small-step operational semantics for program execution.
- OCaml (v4.12.0 or later)
- Menhir: Install it via OPAM:
opam install menhir
- Unzip the Project: Unzip all project files into a folder.
- Build the Project: Run the
make
command to compile the project:make all
- Run the Interpreter:
- To execute a MiniOO program from a file:
./interpreter < examples/input1.oo
- To enter MiniOO code manually (interactive mode):
Exit interactive mode by pressing
./interpreter
Ctrl+D
twice.
- To execute a MiniOO program from a file:
- Clean Up: To remove all compiled files and executables:
make delete
- examples/: Contains sample MiniOO programs showcasing various features.
- makefile: Automates the build and clean-up processes.
- main.ml: Entry point of the interpreter.
- lexer.mll: Implements lexical analysis.
- parser.mly: Implements syntax analysis using Menhir.
- ast.ml: Defines the Abstract Syntax Tree (AST) for MiniOO.
- operationalSemantics.ml: Implements runtime behavior of MiniOO programs.
- staticSemantics.ml: Implements static checks to ensure program correctness.
- prettyPrint.ml: Provides human-readable representations of AST and state.
- operationalTypes.ml: Defines runtime components like stack, memory heap, and state.
- Variable Declaration and Assignment
- Procedures and Function Calls
- Conditional Statements
- While Loops
- Field Access and Dynamic Memory Allocation (malloc)
- Parallel Execution and Atomic Blocks
- OCaml style nested multi-line comments
Below are the MiniOO example programs included in the examples/
directory.
(* Variable Declaration and Assignment *)
var x;
x = 10;
if (x > 5) then {print(x);} else {print(0);};
- Demonstrates variable assignment and conditionals.
(* Procedure and Function Call *)
var p;
p = proc y: print(y + 10); print(0);;
p(5);
- Demonstrates procedure declaration and calls.
(* Conditional Statements *)
var x;
x = 10;
if (x > 5) then {print(x);} else {print(0);};
x = 2;
if x < 5 then print(x); else print(1);;
x = 10;
if x == 10 then print(2);;
- Supports optional parentheses and optional
else
branches.
(* While Loop and Arithmetic Operators *)
var x;
x = (10 * 2) / 4 + 1 - 2;
while (x > 0) {
print(x);
x = x - 1;
};
- Demonstrates loops, arithmetic operators, and semi-colon rules.
(* Field Access and Malloc *)
var x;
(* malloc(x); *) (* Uncomment this line to allocate memory and prevent errors *)
x.F = 100;
print(x.F);
- Requires
malloc
for dynamic object allocation.
(* Parallel Execution *)
var x;
var y;
{
x = 1; print(x); print(x);
|||
y = 2; atom(print(y); print(y););
};
- Demonstrates parallel execution with the
|||
operator. - Atomic blocks ensure uninterrupted execution.
(* Factorial Calculation *)
var factorial;
var result;
result = 1;
factorial = proc n: {
if (n == 0) then {
print(result);
} else {
result = result * n;
factorial(n - 1);
};
};
factorial(5);
- Recursive procedure to calculate factorial.
-
Syntax Rules:
- Variable names start with lowercase letters.
- Field names start with uppercase letters.
- Semi-colons are required after the last command.
-
Limitations:
- Fields must be integers, not variable objects.
- Parallel execution randomly selects one branch.
To run the factorial example:
./interpreter < examples/input7.oo
Output:
120
This interpreter adheres to the formal syntax and operational semantics of MiniOO as described in the project specification. The project implements both static semantics (e.g., scoping rules) and operational semantics for a clear and robust execution of MiniOO programs.
For more details, refer to the Mini-SOS.pdf document or explore the project files.
This project was implemented as part of the CSCI-3110 Honors Programming Languages course.