A sophisticated, statically-typed programming language with object-oriented features, implemented as a complete interpreter pipeline in Standard ML (SML). Nexus demonstrates advanced programming language concepts including lexical analysis, parsing, type checking, and evaluation with support for classes, inheritance, closures, and first-class functions.
- Static Type System: Comprehensive compile-time type checking with detailed error reporting
- Object-Oriented Programming: Full OOP support with classes, inheritance, method dispatch, and the
thiskeyword - First-Class Functions: Functions as values with lexical scoping and closure support
- Lexical Scoping: Proper variable binding with environment-based evaluation
- Control Flow: Conditional statements, loops, and early returns
int- Integer arithmetic with standard operations (+,-,*,/,%)bool- Boolean logic with short-circuit evaluation (&&,||,!)string- String literals with concatenation supportnone- Null/void type for optional valuesfunction- First-class functions with closure captureobject- Instance objects with field access and method dispatch
- Inheritance: Classes can extend other classes using
super - Method Overriding: Support for polymorphic method dispatch
- Closures: Functions capture their lexical environment
- Anonymous Functions: Lambda expressions for functional programming
- Ternary Operator: Conditional expressions (
condition ? then : else) - Dynamic Dispatch: Runtime method resolution for object-oriented code
The interpreter implements a complete language processing pipeline:
- Converts source code into a stream of tokens
- Handles keywords, operators, literals, identifiers, and delimiters
- Comprehensive error reporting for invalid characters and malformed tokens
- Recursive descent parser with operator precedence
- Generates Abstract Syntax Tree (AST) from token stream
- Handles complex expressions, statements, and class declarations
- Robust error recovery and meaningful parse error messages
- Defines data structures for all language constructs
- Supports expressions, statements, declarations, and program structure
- Clean separation between syntax and semantics
- Complete static analysis before execution
- Type inference and checking for all expressions and statements
- Validates function signatures, class hierarchies, and variable declarations
- Prevents common runtime errors through compile-time analysis
- Environment-based interpreter with lexical scoping
- Hash table implementation for efficient variable lookup
- Object system with prototype-based inheritance
- Proper closure implementation with environment capture
- Comprehensive error handling throughout the pipeline
- Detailed error messages for debugging
- Covers lexical, syntactic, semantic, and runtime errors
var x;
var message;x = 42;
message = "Hello, World!";
result = x + 10;
isValid = x > 0 && message != "";if (x > 0) {
print "Positive";
} else {
print "Non-positive";
}
while (x > 0) {
print x;
x = x - 1;
}// Function declaration
var factorial;
factorial = func(n: int) -> int {
return n <= 1 ? 1 : n * factorial(n - 1);
};
// Anonymous functions
var square = func(x: int) -> int { return x * x; };// Class definition
class Animal {
var name;
var age;
func speak() -> string {
return "Some animal sound";
}
}
class Dog : Animal {
func speak() -> string {
return "Woof!";
}
}
// Object instantiation and method calls
var myDog = new Dog();
myDog.name = "Rex";
print myDog.speak(); // Output: "Woof!"- Static typing with compile-time type checking
- Type inference for expressions and assignments
- Function types with parameter and return type checking
- Object types with field and method validation
- Inheritance checking for class hierarchies
- Environment chains for lexical scoping
- Hash table-based variable storage for efficiency
- Reference-based equality for objects and functions
- Automatic memory management through SML's garbage collector
- Lexical errors: Invalid characters, unterminated strings
- Syntax errors: Malformed expressions, missing tokens
- Type errors: Type mismatches, undefined variables
- Runtime errors: Division by zero, null pointer access
This implementation demonstrates mastery of several advanced computer science concepts:
- Compiler Construction: Complete pipeline from source to execution
- Type Theory: Static type system with inference and checking
- Object-Oriented Design: Inheritance, polymorphism, and encapsulation
- Functional Programming: First-class functions, closures, and lexical scoping
- Data Structures: Hash tables, trees, and environment chains
- Language Design: Syntax design, semantic analysis, and runtime systems
The interpreter is implemented in Standard ML. To run a Nexus program:
-
Load the SML modules in order:
tokens.smlast.smlexceptions.smltokenizer.smlparser.smltypechecker.smlevaluator.sml
-
Parse and evaluate a program:
(* Parse from file *)
val program = parse_file "example.nexus";
(* Type check *)
typecheck program;
(* Evaluate *)
evaluate program;Developed for CSC 430 (Programming Languages) - Cal Poly San Luis Obispo