adam-mcdaniel / oakc

A portable programming language with a compact intermediate representation
Apache License 2.0
725 stars 21 forks source link

WIP: add initial mvp for optimizing programs #77

Closed kevinramharak closed 5 months ago

kevinramharak commented 4 years ago

This PR:

It still needs work on implementing the Display trait with readable indents and to work for nested conditional statements like for, while and if statements.

I decided to get more familiar with rust and the codebase so I started exploring a possible implementation of optimizing the IR. I noticed a lot of work is done in MIR so i decided to try and implement an MVP here. I have no idea if this is a good way of implementing it, but I think it shows some of the possiblities.

The current approach is limited to returning a similar type/enum of whatever is being optimized. This restricts things like if (true) { /* body */ } to be optimized to inlining the body. Tough there should also be more clarity about scopes and identifiers before that can be implemented.

It will succesfully optimize the following program:

fn square() -> num {
    return 2 * 2;
}

fn main() {
    if (8 >= 3) {
        let _ = 2 * 8;
    } else {
        let _ = 4 / 2;
    }
}

into the following:

fn main() -> void {
    if (true) {
        let _ = 16;
    } else {
        let _ = 2;
    }
}