A compiler for FOOL (Functional Object-Oriented Language), developed for the course Linguaggi Compilatori e Modelli Computazionali — MSc in Computer Science and Engineering, University of Bologna (UNIBO).
FOOL is a small functional and object-oriented language supporting:
- Primitive types:
int,bool - Variables and functions with local declarations (
let ... in) - Classes with single inheritance (
class ... extends ...) - Methods and method dispatch via dot notation (
obj.method(...)) - Object instantiation (
new ClassName(...)) - Null reference and null-equality checks
- Control flow:
if ... then { ... } else { ... } - Arithmetic and logical operators:
+,-,*,/,&&,||,! - Comparison operators:
==,>=,<= - Print statement
.
├── compiler/ # Front-end and code generation
│ ├── FOOL.g4 # ANTLR4 grammar for FOOL
│ ├── AST.java # Abstract Syntax Tree node definitions
│ ├── ASTGenerationSTVisitor.java # ST → AST visitor
│ ├── SymbolTableASTVisitor.java # Symbol table construction
│ ├── TypeCheckEASTVisitor.java # Type checker
│ ├── PrintEASTVisitor.java # AST pretty-printer
│ ├── CodeGenerationASTVisitor.java # Code generator (SVM assembly)
│ ├── TypeRels.java # Type relation helpers (subtyping)
│ ├── STentry.java # Symbol table entry
│ ├── Test.java # Main entry point
│ ├── exc/ # Compiler exceptions
│ └── lib/ # Base visitor classes and utilities
├── svm/ # Stack Virtual Machine
│ ├── SVM.g4 # ANTLR4 grammar for SVM assembly
│ └── ExecuteVM.java # SVM interpreter
├── lib/
│ └── antlr-4.13.1-complete.jar # ANTLR4 runtime
└── prova.fool # Example FOOL program
- Lexing & Parsing — ANTLR4 tokenises and parses the source file into a parse tree using
FOOL.g4. - AST Generation —
ASTGenerationSTVisitorconverts the parse tree into an AST. - Symbol Table Analysis —
SymbolTableASTVisitorresolves identifiers and builds the enriched AST (EAST). - Type Checking —
TypeCheckEASTVisitorverifies type correctness, supporting subtyping and OOP dispatch rules. - Code Generation —
CodeGenerationASTVisitoremits assembly code for the Stack Virtual Machine. - Assembly & Execution — The SVM assembles and executes the generated
.asmfile.
- Java 11 or higher
- ANTLR 4.13.1 (included in
lib/antlr-4.13.1-complete.jar)
javac -cp lib/antlr-4.13.1-complete.jar compiler/*.java compiler/lib/*.java compiler/exc/*.java svm/*.javaPlace your source file (e.g. prova.fool) in the project root, then run:
java -cp .:lib/antlr-4.13.1-complete.jar compiler.TestOn Windows, replace : with ; in the classpath:
java -cp ".;lib/antlr-4.13.1-complete.jar" compiler.TestThe compiler will:
- Report any lexical, syntax, symbol-table, and type errors.
- If the front-end is error-free, emit
prova.fool.asmand immediately run it on the SVM.
The included prova.fool demonstrates class inheritance and method overriding with a BankLoan / MyBankLoan hierarchy:
let
class Account (money:int) {
fun getMon:int () money;
}
class TradingAcc extends Account (invested:int) {
fun getInv:int () invested;
}
...
var bl:BankLoan = new MyBankLoan(new TradingAcc(50000,40000));
var myLoan:Account = bl.openLoan(myTradingAcc);
in print(if (myLoan==null) then {0} else {myLoan.getMon()});