a static strong typed language
git clone https://github.com/iohub/fx && cd fx
git submodule init && git submodule update
mkdir build_ninja
cd build_ninja
cmake -G Ninja .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=On
ninja
./fx --file ../examples/demo.rb
def fib(int x) int {
if (x <= 0) {
return 0
}
if (x == 1) {
return 1
}
return fib(x-1) + fib(x-2)
}
def main() int {
int z
z = 8
z = z + fib(5)
return fib(z)
}
- simple typed ast
- llvm IR
; ModuleID = '../examples/fib.rb'
source_filename = "../examples/fib.rb"
define i32 @fib(i32 %x) {
entry:
%x1 = alloca i32
%fib_retvar = alloca i32
store i32 %x, i32* %x1
%0 = load i32, i32* %x1
%1 = icmp sle i32 %0, 0
br i1 %1, label %_then, label %_end
_then: ; preds = %entry
store i32 0, i32* %fib_retvar
br label %_ret
_end: ; preds = %entry
%2 = load i32, i32* %x1
%3 = icmp eq i32 %2, 1
br i1 %3, label %_then2, label %_end3
_then2: ; preds = %_end
store i32 1, i32* %fib_retvar
br label %_ret
_end3: ; preds = %_end
%4 = load i32, i32* %x1
%add = sub i32 %4, 1
%5 = call i32 @fib(i32 %add)
%6 = load i32, i32* %x1
%add4 = sub i32 %6, 2
%7 = call i32 @fib(i32 %add4)
%add5 = add i32 %5, %7
store i32 %add5, i32* %fib_retvar
br label %_ret
_ret: ; preds = %_end3, %_then2, %_then
%8 = load i32, i32* %fib_retvar
ret i32 %8
}
define i32 @main() {
entry:
%z = alloca i32
%main_retvar = alloca i32
store i32 8, i32* %z
%0 = load i32, i32* %z
%1 = call i32 @fib(i32 5)
%add = add i32 %0, %1
store i32 %add, i32* %z
%2 = load i32, i32* %z
%3 = call i32 @fib(i32 %2)
store i32 %3, i32* %main_retvar
br label %_ret
_ret: ; preds = %entry
%4 = load i32, i32* %main_retvar
ret i32 %4
}
- Type infer http://www.cse.chalmers.se/edu/year/2011/course/CompFun/lecture4.pdf
- Yacc grammar https://www.lysator.liu.se/c/ANSI-C-grammar-y.html
- Type checker https://jaked.org/blog/2021-09-15-Reconstructing-TypeScript-part-1
- Bidirectional Type Checking https://www.youtube.com/watch?v=utyBNDj7s2w
- Mapping High Level Constructs to LLVM IR https://mapping-high-level-constructs-to-llvm-ir.readthedocs.io/en/latest/README.html