https://github.com/kkestell/fern
Multi-stage optimizing compiler for a statically-typed imperative programming language.
https://github.com/kkestell/fern
compiler llvm python
Last synced: 12 months ago
JSON representation
Multi-stage optimizing compiler for a statically-typed imperative programming language.
- Host: GitHub
- URL: https://github.com/kkestell/fern
- Owner: kkestell
- License: 0bsd
- Created: 2025-01-04T10:29:17.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-15T11:47:55.000Z (about 1 year ago)
- Last Synced: 2025-02-06T02:52:09.120Z (about 1 year ago)
- Topics: compiler, llvm, python
- Language: Python
- Homepage:
- Size: 35.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Fern
A multi-stage optimizing compiler for a statically-typed imperative programming language.
## Example
```
def fib(n: int): int
if n <= 1 then
return n
end
return fib(n - 1) + fib(n - 2)
end
def main(): int
return fib(10)
end
```
```
$ fern example.fern
$ ./example
$ echo $?
55
```
## Compilation Pipeline
The compiler implements a modern compilation pipeline with several optimization stages:
1. Source code is parsed into an Abstract Syntax Tree (AST)
2. A symbol table is generated to track variable and function scopes
3. The AST is converted to Three-Address Code (TAC)
4. Dead code elimination removes unreachable code paths
5. A Control Flow Graph (CFG) is built for analysis
6. Conversion to Static Single Assignment (SSA) form
7. SSA is translated to LLVM Intermediate Representation (IR)
8. Finally, LLVM compiles the IR to a native executable
## TODO
* [ ] Type inference
* [ ] Arrays and composite types
* [ ] Loop constructs (while, for)
* [ ] Standard library functions
* [ ] More aggressive optimizations
* [ ] Better error messages