An interpreter for the Hawat programming language, implemented in Haskell. Hawat is a statically-typed imperative programming language that supports functions, lambdas, control flow, and basic data types.
Hawat is a programming language featuring:
- Static typing with type inference
- Basic types:
int,string,bool,void - Function types:
(Type1, Type2) -> ReturnType - Array types:
[Type](planned for future implementation) - Lambda expressions with closures
- Control flow: if/else, while loops, for loops
- Loop control: break and continue statements
- Built-in functions:
printInt(),printStr(), and more
fun factorial (int n) -> int {
if (n <= 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
fun main () -> int {
printInt(factorial(5)); printStr("\n"); // Output: 120
return 0;
}
fun main () -> int {
(int) -> int double = lambda(int x) -> int {return 2 * x;};
printInt(double(5)); printStr("\n"); // Output: 10
return 0;
}
hawat/
├── src/ # Source code
│ ├── Hawat.cf # Grammar definition (BNF format)
│ ├── Main.hs # Main entry point
│ ├── Interpreter.hs # Program interpreter
│ ├── InterpreterEnv.hs # Interpreter environment
│ ├── InterpreterError.hs # Interpreter error handling
│ ├── Typechecker.hs # Static type checker
│ ├── TypecheckerEnv.hs # Typechecker environment
│ ├── TypecheckerError.hs # Typechecker error handling
│ ├── Utils.hs # Utility functions
│ └── Parser/ # Generated parser files (created by BNFC)
├── good/ # Test programs that should succeed
├── bad/ # Test programs that should fail
├── Makefile # Build configuration
├── run_tests.py # Test runner script
└── README.md # This file
- GHC (Glasgow Haskell Compiler)
- BNFC (BNF Converter) - for generating the parser from grammar
- Make - for building the project
- Python 3 - for running tests
On Ubuntu/Debian:
sudo apt-get install ghc bnfc make python3On macOS with Homebrew:
brew install ghc bnfc make python3-
Clone the repository:
git clone <repository-url> cd hawat
-
Build the interpreter:
make
This will:
- Generate parser files from the grammar (
src/Hawat.cf) using BNFC - Compile the Haskell source code
- Create the
interpreterexecutable
From file:
./interpreter program.hwtFrom stdin:
./interpreter < program.hwt
# or
echo 'fun main() -> int { printStr("Hello World\n"); return 0; }' | ./interpreterHelp:
./interpreter --helpEvery Hawat program must have a main function with signature () -> int:
fun main () -> int {
// Your code here
return 0; // Exit code
}
The project includes a comprehensive test suite:
python3 run_tests.pyOptions:
--interpreter path/to/interpreter- Specify interpreter executable--test_folder path/to/tests- Specify test directory
good/- Programs that should execute successfullybad/- Programs that should fail (syntax errors, type errors, runtime errors)
Each test consists of:
*.hwt- Source file*.out- Expected output
int- Integer numbersstring- Text stringsbool- Boolean values (true,false)void- Void type (no value)(Type1, Type2) -> ReturnType- Function types[Type]- Array types (planned for future implementation)
// Variable declarations
int x = 5;
string name = "Hawat";
bool flag = true;
// Function definition
fun add(int a, int b) -> int {
return a + b;
}
// Arrays (planned for future implementation)
// [int] numbers = [1, 2, 3, 4, 5];
// printInt(numbers[0]); // Access element
// If statements
if (condition) {
// code
} else if (other_condition) {
// code
} else {
// code
}
// While loops
while (x < 10) {
x++;
}
// For loops
for (i = 0 to 9) {
printInt(i);
}
// Break and continue
while (true) {
if (condition) break;
if (other_condition) continue;
// code
}
fun main() -> int {
// Lambda with closure
int multiplier = 3;
(int) -> int multiply = lambda(int x) -> int {
return x * multiplier;
};
printInt(multiply(5)); // Output: 15
return 0;
}
The interpreter provides detailed error messages for:
- Parse errors - Invalid syntax
- Type errors - Type mismatches, undeclared variables
- Runtime errors - Division by zero, etc.
make clean # Remove build files
make distclean # Remove build files and executable