Skip to content

iohub/fx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

92 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fx

a static strong typed language

build

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

run

./fx --file ../examples/demo.rb

visualize

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
}

reference

  1. Type infer http://www.cse.chalmers.se/edu/year/2011/course/CompFun/lecture4.pdf
  2. Yacc grammar https://www.lysator.liu.se/c/ANSI-C-grammar-y.html
  3. Type checker https://jaked.org/blog/2021-09-15-Reconstructing-TypeScript-part-1
  4. Bidirectional Type Checking https://www.youtube.com/watch?v=utyBNDj7s2w
  5. Mapping High Level Constructs to LLVM IR https://mapping-high-level-constructs-to-llvm-ir.readthedocs.io/en/latest/README.html

Releases

No releases published

Packages

No packages published