Skip to content

Latest commit

 

History

History
43 lines (35 loc) · 1.06 KB

c_as_backend.md

File metadata and controls

43 lines (35 loc) · 1.06 KB

C as Backend

Currently the target language is C. That is, the milone-lang compiler reads a source code and translates to a C code. For example, assume the following code is given.

let main _ =
  let rec factorial x =
    if x = 0 then 1 else x * factorial (x - 1)
  factorial 5 - 120 // exit code

The compiler emits a C code that is equivalent to the following:

int factorial(int x) {
    if (x == 0) {
        return 1;
    } else {
        return x * factorial(x - 1);
    }
}

int main() {
    return factorial(5) - 120;
}

The actual output is available at factorial.c.

The diagram below illustrates how it does self-hosting.

    <milone-lang compiler>.fs
        |                | Compile with F# compiler
        |                v
        | Compile with <milone-lang compiler>.exe
        v                           ^ an executable on .NET runtime
    <milone-lang compiler>.c
        | Compile with C compiler
        v
    <milone-lang compiler>.exe
                 ^ a native executable