Fern
Fern is a multi-stage optimizing compiler for a statically-typed imperative programming language.
Example
A simple program that calculates the 10th number in the Fibonacci series:
fn fib(n: int) -> int {
if (n <= 1) {
return n;
}
return fib(n - 1) + fib(n - 2);
}
fn main() -> int {
return fib(10);
}
Compile and run:
$ fern example.fern
$ ./example
$ echo $?
55
Compilation Pipeline
The compiler implements a modern compilation pipeline with several optimization stages:
- Source code is parsed into an Abstract Syntax Tree (AST)
- A symbol table is generated to track variable and function scopes
- The AST is converted to Three-Address Code (TAC)
- Dead code elimination removes unreachable code paths
- A Control Flow Graph (CFG) is built for analysis
- Conversion to Static Single Assignment (SSA) form
- SSA is translated to LLVM Intermediate Representation (IR)
- Finally, LLVM compiles the IR to a native executable
Source
The source for Fern is available on GitHub.