adam-mcdaniel/sage

Function Flattening

adam-mcdaniel opened this issue · 0 comments

Right now, the compiler will generate code with nested function definitions. This is fine by the virtual machine specification, but it makes it harder to compile the output code. Right now, neither the interpreter or C target call functions properly. In both implementations, functions are only defined when their definition is executed.

To fix this, we simply need to flatten the functions like so:

fn f0() {
    fn f1() {
        fn f2() {

        }
        fn f3() {
            fn f4() {
            }
        }
    }
}
// Becomes:
fn f0() {}
fn f1() {}
fn f2() {}
fn f3() {}
fn f4() {}

This can be done in the VM code, it doesn't have to be done in the frontend. This should just be a simple pass before we compile the code to a specific target.